Skip to content
Snippets Groups Projects
Verified Commit 03b64f2c authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Add more statistics

parent f4bf74ec
No related branches found
No related tags found
No related merge requests found
......@@ -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 PercentageEntry struct {
Name string
Value float64
}
type TotalEntry struct {
Name string
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
}
......@@ -23,11 +23,71 @@
<p>Total: {{.TotalCharacters}} Characters, {{.TotalWords}} Words</p>
<table>
<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}}
</tbody>
</table>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment