From 6471f828ae9be8f0a4e46a9a73ed8c42c2a0d2a6 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 24 May 2024 18:37:05 -0700 Subject: [PATCH] all: migrate executed command logging to sqlboiler --- commands/commands.go | 4 ---- commands/yagcommmand.go | 10 +++++---- common/common.go | 4 ++++ common/util.go | 22 ------------------- stdcommands/topcommands/topcommands.go | 29 +++++++++++++++++--------- web/handlers_general.go | 9 ++------ 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 6f91c89dd2..6d15c91dbd 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -49,10 +49,6 @@ func (p *Plugin) PluginInfo() *common.PluginInfo { func RegisterPlugin() { plugin := &Plugin{} common.RegisterPlugin(plugin) - err := common.GORM.AutoMigrate(&common.LoggedExecutedCommand{}).Error - if err != nil { - logger.WithError(err).Fatal("Failed migrating logged commands database") - } common.InitSchemas("commands", DBSchemas...) } diff --git a/commands/yagcommmand.go b/commands/yagcommmand.go index e1971b1df7..fde7c80593 100644 --- a/commands/yagcommmand.go +++ b/commands/yagcommmand.go @@ -20,7 +20,10 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/sirupsen/logrus" + "github.com/volatiletech/sqlboiler/v4/boil" "github.com/volatiletech/sqlboiler/v4/queries/qm" + + commonmodels "github.com/botlabs-gg/yagpdb/v2/common/models" ) type ContextKey int @@ -233,7 +236,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) { } // Set up log entry for later use - logEntry := &common.LoggedExecutedCommand{ + logEntry := &commonmodels.ExecutedCommand{ UserID: discordgo.StrID(data.Author.ID), ChannelID: discordgo.StrID(data.ChannelID), @@ -243,8 +246,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) { } if data.GuildData != nil { - logEntry.GuildID = discordgo.StrID(data.GuildData.GS.ID) - + logEntry.GuildID.SetValid(discordgo.StrID(data.GuildData.GS.ID)) } metricsExcecutedCommands.With(prometheus.Labels{"name": "(other)", "trigger_type": triggerType}).Inc() @@ -284,7 +286,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) { } // Create command log entry - err := common.GORM.Create(logEntry).Error + err := logEntry.InsertG(data.Context(), boil.Infer()) if err != nil { logger.WithError(err).Error("Failed creating command execution log") } diff --git a/common/common.go b/common/common.go index 60fb491e30..72557269d6 100644 --- a/common/common.go +++ b/common/common.go @@ -131,6 +131,10 @@ func Init() error { logger.Info("Initializing core schema") InitSchemas("core_configs", CoreServerConfDBSchema, localIDsSchema) + + logger.Info("Initializing executed command log schema") + InitSchemas("executed_commands", ExecutedCommandDBSchemas...) + initQueuedSchemas() return err diff --git a/common/util.go b/common/util.go index c6cbc37ab3..e64804dd10 100644 --- a/common/util.go +++ b/common/util.go @@ -354,28 +354,6 @@ func IsDiscordErr(err error, codes ...int) bool { return false } -type LoggedExecutedCommand struct { - SmallModel - - UserID string - ChannelID string - GuildID string - - // Name of command that was triggered - Command string - // Raw command with arguments passed - RawCommand string - // If command returned any error this will be no-empty - Error string - - TimeStamp time.Time - ResponseTime int64 -} - -func (l LoggedExecutedCommand) TableName() string { - return "executed_commands" -} - func HumanizePermissions(perms int64) (res []string) { if perms&discordgo.PermissionAdministrator == discordgo.PermissionAdministrator { res = append(res, "Administrator") diff --git a/stdcommands/topcommands/topcommands.go b/stdcommands/topcommands/topcommands.go index 7412c514a7..8867aade55 100644 --- a/stdcommands/topcommands/topcommands.go +++ b/stdcommands/topcommands/topcommands.go @@ -26,17 +26,31 @@ func cmdFuncTopCommands(data *dcmd.Data) (interface{}, error) { hours := data.Args[0].Int() within := time.Now().Add(time.Duration(-hours) * time.Hour) - var results []*TopCommandsResult - err := common.GORM.Table(common.LoggedExecutedCommand{}.TableName()).Select("command, COUNT(id)").Where("created_at > ?", within).Group("command").Order("count(id) desc").Scan(&results).Error + const q = ` +SELECT command, COUNT(id) +FROM executed_commands +WHERE created_at > $1 +GROUP BY command +ORDER BY COUNT(id) DESC; +` + rows, err := common.PQ.Query(q, within) if err != nil { return nil, err } + defer rows.Close() out := fmt.Sprintf("```\nCommand stats from now to %d hour(s) ago\n# Total - Command\n", hours) total := 0 - for k, result := range results { - out += fmt.Sprintf("#%02d: %5d - %s\n", k+1, result.Count, result.Command) - total += result.Count + for k := 1; rows.Next(); k++ { + var command string + var count int + err = rows.Scan(&command, &count) + if err != nil { + return nil, err + } + + out += fmt.Sprintf("#%02d: %5d - %s\n", k, count, command) + total += count } cpm := float64(total) / float64(hours) / 60 @@ -46,8 +60,3 @@ func cmdFuncTopCommands(data *dcmd.Data) (interface{}, error) { return out, nil } - -type TopCommandsResult struct { - Command string - Count int -} diff --git a/web/handlers_general.go b/web/handlers_general.go index 0cfb63cc92..8209a65a91 100644 --- a/web/handlers_general.go +++ b/web/handlers_general.go @@ -386,17 +386,12 @@ var commandsRanToday = new(int64) func pollCommandsRan() { t := time.NewTicker(time.Minute) for { - var result struct { - Count int64 - } - within := time.Now().Add(-24 * time.Hour) - - err := common.GORM.Table(common.LoggedExecutedCommand{}.TableName()).Select("COUNT(*)").Where("created_at > ?", within).Scan(&result).Error + count, err := models.ExecutedCommands(models.ExecutedCommandWhere.CreatedAt.GT(within)).CountG(context.Background()) if err != nil { logger.WithError(err).Error("failed counting commands ran today") } else { - atomic.StoreInt64(commandsRanToday, result.Count) + atomic.StoreInt64(commandsRanToday, count) } <-t.C