Start Building Stats Page

This commit is contained in:
Brian Buller 2015-10-30 08:10:05 -05:00
parent aef1feb64f
commit fff9cb42e9
4 changed files with 69 additions and 2 deletions

View File

@ -56,7 +56,7 @@ func main() {
} }
// For now, we're not running the web server // For now, we're not running the web server
//statWebMain(slack) statWebMain(slack)
statBotMain(slack) statBotMain(slack)
} }

View File

@ -717,6 +717,59 @@ func decrementUserStat(user string, key string) error {
return addUserStat(user, key, -1) return addUserStat(user, key, -1)
} }
func getAllUserStats(user string) map[string]int {
var ret map[string]int
openDatabase()
err := db.Update(func(tx *bolt.Tx) error {
var b, uB, uSB *bolt.Bucket
var err error
b = tx.Bucket([]byte("users"))
if uB, err = b.CreateBucketIfNotExists([]byte(user)); err == nil {
if uSB, err = uB.CreateBucketIfNotExists([]byte("stats")); err == nil {
uSB.ForEach(func(k, v []byte) error {
if st, err = bktGetInt(uSB, k); err == nil {
ret[k] = ret[k] + st
return nil
}
return err
})
}
}
return err
})
closeDatabase()
return ret
}
func getAllUsersStats() (map[string]int, error) {
var ret map[string]int
openDatabase()
err := db.Update(func(tx *bolt.Tx) error {
var b, uB, uSB *bolt.Bucket
var err error
b = tx.Bucket([]byte("users"))
err = b.ForEach(func(k, v []byte) error {
if v == nil {
// Bucket
if uB, err = b.CreateBucketIfNotExists([]byte(k)); err == nil {
if uSB, err = uB.CreateBucketIfNotExists([]byte("stats")); err == nil {
uSB.ForEach(func(k, v []byte) error {
if st, err = bktGetInt(uSB, k); err == nil {
ret[k] = ret[k] + st
return nil
}
return err
})
}
}
}
})
return err
})
closeDatabase()
return ret, err
}
func bktGetBucket(b *bolt.Bucket, key string) (*bolt.Bucket, error) { func bktGetBucket(b *bolt.Bucket, key string) (*bolt.Bucket, error) {
bkt := b.Bucket([]byte(key)) bkt := b.Bucket([]byte(key))
if bkt != nil { if bkt != nil {

View File

@ -83,9 +83,16 @@ func initRequest(w http.ResponseWriter, req *http.Request) {
func handleStats(w http.ResponseWriter, req *http.Request) { func handleStats(w http.ResponseWriter, req *http.Request) {
initRequest(w, req) initRequest(w, req)
type StatData struct {
Stats map[string]int
}
// Get the global stats
stats := getAllUsersStats()
s := StatData{Stats: stats}
setMenuItemActive("Stats") setMenuItemActive("Stats")
showPage("stats.html", site, w) showPage("stats.html", site, w)
} }

View File

@ -1,3 +1,10 @@
<div> <div>
devICT Slack Statistics! devICT Slack Statistics!
</div> </div>
<div>
<table>
{{ for $k, $v := range .TemplateData.Stats }}
<tr><td>{{ $k }}</td><td>{{ $v }}</td></tr>
{{ end }}
</table>
</div>