Building stats pages

Hopefully fixed an issue with user stats too
This commit is contained in:
Brian Buller 2015-10-30 09:33:18 -05:00
parent fff9cb42e9
commit 1f5eae33b9
4 changed files with 45 additions and 23 deletions

View File

@ -105,12 +105,10 @@ func processMessage(slack *Slack, m *Message) {
} }
for _, stats := range statProcessors { for _, stats := range statProcessors {
if err == nil { if usr.IsBot {
if usr.IsBot { stats.ProcessBotMessage(m)
stats.ProcessBotMessage(m) } else {
} else { stats.ProcessMessage(m)
stats.ProcessMessage(m)
}
} }
} }
for _, proc := range messageProcessors { for _, proc := range messageProcessors {

View File

@ -717,8 +717,8 @@ func decrementUserStat(user string, key string) error {
return addUserStat(user, key, -1) return addUserStat(user, key, -1)
} }
func getAllUserStats(user string) map[string]int { func getAllUserStats(user string) (map[string]int, error) {
var ret map[string]int ret := make(map[string]int)
openDatabase() openDatabase()
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
var b, uB, uSB *bolt.Bucket var b, uB, uSB *bolt.Bucket
@ -726,9 +726,12 @@ func getAllUserStats(user string) map[string]int {
b = tx.Bucket([]byte("users")) b = tx.Bucket([]byte("users"))
if uB, err = b.CreateBucketIfNotExists([]byte(user)); err == nil { if uB, err = b.CreateBucketIfNotExists([]byte(user)); err == nil {
if uSB, err = uB.CreateBucketIfNotExists([]byte("stats")); err == nil { if uSB, err = uB.CreateBucketIfNotExists([]byte("stats")); err == nil {
var st int
var key string
uSB.ForEach(func(k, v []byte) error { uSB.ForEach(func(k, v []byte) error {
if st, err = bktGetInt(uSB, k); err == nil { key = string(k)
ret[k] = ret[k] + st if st, err = bktGetInt(uSB, key); err == nil {
ret[key] = ret[key] + st
return nil return nil
} }
return err return err
@ -738,11 +741,11 @@ func getAllUserStats(user string) map[string]int {
return err return err
}) })
closeDatabase() closeDatabase()
return ret return ret, err
} }
func getAllUsersStats() (map[string]int, error) { func getAllUsersStats() (map[string]int, error) {
var ret map[string]int ret := make(map[string]int)
openDatabase() openDatabase()
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
var b, uB, uSB *bolt.Bucket var b, uB, uSB *bolt.Bucket
@ -753,9 +756,12 @@ func getAllUsersStats() (map[string]int, error) {
// Bucket // Bucket
if uB, err = b.CreateBucketIfNotExists([]byte(k)); err == nil { if uB, err = b.CreateBucketIfNotExists([]byte(k)); err == nil {
if uSB, err = uB.CreateBucketIfNotExists([]byte("stats")); err == nil { if uSB, err = uB.CreateBucketIfNotExists([]byte("stats")); err == nil {
var st int
var key string
uSB.ForEach(func(k, v []byte) error { uSB.ForEach(func(k, v []byte) error {
if st, err = bktGetInt(uSB, k); err == nil { key = string(k)
ret[k] = ret[k] + st if st, err = bktGetInt(uSB, key); err == nil {
ret[key] = ret[key] + st
return nil return nil
} }
return err return err
@ -763,6 +769,7 @@ func getAllUsersStats() (map[string]int, error) {
} }
} }
} }
return err
}) })
return err return err
}) })

View File

@ -85,12 +85,18 @@ func handleStats(w http.ResponseWriter, req *http.Request) {
initRequest(w, req) initRequest(w, req)
type StatData struct { type StatData struct {
Stats map[string]int TotalChannelMessages int
Error string
} }
// Get the global stats // Get the global stats
stats := getAllUsersStats() var s StatData
s := StatData{Stats: stats} if stats, err := getAllUsersStats(); err == nil {
s = StatData{TotalChannelMessages: stats["channel-message"], Error: ""}
} else {
s = StatData{TotalChannelMessages: -1, Error: fmt.Sprintf("%s", err)}
}
site.TemplateData = s
setMenuItemActive("Stats") setMenuItemActive("Stats")
showPage("stats.html", site, w) showPage("stats.html", site, w)

View File

@ -1,10 +1,21 @@
<div> <div>
devICT Slack Statistics! devICT Slack!
</div> </div>
<div> <div>
<table> {{ .TemplateData.Error }}
{{ for $k, $v := range .TemplateData.Stats }} </div>
<tr><td>{{ $k }}</td><td>{{ $v }}</td></tr> <div>
{{ end }} <table class="pure-table">
<thead>
<tr>
<th colspan="2">Statistics</th>
</tr>
</thead>
<tbody>
<tr>
<td>Total Channel Messages</td>
<td>{{ .TemplateData.TotalChannelMessages }}</td>
</tr>
</tbody>
</table> </table>
</div> </div>