fix(command): wrong sql and weird gorm behaviors

This commit is contained in:
Anhgelus Morhtuuzh 2025-08-21 17:35:26 +02:00
parent a795e62723
commit cf4e7a6efd
Signed by: anhgelus
GPG key ID: CAD341EFA92DDDE5

View file

@ -11,6 +11,7 @@ import (
"github.com/anhgelus/gokord/cmd" "github.com/anhgelus/gokord/cmd"
"github.com/anhgelus/gokord/logger" "github.com/anhgelus/gokord/logger"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/jackc/pgx/v5/pgtype"
"gonum.org/v1/plot" "gonum.org/v1/plot"
"gonum.org/v1/plot/plotter" "gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil" "gonum.org/v1/plot/plotutil"
@ -18,10 +19,9 @@ import (
) )
type data struct { type data struct {
CreatedAt time.Time CreatedAt *pgtype.Date
XP int XP int
CopaingID int CopaingID int
copaing *user.Copaing
} }
func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionMap, resp *cmd.ResponseBuilder) { func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionMap, resp *cmd.ResponseBuilder) {
@ -39,47 +39,41 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM
} }
var rawData []*data var rawData []*data
res := gokord.DB.Raw( res := gokord.DB.Raw(
`SELECT "created_at"::date::text, sum(xp) as xp, copaing_id FROM copaing_xps GROUP BY "created_at"::date `+ `SELECT "created_at"::date::text, sum("xp") as xp, "copaing_id" FROM copaing_xps WHERE "guild_id" = ? and "created_at" > ? GROUP BY "created_at"::date, "copaing_id"`,
` WHERE guild_id = ? and created_at < ?`,
i.GuildID, exp.TimeStampNDaysBefore(days), i.GuildID, exp.TimeStampNDaysBefore(days),
) )
if res.Error != nil {
logger.Alert("commands/stats.go - Fetching XP data", res.Error.Error(), "guild_id", i.GuildID)
return
}
if err := res.Scan(&rawData).Error; err != nil { if err := res.Scan(&rawData).Error; err != nil {
logger.Alert("commands/stats.go - Scanning result", err.Error(), "res") logger.Alert("commands/stats.go - Fetching result", err.Error())
return return
} }
copaings := map[int]*user.Copaing{} copaings := map[int]*user.Copaing{}
stats := map[int]*[]*plotter.XY{} stats := map[int]*[]plotter.XY{}
for _, s := range rawData { for _, raw := range rawData {
c, ok := copaings[s.CopaingID] _, ok := copaings[raw.CopaingID]
if ok { if !ok {
s.copaing = c var cp user.Copaing
} else { if err := gokord.DB.First(&cp, raw.CopaingID).Error; err != nil {
if err := gokord.DB.First(s.copaing, s.CopaingID).Error; err != nil { logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", raw.CopaingID)
logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", s.CopaingID)
return return
} }
copaings[s.CopaingID] = s.copaing copaings[raw.CopaingID] = &cp
} }
pts, ok := stats[s.CopaingID] pts, ok := stats[raw.CopaingID]
if !ok { if !ok {
pts = &[]*plotter.XY{} pts = &[]plotter.XY{}
stats[s.CopaingID] = pts stats[raw.CopaingID] = pts
} }
t := float64(s.CreatedAt.Unix()-time.Now().Unix()) / (24 * 60 * 60) t := float64(raw.CreatedAt.Time.Unix()-time.Now().Unix()) / (24 * 60 * 60)
*pts = append(*pts, &plotter.XY{ *pts = append(*pts, plotter.XY{
X: t, X: t,
Y: float64(s.XP), Y: float64(raw.XP),
}) })
} }
p := plot.New() p := plot.New()
p.Title.Text = "XP" p.Title.Text = "Évolution de l'XP"
p.X.Label.Text = "Jours" p.X.Label.Text = "Jours"
p.Y.Label.Text = "XP" p.Y.Label.Text = "XP"
@ -89,7 +83,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM
logger.Alert("commands/stats.go - Fetching guild member", err.Error()) logger.Alert("commands/stats.go - Fetching guild member", err.Error())
return return
} }
err = plotutil.AddLinePoints(p, m.DisplayName(), stats[in]) err = plotutil.AddLinePoints(p, m.DisplayName(), plotter.XYs(*stats[in]))
if err != nil { if err != nil {
logger.Alert("commands/stats.go - Adding line points", err.Error()) logger.Alert("commands/stats.go - Adding line points", err.Error())
return return