From 4b1c625fcdec357ad27bf572559556c11170f654 Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Thu, 24 May 2018 21:00:25 +0200
Subject: [PATCH] More stats

---
 .gitignore                |  3 +--
 Readme.md                 |  1 +
 assets/sass/style.sass    | 15 +++++++++++---
 main.go                   | 38 +++++++++++++++++++++++++++++++---
 templates/statistics.html | 43 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 92 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore
index 4be66ce..f8570a4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,3 @@
 /.idea/
 /vendor/
-/node_modules/
-/assets/css/
\ No newline at end of file
+/node_modules/
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
index 15d6bb9..e14784c 100644
--- a/Readme.md
+++ b/Readme.md
@@ -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                                         |
diff --git a/assets/sass/style.sass b/assets/sass/style.sass
index 26ff1a0..3c3ffec 100644
--- a/assets/sass/style.sass
+++ b/assets/sass/style.sass
@@ -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
@@ -27,4 +29,11 @@ table
       tr:not(:first-child)
         font-size: 10px
       &:nth-child(2n)
-        background: rgba(52, 68, 84, 0.1)
\ No newline at end of file
+        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
diff --git a/main.go b/main.go
index 3e3e28e..14dafbe 100644
--- a/main.go
+++ b/main.go
@@ -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 {
diff --git a/templates/statistics.html b/templates/statistics.html
index ce1c33f..8ffe557 100644
--- a/templates/statistics.html
+++ b/templates/statistics.html
@@ -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>
-- 
GitLab