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

More stats

parent 9b379f4e
No related branches found
No related tags found
No related merge requests found
/.idea/ /.idea/
/vendor/ /vendor/
/node_modules/ /node_modules/
/assets/css/
\ No newline at end of file
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
| ------------------------ | ---------------------------------------------- | ------------------------------------------------------ | | ------------------------ | ---------------------------------------------- | ------------------------------------------------------ |
|`KSTATS_DATABASE_TYPE`* |`postgres` | Database driver (only postgres is supported currently) | |`KSTATS_DATABASE_TYPE`* |`postgres` | Database driver (only postgres is supported currently) |
|`KSTATS_DATABASE_URL`* |`postgresql://kstats:hunter2@localhost/statsbot`| Database URL | |`KSTATS_DATABASE_URL`* |`postgresql://kstats:hunter2@localhost/statsbot`| Database URL |
|`KSTATS_REDIS_ENABLED` |`true` | If Redis should be used as cache |
|`KSTATS_REDIS_ADDRESS`* |`localhost:6379` | Redis Address | |`KSTATS_REDIS_ADDRESS`* |`localhost:6379` | Redis Address |
|`KSTATS_REDIS_PASSWORD` |`hunter2` | Redis Password | |`KSTATS_REDIS_PASSWORD` |`hunter2` | Redis Password |
...@@ -11,9 +11,11 @@ table ...@@ -11,9 +11,11 @@ table
width: 100% width: 100%
border-collapse: collapse border-collapse: collapse
margin: 1rem 0 margin: 1rem 0
th tr th
background: rgba(52, 68, 84, 1.0) background: rgba(52, 68, 84, 0.7)
color: #ffffff color: #ffffff
thead tr:first-child th
background: rgba(52, 68, 84, 1.0)
tr tr
background: rgba(52, 68, 84, 0.2) background: rgba(52, 68, 84, 0.2)
font-size: 13px font-size: 13px
...@@ -28,3 +30,10 @@ table ...@@ -28,3 +30,10 @@ table
font-size: 10px font-size: 10px
&:nth-child(2n) &:nth-child(2n)
background: rgba(52, 68, 84, 0.1) background: rgba(52, 68, 84, 0.1)
&.activity
.chart
td
width: 4.16666666%
div
margin-top: 1rem
background: rgba(52, 68, 84, 0.4)
\ No newline at end of file
...@@ -2,16 +2,16 @@ package main ...@@ -2,16 +2,16 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/json"
"fmt" "fmt"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"gopkg.in/redis.v4"
"html/template" "html/template"
"net/http" "net/http"
"os" "os"
"path" "path"
"strings" "strings"
"time" "time"
"github.com/go-redis/redis"
"encoding/json"
) )
const DEBUG = false const DEBUG = false
...@@ -64,6 +64,7 @@ type ChannelData struct { ...@@ -64,6 +64,7 @@ type ChannelData struct {
Words int Words int
WordsPerLine float64 WordsPerLine float64
CharactersPerLine float64 CharactersPerLine float64
HourUsage []float64
Users []UserData Users []UserData
Questions []FloatEntry Questions []FloatEntry
Exclamations []FloatEntry Exclamations []FloatEntry
...@@ -118,7 +119,8 @@ func handleError(err error) { ...@@ -118,7 +119,8 @@ func handleError(err error) {
func main() { func main() {
config := NewConfigFromEnv() config := NewConfigFromEnv()
redisClient := redis.NewClient(&redis.Options{ var redisClient *redis.Client
redisClient = redis.NewClient(&redis.Options{
Addr: config.Redis.Address, Addr: config.Redis.Address,
Password: config.Redis.Password, Password: config.Redis.Password,
}) })
...@@ -181,6 +183,11 @@ func buildChannelData(db *sql.DB, channel string) (channelData ChannelData, err ...@@ -181,6 +183,11 @@ func buildChannelData(db *sql.DB, channel string) (channelData ChannelData, err
return return
} }
channelData.HourUsage, err = retrieveHourUsage(db, channelData.Id)
if err != nil {
return
}
channelData.Users, err = retrieveUsers(db, channelData.Id) channelData.Users, err = retrieveUsers(db, channelData.Id)
if err != nil { if err != nil {
return return
...@@ -256,6 +263,31 @@ func retrievePercentageStats(db *sql.DB, channel int, stats string) ([]FloatEntr ...@@ -256,6 +263,31 @@ func retrievePercentageStats(db *sql.DB, channel int, stats string) ([]FloatEntr
return data, nil return data, nil
} }
func retrieveHourUsage(db *sql.DB, channel int) ([]float64, error) {
result, err := db.Query("SELECT coalesce(count, 0) AS count FROM generate_series(0, 23, 1) AS series LEFT OUTER JOIN (SELECT EXTRACT(HOUR FROM time) as hour, count(*) as count FROM messages WHERE channel = $1 GROUP BY hour) results ON (series = results.hour)", channel)
if err != nil {
return nil, err
}
var data []int
max := 0
for result.Next() {
var info int
err := result.Scan(&info)
if err != nil {
panic(err)
}
if info > max {
max = info
}
data = append(data, info)
}
var normalizedResult []float64
for _, element := range data {
normalizedResult = append(normalizedResult, float64(element)/float64(max)*100.0)
}
return normalizedResult, nil
}
func retrieveLongestLines(db *sql.DB, channel int) ([]FloatEntry, error) { func retrieveLongestLines(db *sql.DB, channel int) ([]FloatEntry, error) {
result, err := db.Query("SELECT coalesce(users.nick, '[Unknown]'), t.average FROM (SELECT coalesce(groups.\"group\", messages.sender) AS hash, avg(messages.characters) as average FROM messages LEFT JOIN groups ON messages.sender = groups.nick AND groups.channel = $1 WHERE messages.channel = $1 GROUP BY hash ORDER BY average DESC) t LEFT JOIN users ON t.hash = users.hash LIMIT $2;", channel, 2) result, err := db.Query("SELECT coalesce(users.nick, '[Unknown]'), t.average FROM (SELECT coalesce(groups.\"group\", messages.sender) AS hash, avg(messages.characters) as average FROM messages LEFT JOIN groups ON messages.sender = groups.nick AND groups.channel = $1 WHERE messages.channel = $1 GROUP BY hash ORDER BY average DESC) t LEFT JOIN users ON t.hash = users.hash LIMIT $2;", channel, 2)
if err != nil { if err != nil {
......
...@@ -21,6 +21,49 @@ ...@@ -21,6 +21,49 @@
<link href="/assets/css/style.css" rel="stylesheet"> <link href="/assets/css/style.css" rel="stylesheet">
<table class="activity">
<thead>
<tr>
<th colspan="24">Most active hours</th>
</tr>
</thead>
<tr class="chart">
{{range .HourUsage}}
<td align="center" valign="bottom">
<div style="height: {{.}}px"></div>
</td>
{{end}}
</tr>
<tfoot>
<tr>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
<th>16</th>
<th>17</th>
<th>18</th>
<th>19</th>
<th>20</th>
<th>21</th>
<th>22</th>
<th>23</th>
</tr>
</tfoot>
</table>
<table> <table>
<thead> <thead>
<tr> <tr>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment