-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
101 lines (83 loc) · 1.9 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"net/http"
"sort"
"time"
)
type Rooms = map[string]*RoomStats
type ListOfRooms = []*RoomStats
type Data struct {
Connections int `json:"connections"`
Users int `json:"users"`
Users1min int `json:"users_1min"`
Users5min int `json:"users_5min"`
Users15min int `json:"users_15min"`
Rooms ListOfRooms `json:"rooms"`
Version string `json:"version"`
BannedUsers int `json:"banned_users"`
UptimeFrom string `json:"uptime_from"`
}
type RoomStats struct {
Name string `json:"name"`
Online int64 `json:"online"`
Writers int64 `json:"writers"`
Messages int64 `json:"messages"`
}
func getStats(hub *Hub) *Data {
now := time.Now().Unix()
min1time := now - 60
min5time := now - 60*5
min15time := now - 60*15
min1 := 0
min5 := 0
min15 := 0
uniq := make(map[string]*Client)
for client := range hub.clients {
uniq[client.id] = client
}
rooms := make(Rooms)
for _, c := range uniq {
if c.time > min1time {
min1++
}
if c.time > min5time {
min5++
}
if c.time > min15time {
min15++
}
for _, room := range c.rooms {
if _, ok := rooms[room]; ok {
rooms[room].Online++
} else {
r := logGetStats(room)
rooms[room] = &RoomStats{room, 1, r.writets, r.messages}
}
}
}
listOfRooms := []*RoomStats{}
for _, r := range rooms {
listOfRooms = append(listOfRooms, r)
}
sort.Slice(listOfRooms, func(i, j int) bool {
return listOfRooms[i].Online > listOfRooms[j].Online
})
return &Data{
len(hub.clients),
len(uniq),
min1,
min5,
min15,
listOfRooms,
version,
ipLimit.BannedCount() + idLimit.BannedCount(),
uptime.Format("2006-01-02 15:04:05"),
}
}
func stats(hub *Hub, w http.ResponseWriter) {
s := getStats(hub)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(s)
}