-
Notifications
You must be signed in to change notification settings - Fork 938
/
Copy pathbackgroundworker.go
132 lines (110 loc) · 3.02 KB
/
backgroundworker.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
package analytics
import (
"strconv"
"strings"
"sync"
"time"
"emperror.dev/errors"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/backgroundworkers"
"github.com/mediocregopher/radix/v3"
)
var _ backgroundworkers.BackgroundWorkerPlugin = (*Plugin)(nil)
func (p *Plugin) RunBackgroundWorker() {
ticker := time.NewTicker(time.Minute * 60)
for {
select {
case <-ticker.C:
logger.Info("Performing saving of temp analytics")
started := time.Now()
err := p.saveTempStats()
if err != nil {
logger.WithError(err).Error("failed saving temp analytics")
}
logger.Infof("Took %s to save analytics", time.Since(started))
case wg := <-p.stopWorkers:
wg.Done()
return
}
p.RunBackgroundWorker()
}
}
func (p *Plugin) StopBackgroundWorker(wg *sync.WaitGroup) {
p.stopWorkers <- wg
}
type ActivityBucket struct {
GuildID int64
Count int
Plugin string
Name string
}
func (p *Plugin) saveTempStats() error {
compiled := make(map[string][]*ActivityBucket)
err := common.RedisPool.Do(radix.WithConn("", func(c radix.Conn) error {
s := radix.NewScanner(c, radix.ScanOpts{
Command: "SCAN",
Pattern: "anaylytics_active_units.*",
})
var key string
for s.Next(&key) {
// copy it to a safe location first
err := c.Do(radix.Cmd(nil, "RENAME", key, "temp_"+key))
if err != nil {
return errors.WithStackIf(err)
}
// read raw counts
var rawCounts map[string]string
err = c.Do(radix.Cmd(&rawCounts, "HGETALL", "temp_"+key))
if err != nil {
return errors.WithStackIf(err)
}
// get plugin and metric name from key
split := strings.Split(key, ".")
if len(split) < 3 {
return errors.New("Incorrect length on analytic name")
}
plugin := split[1]
metricName := split[2]
bucket := compiled[plugin+"."+metricName]
OUTER:
for g, countStr := range rawCounts {
parsedG, _ := strconv.ParseInt(g, 10, 64)
parsedCount, _ := strconv.Atoi(countStr)
for _, compiledCount := range bucket {
if compiledCount.GuildID == parsedG {
compiledCount.Count += parsedCount
continue OUTER
}
}
// did not find the entry for this guild, create a new one
bucket = append(bucket, &ActivityBucket{
GuildID: parsedG,
Plugin: plugin,
Name: metricName,
Count: parsedCount,
})
}
compiled[plugin+"."+metricName] = bucket
// clear it
err = c.Do(radix.Cmd(nil, "DEL", "temp_"+key))
if err != nil {
return errors.WithStackIf(err)
}
}
return nil
}))
if err != nil {
return errors.WithStackIf(err)
}
const q = `INSERT INTO analytics (guild_id, created_at, plugin, name, count)
VALUES ($1, now(), $2, $3, $4)`
for _, bucket := range compiled {
for _, entry := range bucket {
_, err := common.PQ.Exec(q, entry.GuildID, entry.Plugin, entry.Name, entry.Count)
if err != nil {
return errors.WithStackIf(err)
}
}
}
return nil
}