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 {
if err == nil {
if usr.IsBot {
stats.ProcessBotMessage(m)
} else {
stats.ProcessMessage(m)
}
if usr.IsBot {
stats.ProcessBotMessage(m)
} else {
stats.ProcessMessage(m)
}
}
for _, proc := range messageProcessors {

View File

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

View File

@ -85,12 +85,18 @@ func handleStats(w http.ResponseWriter, req *http.Request) {
initRequest(w, req)
type StatData struct {
Stats map[string]int
TotalChannelMessages int
Error string
}
// Get the global stats
stats := getAllUsersStats()
s := StatData{Stats: stats}
var s StatData
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")
showPage("stats.html", site, w)

View File

@ -1,10 +1,21 @@
<div>
devICT Slack Statistics!
devICT Slack!
</div>
<div>
<table>
{{ for $k, $v := range .TemplateData.Stats }}
<tr><td>{{ $k }}</td><td>{{ $v }}</td></tr>
{{ end }}
{{ .TemplateData.Error }}
</div>
<div>
<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>
</div>