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
Branches
No related tags found
No related merge requests found
/.idea/
/vendor/
/node_modules/
\ No newline at end of file
/assets/css/
\ No newline at end of file
......@@ -6,5 +6,6 @@
| ------------------------ | ---------------------------------------------- | ------------------------------------------------------ |
|`KSTATS_DATABASE_TYPE`* |`postgres` | Database driver (only postgres is supported currently) |
|`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_PASSWORD` |`hunter2` | Redis Password |
......@@ -11,9 +11,11 @@ table
width: 100%
border-collapse: collapse
margin: 1rem 0
th
background: rgba(52, 68, 84, 1.0)
tr th
background: rgba(52, 68, 84, 0.7)
color: #ffffff
thead tr:first-child th
background: rgba(52, 68, 84, 1.0)
tr
background: rgba(52, 68, 84, 0.2)
font-size: 13px
......@@ -28,3 +30,10 @@ table
font-size: 10px
&:nth-child(2n)
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
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"gopkg.in/redis.v4"
"html/template"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/go-redis/redis"
"encoding/json"
)
const DEBUG = false
......@@ -64,6 +64,7 @@ type ChannelData struct {
Words int
WordsPerLine float64
CharactersPerLine float64
HourUsage []float64
Users []UserData
Questions []FloatEntry
Exclamations []FloatEntry
......@@ -118,7 +119,8 @@ func handleError(err error) {
func main() {
config := NewConfigFromEnv()
redisClient := redis.NewClient(&redis.Options{
var redisClient *redis.Client
redisClient = redis.NewClient(&redis.Options{
Addr: config.Redis.Address,
Password: config.Redis.Password,
})
......@@ -181,6 +183,11 @@ func buildChannelData(db *sql.DB, channel string) (channelData ChannelData, err
return
}
channelData.HourUsage, err = retrieveHourUsage(db, channelData.Id)
if err != nil {
return
}
channelData.Users, err = retrieveUsers(db, channelData.Id)
if err != nil {
return
......@@ -256,6 +263,31 @@ func retrievePercentageStats(db *sql.DB, channel int, stats string) ([]FloatEntr
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) {
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 {
......
......@@ -21,6 +21,49 @@
<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>
<thead>
<tr>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment