From 30789669b737f4d9b3715d1275945d38d3c96973 Mon Sep 17 00:00:00 2001 From: cws Date: Mon, 5 Jun 2017 21:45:34 -0400 Subject: [PATCH] stats: Add a dumb URL for getting all stats from the db --- example_config.json | 4 ++++ plugins/stats/stats.go | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/example_config.json b/example_config.json index 11c5bd4..777a2bc 100644 --- a/example_config.json +++ b/example_config.json @@ -67,5 +67,9 @@ }, "Reminder": { "MaxBatchAdd" : 10 + }, + "Stats": { + "DBPath": "stats.db", + "Sightings": ["user"] } } diff --git a/plugins/stats/stats.go b/plugins/stats/stats.go index 58d652c..d6c4729 100644 --- a/plugins/stats/stats.go +++ b/plugins/stats/stats.go @@ -5,7 +5,10 @@ package stats import ( "encoding/json" + "fmt" "log" + "net/http" + "os" "strings" "time" @@ -91,7 +94,7 @@ func statFromDB(path, day, bucket, key string) (stat, error) { buk := []byte(bucket) k := []byte(key) if err != nil { - log.Printf("Couldn't open BoltDB for stats: %s", err) + log.Printf("Couldn't open BoltDB for stats (%s): %s", path, err) return stat{}, err } defer db.Close() @@ -132,7 +135,7 @@ func statFromDB(path, day, bucket, key string) (stat, error) { func (s stats) toDB(path string) error { db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 1 * time.Second}) if err != nil { - log.Printf("Couldn't open BoltDB for stats: %s", err) + log.Printf("Couldn't open BoltDB for stats (%s): %s", path, err) return err } defer db.Close() @@ -212,8 +215,20 @@ func (p *StatsPlugin) BotMessage(message msg.Message) bool { func (p *StatsPlugin) Help(e string, m []string) { } +func (p *StatsPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { + f, err := os.Open(p.bot.Config().Stats.DBPath) + if err != nil { + log.Printf("Error opening DB for web service: %s", err) + fmt.Fprintf(w, "Error opening DB") + return + } + http.ServeContent(w, r, "stats.db", time.Now(), f) +} + func (p *StatsPlugin) RegisterWeb() *string { - return nil + http.HandleFunc("/stats", p.serveQuery) + tmp := "/stats" + return &tmp } func (p *StatsPlugin) mkUserStat(message msg.Message) stats {