-
Notifications
You must be signed in to change notification settings - Fork 915
/
topevents.go
88 lines (71 loc) · 2.06 KB
/
topevents.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
package topevents
import (
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
"sort"
)
var Command = &commands.YAGCommand{
Cooldown: 2,
CmdCategory: commands.CategoryDebug,
Name: "topevents",
Description: "Shows gateway event processing stats for all or one shard",
HideFromHelp: true,
Arguments: []*dcmd.ArgDef{
{Name: "shard", Type: dcmd.Int},
},
RunFunc: cmdFuncTopEvents,
}
func cmdFuncTopEvents(data *dcmd.Data) (interface{}, error) {
shardsTotal, lastPeriod := bot.EventLogger.GetStats()
sortable := make([]*DiscordEvtEntry, len(eventsystem.AllDiscordEvents))
for i, _ := range sortable {
sortable[i] = &DiscordEvtEntry{
Name: eventsystem.AllDiscordEvents[i].String(),
}
}
for i, _ := range shardsTotal {
if data.Args[0].Value != nil && data.Args[0].Int() != i {
continue
}
for de, j := range eventsystem.AllDiscordEvents {
sortable[de].Total += shardsTotal[i][j]
sortable[de].PerSecond += float64(lastPeriod[i][j]) / bot.EventLoggerPeriodDuration.Seconds()
}
}
sort.Sort(DiscordEvtEntrySortable(sortable))
out := "Total event stats across all shards:\n"
if data.Args[0].Value != nil {
out = fmt.Sprintf("Stats for shard %d:\n", data.Args[0].Int())
}
out += "```\n# Total - /s - Event\n"
sum := int64(0)
sumPerSecond := float64(0)
for k, entry := range sortable {
out += fmt.Sprintf("#%-2d: %7d - %5.1f - %s\n", k+1, entry.Total, entry.PerSecond, entry.Name)
sum += entry.Total
sumPerSecond += entry.PerSecond
}
out += fmt.Sprintf("\nTotal: %d, Events per second: %.1f", sum, sumPerSecond)
out += "\n```"
return out, nil
}
type DiscordEvtEntry struct {
Name string
Total int64
PerSecond float64
}
type DiscordEvtEntrySortable []*DiscordEvtEntry
func (d DiscordEvtEntrySortable) Len() int {
return len(d)
}
func (d DiscordEvtEntrySortable) Less(i, j int) bool {
return d[i].Total > d[j].Total
}
func (d DiscordEvtEntrySortable) Swap(i, j int) {
temp := d[i]
d[i] = d[j]
d[j] = temp
}