-
Notifications
You must be signed in to change notification settings - Fork 927
/
serverstats.go
105 lines (82 loc) · 2.48 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
package serverstats
//go:generate sqlboiler --no-hooks psql
import (
"context"
"database/sql"
"strconv"
"strings"
"sync"
"time"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/premium"
"github.com/jonas747/yagpdb/serverstats/models"
)
var confDeprecated = config.RegisterOption("yagpdb.serverstats.deprecated", "Wether to mark server stats as disabled or not, this will disable recording of new stats", false)
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)
}
// 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
}
// 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())
}
var _ premium.NewPremiumGuildListener = (*Plugin)(nil)
func (p *Plugin) OnNewPremiumGuild(guildID int64) error {
const q = `UPDATE server_stats_periods_compressed SET premium=true WHERE guild_id=$1 AND premium=false`
_, err := common.PQ.Exec(q, guildID)
return err
}