From 03b64f2c237fd2afa9148949b016a043a7550593 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Thu, 24 May 2018 14:29:29 +0200 Subject: [PATCH] Add more statistics --- main.go | 68 ++++++++++++++++++++++++++++++++++++--- templates/statistics.html | 64 ++++++++++++++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 24210fa..ba20c7b 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "os" "path" "strings" + "time" ) type Config struct { @@ -49,12 +50,33 @@ type ChannelData struct { TotalWords int TotalCharacters int Users []UserData + Questions []PercentageEntry + Exclamations []PercentageEntry + Caps []PercentageEntry + EmojiHappy []PercentageEntry + EmojiSad []PercentageEntry + LongestLines []TotalEntry + ShortestLines []TotalEntry + Total []TotalEntry + Average TotalEntry + ChannelAverage TotalEntry } -type UserData struct { +type PercentageEntry struct { + Name string + Value float64 +} + +type TotalEntry struct { Name string - Total int - Words int + Value int +} + +type UserData struct { + Name string + Total int + Words int + LastSeen time.Time } func main() { @@ -80,19 +102,38 @@ func main() { println(err.Error()) return } - result, err := db.Query("SELECT coalesce(users.nick, '[Unknown]'), t.characters, t.words FROM (SELECT coalesce(groups.\"group\", messages.sender) AS hash, SUM(messages.characters) as characters, SUM(messages.words) as words FROM messages LEFT JOIN groups ON messages.sender = groups.nick AND groups.channel = 1 WHERE messages.channel = 1 GROUP BY hash ORDER BY characters DESC) t LEFT JOIN users ON t.hash = users.hash LIMIT 20") + result, err := db.Query("SELECT coalesce(users.nick, '[Unknown]'), t.characters, t.words, t.lastSeen FROM (SELECT coalesce(groups.\"group\", messages.sender) AS hash, SUM(messages.characters) as characters, SUM(messages.words) as words, MAX(messages.time) AS lastSeen FROM messages LEFT JOIN groups ON messages.sender = groups.nick AND groups.channel = 1 WHERE messages.channel = 1 GROUP BY hash ORDER BY characters DESC) t LEFT JOIN users ON t.hash = users.hash LIMIT 20") if err != nil { println(err.Error()) return } for result.Next() { var info UserData - err := result.Scan(&info.Name, &info.Total, &info.Words) + err := result.Scan(&info.Name, &info.Total, &info.Words, &info.LastSeen) if err != nil { panic(err) } channelData.Users = append(channelData.Users, info) } + + channelData.Questions, err = retrievePercentageStats(db, "question") + if err != nil { + println(err.Error()) + return + } + + channelData.Exclamations, err = retrievePercentageStats(db, "exclamation") + if err != nil { + println(err.Error()) + return + } + + channelData.Caps, err = retrievePercentageStats(db, "caps") + if err != nil { + println(err.Error()) + return + } + err = formatTemplate(w, "statistics", channelData) if err != nil { println(err.Error()) @@ -114,3 +155,20 @@ func main() { panic(err) } } + +func retrievePercentageStats(db *sql.DB, stats string) ([]PercentageEntry, error) { + var data []PercentageEntry + result, err := db.Query("SELECT coalesce(users.nick, '[Unknown]'), t." + stats + " FROM (SELECT coalesce(groups.\"group\", messages.sender) AS hash, round((count(nullif(messages." + stats + ", false)) * 100) :: numeric / count(*)) as " + stats + " FROM messages LEFT JOIN groups ON messages.sender = groups.nick AND groups.channel = 1 WHERE messages.channel = 1 GROUP BY hash ORDER BY " + stats + " DESC) t LEFT JOIN users ON t.hash = users.hash LIMIT 2;") + if err != nil { + return nil, err + } + for result.Next() { + var info PercentageEntry + err := result.Scan(&info.Name, &info.Value) + if err != nil { + panic(err) + } + data = append(data, info) + } + return data, nil +} diff --git a/templates/statistics.html b/templates/statistics.html index 530fb11..ffc84d4 100644 --- a/templates/statistics.html +++ b/templates/statistics.html @@ -23,11 +23,71 @@ <p>Total: {{.TotalCharacters}} Characters, {{.TotalWords}} Words</p> <table> -{{range .Users}} + <thead> + <tr> + <th colspan="3">Most active nicks</th> + </tr> + <tr> + <th>Nick</th> + <th>Number of lines</th> + <th>Number of Words</th> + <th>Last seen</th> + </tr> + </thead> + <tbody> + {{range .Users}} <tr> <td>{{.Name}}</td> <td>{{.Total}}</td> <td>{{.Words}}</td> + <td>{{.LastSeen.Format "2006-01-02"}}</td> + </tr> + {{end}} + </tbody> +</table> + +<table> + <thead> + <tr> + <th>Big numbers</th> + </tr> + </thead> + <tbody> + {{with index .Questions 0}} + <tr> + <td>Is <b>{{.Name}}</b> stupid or just asking too many questions? {{.Value}}% lines contained a question!</td> + </tr> + {{end}} + {{with index .Questions 1}} + <tr> + <td><b>{{.Name}}</b> didn't know that much either. {{.Value}}% of his/her lines were questions.</td> + </tr> + {{end}} + </tbody> + <tbody> + {{with index .Exclamations 0}} + <tr> + <td>The loudest one was <b>{{.Name}}</b>, who yelled {{.Value}}% of the time!</td> + </tr> + {{end}} + {{with index .Exclamations 1}} + <tr> + <td>Another <i>old yeller</i> was <b>{{.Name}}</b>, who shouted {{.Value}}% of the time!</td> + </tr> + {{end}} + </tbody> + <tbody> + {{with index .Caps 0}} + <tr> + <td>It seems that <b>{{.Name}}</b>'s shift-key is hanging: {{.Value}}of the time he/she wrote UPPERCASE.</td> + </tr> + {{end}} + {{with index .Caps 1}} + <tr> + <td><b>{{.Name}}</b> just forgot to deactivate his/her Caps-Lock. He/She wrote UPPERCASE {{.Value}}% of the + time. + </td> </tr> -{{end}} + {{end}} + </tbody> </table> \ No newline at end of file -- GitLab