forked from botlabs-gg/yagpdb
/
serverstats.go
438 lines (354 loc) · 10.5 KB
/
serverstats.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package serverstats
//go:generate sqlboiler --no-hooks psql
import (
"context"
"database/sql"
"strconv"
"strings"
"sync"
"time"
"emperror.dev/errors"
"github.com/jonas747/discordgo"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/bot/botrest"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/serverstats/models"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/queries/qm"
)
type Plugin struct {
stopStatsLoop chan *sync.WaitGroup
}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Server Stats",
SysName: "server_stats",
Category: common.PluginCategoryMisc,
}
}
var logger = common.GetPluginLogger(&Plugin{})
func RegisterPlugin() {
common.InitSchemas("serverstats", DBSchemas...)
plugin := &Plugin{
stopStatsLoop: make(chan *sync.WaitGroup),
}
common.RegisterPlugin(plugin)
}
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(guildID int64) (*DailyStats, error) {
// Query the short term stats and the long term stats
// TODO: If we start moving them over in between we will get somehwat incorrect stats
// not sure how to fix other than locking
stats, err := RetrieveRedisStats(guildID)
if err != nil {
return nil, err
}
// rows, err := ServerStatsPeriodStore.FindAll(models.NewStatsPeriodQuery().FindByGuildID(
// kallax.Eq, guildID).FindByStarted(kallax.Gt))
messageStatsRows, err := models.ServerStatsPeriods(qm.Where("guild_id = ?", guildID), qm.Where("started > ?", time.Now().Add(time.Hour*-24))).AllG(context.Background())
if err != nil {
return nil, err
}
// Merge the stats togheter
for _, period := range messageStatsRows {
stringedChannel := strconv.FormatInt(period.ChannelID.Int64, 10)
if st, ok := stats.ChannelMessages[stringedChannel]; ok {
st.Count += period.Count.Int64
} else {
stats.ChannelMessages[stringedChannel] = &ChannelStats{
Name: stringedChannel,
Count: period.Count.Int64,
}
}
}
t := RoundHour(time.Now())
memberStatsRows, err := models.ServerStatsMemberPeriods(
models.ServerStatsMemberPeriodWhere.GuildID.EQ(guildID),
qm.OrderBy("id desc"), qm.Limit(25)).AllG(context.Background())
// Sum the stats
for i, v := range memberStatsRows {
if i == 0 {
stats.TotalMembers = int(v.NumMembers)
if v.CreatedAt.UTC() == t.UTC() {
continue
}
}
if t.Sub(v.CreatedAt) > time.Hour*25 {
break
}
stats.JoinedDay += int(v.Joins)
stats.LeftDay += int(v.Leaves)
}
return stats, nil
}
func RetrieveRedisStats(guildID int64) (*DailyStats, error) {
now := time.Now()
yesterday := now.Add(time.Hour * -24)
unixYesterday := discordgo.StrID(yesterday.Unix())
var messageStatsRaw []string
err := common.RedisPool.Do(retryableredis.Cmd(&messageStatsRaw, "ZRANGEBYSCORE", RedisKeyChannelMessages(guildID), unixYesterday, "+inf"))
if err != nil {
return nil, err
}
online, err := botrest.GetOnlineCount(guildID)
if err != nil {
if botrest.BotIsRunning() {
logger.WithError(err).Error("Failed fetching online count")
}
}
channelResult, err := parseMessageStats(messageStatsRaw, guildID)
if err != nil {
return nil, err
}
stats := &DailyStats{
ChannelMessages: channelResult,
Online: int(online),
}
return stats, nil
}
func parseMessageStats(raw []string, guildID int64) (map[string]*ChannelStats, error) {
channelResult := make(map[string]*ChannelStats)
for _, result := range raw {
split := strings.Split(result, ":")
if len(split) < 2 {
logger.WithFields(logrus.Fields{
"guild": guildID,
"result": result,
}).Error("Invalid message stats")
continue
}
channelID := split[0]
stats, ok := channelResult[channelID]
if ok {
stats.Count++
} else {
name := channelID
channelResult[channelID] = &ChannelStats{
Name: name,
Count: 1,
}
}
}
return channelResult, nil
}
// ServerStatsConfig represents a configuration for a server
// reason we dont reference the model directly is because i need to figure out a way to
// migrate them over to the new schema, painlessly.
type ServerStatsConfig struct {
Public bool
IgnoreChannels string
ParsedChannels []int64
}
func (s *ServerStatsConfig) ParseChannels() {
split := strings.Split(s.IgnoreChannels, ",")
for _, v := range split {
parsed, err := strconv.ParseInt(v, 10, 64)
if err == nil {
s.ParsedChannels = append(s.ParsedChannels, parsed)
}
}
}
func configFromModel(model *models.ServerStatsConfig) *ServerStatsConfig {
conf := &ServerStatsConfig{
Public: model.Public.Bool,
IgnoreChannels: model.IgnoreChannels.String,
}
conf.ParseChannels()
return conf
}
func GetConfig(ctx context.Context, GuildID int64) (*ServerStatsConfig, error) {
if ctx == nil {
ctx = context.Background()
}
conf, err := models.FindServerStatsConfigG(ctx, GuildID)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
if conf == nil {
return &ServerStatsConfig{}, nil
}
return configFromModel(conf), nil
}
const (
RedisKeyGuildMembersChanged = "servers_stats_guild_members_changed"
)
func RedisKeyChannelMessages(guildID int64) string {
return "guild_stats_msg_channel_day:" + strconv.FormatInt(guildID, 10)
}
// RoundHour rounds a time.Time down to the hour
func RoundHour(t time.Time) time.Time {
t = t.UTC()
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
}
type MemberChartDataPeriod struct {
T time.Time `json:"t"`
Joins int `json:"joins"`
Leaves int `json:"leaves"`
NumMembers int `json:"num_members"`
MaxOnline int `json:"max_online"`
}
func RetrieveMemberChartStats(guildID int64, days int) ([]*MemberChartDataPeriod, error) {
query := `select date_trunc('day', created_at), sum(joins), sum(leaves), max(num_members), max(max_online)
FROM server_stats_member_periods
WHERE guild_id=$1
GROUP BY 1
ORDER BY 1 DESC`
args := []interface{}{guildID}
if days > 0 {
query += " LIMIT $2;"
args = append(args, days)
}
rows, err := common.PQ.Query(query, args...)
if err != nil {
return nil, errors.WrapIf(err, "pq.query")
}
defer rows.Close()
var results []*MemberChartDataPeriod
if days > 0 {
results = make([]*MemberChartDataPeriod, days)
} else {
// we don't know the size
results = make([]*MemberChartDataPeriod, 100)
}
for rows.Next() {
var t time.Time
var joins int
var leaves int
var numMembers int
var maxOnline int
err := rows.Scan(&t, &joins, &leaves, &numMembers, &maxOnline)
if err != nil {
return nil, errors.WrapIf(err, "rows.scan")
}
daysOld := int(time.Since(t).Hours() / 24)
if daysOld > days && len(results) > 0 && days > 0 {
// only grab results within time period specified (but always grab 1 even if outside our range)
break
}
if days > 0 && daysOld >= days {
// clamp to last if we specified a time
daysOld = days - 1
}
if daysOld >= len(results) {
if daysOld > 10000 {
continue // ignore this then, should never happen, but lets just avoid running out of memory if it does
}
newResults := make([]*MemberChartDataPeriod, daysOld*2)
copy(newResults, results)
results = newResults
}
results[daysOld] = &MemberChartDataPeriod{
T: t,
Joins: joins,
Leaves: leaves,
NumMembers: numMembers,
MaxOnline: maxOnline,
}
}
firstNonNullResult := -1
// fill in the blank days
var lastProperResult MemberChartDataPeriod
for i := len(results) - 1; i >= 0; i-- {
if results[i] == nil && !lastProperResult.T.IsZero() {
cop := lastProperResult
t := time.Now().Add(time.Hour * 24 * -time.Duration(i))
cop.T = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, lastProperResult.T.Location())
results[i] = &cop
} else if results[i] != nil {
lastProperResult = *results[i]
lastProperResult.Joins = 0
lastProperResult.Leaves = 0
if firstNonNullResult == -1 {
firstNonNullResult = i
}
}
}
// cut out nil results
results = results[:firstNonNullResult+1]
return results, nil
}
type MessageChartDataPeriod struct {
T time.Time `json:"t"`
MessageCount int `json:"message_count"`
}
func RetrieveMessageChartData(guildID int64, days int) ([]*MessageChartDataPeriod, error) {
queryPre := `select date_trunc('day', started), sum(count)
FROM server_stats_periods
WHERE guild_id=$1 `
queryPost := `
GROUP BY 1
ORDER BY 1 DESC`
args := []interface{}{guildID}
if days > 0 {
queryPre += " AND started > $2"
args = append(args, time.Now().Add(time.Hour*24*time.Duration(-days)))
}
rows, err := common.PQ.Query(queryPre+queryPost, args...)
if err != nil {
return nil, errors.WrapIf(err, "pq.query")
}
defer rows.Close()
var results []*MessageChartDataPeriod
if days > 0 {
results = make([]*MessageChartDataPeriod, days)
} else {
// we don't know the size
results = make([]*MessageChartDataPeriod, 100)
}
for rows.Next() {
var t time.Time
var count int
err := rows.Scan(&t, &count)
if err != nil {
return nil, errors.WrapIf(err, "rows.scan")
}
daysOld := int(time.Since(t).Hours() / 24)
if daysOld >= days && days > 0 {
// clamp to last if we specified a time
daysOld = days - 1
}
if daysOld >= len(results) {
// we don't know the size so we have to dynamically adjust
if daysOld > 10000 {
continue // ignore this then, should never happen, but lets just avoid running out of memory if it does
}
newResults := make([]*MessageChartDataPeriod, daysOld*2)
copy(newResults, results)
results = newResults
}
results[daysOld] = &MessageChartDataPeriod{
T: t,
MessageCount: count,
}
}
firstNonNullResult := -1
// fill in the blank days
var lastProperResult MessageChartDataPeriod
for i := len(results) - 1; i >= 0; i-- {
if results[i] == nil && !lastProperResult.T.IsZero() {
cop := lastProperResult
t := time.Now().Add(time.Hour * 24 * -time.Duration(i))
cop.T = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, lastProperResult.T.Location())
results[i] = &cop
} else if results[i] != nil {
lastProperResult = *results[i]
lastProperResult.MessageCount = 0
if firstNonNullResult == -1 {
firstNonNullResult = i
}
}
}
// cut out nil results
results = results[:firstNonNullResult+1]
return results, nil
}