-
Notifications
You must be signed in to change notification settings - Fork 940
/
Copy pathstats.go
175 lines (140 loc) · 4.28 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package serverstats
import (
"context"
"strconv"
"time"
"emperror.dev/errors"
"github.com/jonas747/yagpdb/bot/botrest"
"github.com/jonas747/yagpdb/common"
)
type ChannelStats struct {
Name string `json:"name"`
Count int64 `json:"count"`
}
type DailyStats struct {
ChannelMessages map[string]*ChannelStats `json:"channels_messages"`
JoinedDay int `json:"joined_day"`
LeftDay int `json:"left_day"`
Online int `json:"online_now"`
TotalMembers int `json:"total_members_now"`
}
func RetrieveDailyStats(t time.Time, guildID int64) (*DailyStats, error) {
msgStats, err := readHourlyMessageStats(t, guildID)
if err != nil {
return nil, errors.WithStackIf(err)
}
miscStats, err := readHourlyMiscStats(t, guildID)
if err != nil {
return nil, errors.WithStackIf(err)
}
online, err := botrest.GetOnlineCount(guildID)
if err != nil {
logger.WithError(err).Error("Failed fetching online count")
}
miscStats.Online = int(online)
miscStats.ChannelMessages = msgStats
return miscStats, nil
}
func readHourlyMessageStats(t time.Time, guildID int64) (map[string]*ChannelStats, error) {
// read horly message stats
const qMsgStatsHourly = `SELECT t, channel_id, count
FROM server_stats_hourly_periods_messages
WHERE t > ($2::timestamptz - INTERVAL '25 hours') AND guild_id = $1 AND date_trunc('hour', t) < date_trunc('hour', $2)`
rows, err := common.PQ.Query(qMsgStatsHourly, guildID, t)
if err != nil {
return nil, errors.WithStackIf(err)
}
defer rows.Close()
channelStats := make(map[string]*ChannelStats)
for rows.Next() {
var channelID int64
var count int64
var t time.Time
err = rows.Scan(&t, &channelID, &count)
if err != nil {
return nil, errors.WithStackIf(err)
}
cStr := strconv.FormatInt(channelID, 10)
if cs, ok := channelStats[cStr]; ok {
cs.Count += count
} else {
channelStats[cStr] = &ChannelStats{
Name: cStr,
Count: count,
}
}
}
return channelStats, nil
}
func readHourlyMiscStats(t time.Time, guildID int64) (*DailyStats, error) {
// read the rest
const qMiscStatsHourly = `SELECT t, num_members, max_online, joins, leaves, max_voice
FROM server_stats_hourly_periods_misc
WHERE t > ($2::timestamptz - INTERVAL '25 hours') AND guild_id = $1 AND date_trunc('hour', t) < date_trunc('hour', $2)`
rows, err := common.PQ.Query(qMiscStatsHourly, guildID, t)
if err != nil {
return nil, errors.WithStackIf(err)
}
defer rows.Close()
sum := &DailyStats{}
for rows.Next() {
var t time.Time
var numMembers int
var maxOnline int
var joins, leaves int
var maxVoice int
err = rows.Scan(&t, &numMembers, &maxOnline, &joins, &leaves, &maxVoice)
if err != nil {
return nil, errors.WithStackIf(err)
}
sum.JoinedDay += joins
sum.LeftDay += leaves
if maxOnline > sum.Online {
sum.Online = maxOnline
}
if numMembers > sum.TotalMembers {
sum.TotalMembers = numMembers
}
}
return sum, nil
}
type ChartDataPeriod struct {
T time.Time `json:"t"`
Joins int `json:"joins"`
Leaves int `json:"leaves"`
NumMembers int `json:"num_members"`
MaxOnline int `json:"max_online"`
Messages int `json:"num_messages"`
}
func RetrieveChartDataPeriods(ctx context.Context, guildID int64, t time.Time, days int) ([]*ChartDataPeriod, error) {
const q = `SELECT t, num_messages, num_members, max_online, joins, leaves, max_voice
FROM server_stats_periods_compressed
WHERE t > $2 AND guild_id = $1 and t < $3
ORDER BY t DESC;`
if days <= 0 {
days = 1000
}
rows, err := common.PQ.QueryContext(ctx, q, guildID, t.Add(time.Hour*-24*time.Duration(days+1)), t.Truncate(time.Hour*24))
if err != nil {
return nil, errors.WithStackIf(err)
}
defer rows.Close()
periods := make([]*ChartDataPeriod, 0, days)
for rows.Next() {
var t time.Time
var numMessages, numMembers, maxOnline, joins, leaves, maxVoice int
err = rows.Scan(&t, &numMessages, &numMembers, &maxOnline, &joins, &leaves, &maxVoice)
if err != nil {
return nil, errors.WithStackIf(err)
}
periods = append(periods, &ChartDataPeriod{
T: t,
Joins: joins,
Leaves: leaves,
NumMembers: numMembers,
MaxOnline: maxOnline,
Messages: numMessages,
})
}
return periods, nil
}