forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventlogger.go
98 lines (74 loc) · 1.93 KB
/
eventlogger.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
package bot
import (
"github.com/jonas747/yagpdb/bot/eventsystem"
"sync"
"sync/atomic"
"time"
)
const EventLoggerPeriodDuration = time.Second * 10
var (
EventLogger = &eventLogger{}
)
type eventLogger struct {
sync.Mutex
totalStats [][]*int64
lastPeriod [][]int64
perPeriod [][]int64
numShards int
}
func (e *eventLogger) init(numShards int) {
e.totalStats = make([][]*int64, numShards)
e.lastPeriod = make([][]int64, numShards)
e.perPeriod = make([][]int64, numShards)
// Initialize these
for i, _ := range e.totalStats {
e.totalStats[i] = make([]*int64, len(eventsystem.AllEvents))
e.lastPeriod[i] = make([]int64, len(eventsystem.AllEvents))
e.perPeriod[i] = make([]int64, len(eventsystem.AllEvents))
for j, _ := range e.totalStats[i] {
e.totalStats[i][j] = new(int64)
}
}
}
func (e *eventLogger) run() {
ticker := time.NewTicker(EventLoggerPeriodDuration)
for {
<-ticker.C
e.flushStats()
}
}
func (e *eventLogger) GetStats() (total [][]int64, perPeriod [][]int64) {
e.Lock()
total = make([][]int64, len(e.totalStats))
perPeriod = make([][]int64, len(e.totalStats))
for i := 0; i < len(e.totalStats); i++ {
perPeriod[i] = make([]int64, len(e.totalStats[i]))
total[i] = make([]int64, len(e.totalStats[i]))
for j := 0; j < len(e.totalStats[i]); j++ {
perPeriod[i][j] = e.perPeriod[i][j]
total[i][j] = atomic.LoadInt64(e.totalStats[i][j])
}
}
e.Unlock()
return
}
func (e *eventLogger) flushStats() {
e.Lock()
for i := 0; i < len(e.totalStats); i++ {
for j := 0; j < len(e.totalStats[i]); j++ {
currentVal := atomic.LoadInt64(e.totalStats[i][j])
e.perPeriod[i][j] = currentVal - e.lastPeriod[i][j]
e.lastPeriod[i][j] = currentVal
}
}
e.Unlock()
}
func (e *eventLogger) handleEvent(evt *eventsystem.EventData) {
if evt.Session == nil {
return
}
if int(evt.Type) >= len(eventsystem.AllEvents) {
return
}
atomic.AddInt64(e.totalStats[evt.Session.ShardID][evt.Type], 1)
}