From e00f05d7c636f9a1b7a10930402cfeb9331f6631 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 15:41:40 +0200 Subject: feat(command): fetch stats --- commands/stats.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 commands/stats.go (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go new file mode 100644 index 0000000..80a9b1b --- /dev/null +++ b/commands/stats.go @@ -0,0 +1,62 @@ +package commands + +import ( + "time" + + "git.anhgelus.world/anhgelus/les-copaings-bot/config" + "git.anhgelus.world/anhgelus/les-copaings-bot/exp" + "git.anhgelus.world/anhgelus/les-copaings-bot/user" + "github.com/anhgelus/gokord" + "github.com/anhgelus/gokord/cmd" + "github.com/anhgelus/gokord/logger" + "github.com/bwmarrin/discordgo" +) + +type data struct { + CreatedAt time.Time + XP int + CopaingID int + copaing *user.Copaing +} + +func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionMap, resp *cmd.ResponseBuilder) { + cfg := config.GetGuildConfig(i.GuildID) + days := cfg.DaysXPRemains + if v, ok := opt["days"]; ok { + in := v.IntValue() + if in < 0 || uint(in) > days { + if err := resp.SetMessage("Nombre de jours invalide").IsEphemeral().Send(); err != nil { + logger.Alert("commands/stats.go - Sending invalid days", err.Error()) + } + return + } + days = uint(in) + } + var stats []*data + res := gokord.DB.Raw( + `SELECT "created_at"::date::text, sum(xp) as xp, copaing_id FROM copaing_xps GROUP BY "created_at"::date `+ + ` WHERE guild_id = ? and created_at < ?`, + 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(&stats).Error; err != nil { + logger.Alert("commands/stats.go - Scanning result", err.Error(), "res") + return + } + copaings := map[int]*user.Copaing{} + for _, s := range stats { + c, ok := copaings[s.CopaingID] + if ok { + s.copaing = c + } else { + if err := gokord.DB.First(s.copaing, s.CopaingID).Error; err != nil { + logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", s.CopaingID) + return + } + copaings[s.CopaingID] = s.copaing + } + } +} -- cgit v1.2.3 From a795e62723a2eb04dab421f171881597be139374 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 16:50:04 +0200 Subject: feat(command): generate plot and send --- commands/stats.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 80a9b1b..6900051 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -1,6 +1,7 @@ package commands import ( + "bytes" "time" "git.anhgelus.world/anhgelus/les-copaings-bot/config" @@ -10,6 +11,10 @@ import ( "github.com/anhgelus/gokord/cmd" "github.com/anhgelus/gokord/logger" "github.com/bwmarrin/discordgo" + "gonum.org/v1/plot" + "gonum.org/v1/plot/plotter" + "gonum.org/v1/plot/plotutil" + "gonum.org/v1/plot/vg" ) type data struct { @@ -19,7 +24,7 @@ type data struct { copaing *user.Copaing } -func Stats(_ *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) { cfg := config.GetGuildConfig(i.GuildID) days := cfg.DaysXPRemains if v, ok := opt["days"]; ok { @@ -32,7 +37,7 @@ func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM } days = uint(in) } - var stats []*data + var rawData []*data res := gokord.DB.Raw( `SELECT "created_at"::date::text, sum(xp) as xp, copaing_id FROM copaing_xps GROUP BY "created_at"::date `+ ` WHERE guild_id = ? and created_at < ?`, @@ -42,12 +47,15 @@ func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM logger.Alert("commands/stats.go - Fetching XP data", res.Error.Error(), "guild_id", i.GuildID) return } - if err := res.Scan(&stats).Error; err != nil { + if err := res.Scan(&rawData).Error; err != nil { logger.Alert("commands/stats.go - Scanning result", err.Error(), "res") return } + copaings := map[int]*user.Copaing{} - for _, s := range stats { + stats := map[int]*[]*plotter.XY{} + + for _, s := range rawData { c, ok := copaings[s.CopaingID] if ok { s.copaing = c @@ -58,5 +66,51 @@ func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM } copaings[s.CopaingID] = s.copaing } + pts, ok := stats[s.CopaingID] + if !ok { + pts = &[]*plotter.XY{} + stats[s.CopaingID] = pts + } + t := float64(s.CreatedAt.Unix()-time.Now().Unix()) / (24 * 60 * 60) + *pts = append(*pts, &plotter.XY{ + X: t, + Y: float64(s.XP), + }) + } + + p := plot.New() + p.Title.Text = "XP" + p.X.Label.Text = "Jours" + p.Y.Label.Text = "XP" + + for in, c := range copaings { + m, err := s.GuildMember(i.GuildID, c.DiscordID) + if err != nil { + logger.Alert("commands/stats.go - Fetching guild member", err.Error()) + return + } + err = plotutil.AddLinePoints(p, m.DisplayName(), stats[in]) + if err != nil { + logger.Alert("commands/stats.go - Adding line points", err.Error()) + return + } + } + w, err := p.WriterTo(4*vg.Inch, 4*vg.Inch, "png") + if err != nil { + logger.Alert("commands/stats.go - Generating png", err.Error()) + return + } + b := new(bytes.Buffer) + _, err = w.WriteTo(b) + if err != nil { + logger.Alert("commands/stats.go - Writing png", err.Error()) + } + err = resp.AddFile(&discordgo.File{ + Name: "plot.png", + ContentType: "image/png", + Reader: b, + }).Send() + if err != nil { + logger.Alert("commands/stats.go - Sending response", err.Error()) } } -- cgit v1.2.3 From cf4e7a6efd08e50885cf85d3e198e8ca59105589 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 17:35:26 +0200 Subject: fix(command): wrong sql and weird gorm behaviors --- commands/stats.go | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 6900051..116e89f 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -11,6 +11,7 @@ import ( "github.com/anhgelus/gokord/cmd" "github.com/anhgelus/gokord/logger" "github.com/bwmarrin/discordgo" + "github.com/jackc/pgx/v5/pgtype" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/plotutil" @@ -18,10 +19,9 @@ import ( ) type data struct { - CreatedAt time.Time + CreatedAt *pgtype.Date XP int CopaingID int - copaing *user.Copaing } 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 res := gokord.DB.Raw( - `SELECT "created_at"::date::text, sum(xp) as xp, copaing_id FROM copaing_xps GROUP BY "created_at"::date `+ - ` WHERE guild_id = ? and created_at < ?`, + `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"`, 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 { - logger.Alert("commands/stats.go - Scanning result", err.Error(), "res") + logger.Alert("commands/stats.go - Fetching result", err.Error()) return } copaings := map[int]*user.Copaing{} - stats := map[int]*[]*plotter.XY{} + stats := map[int]*[]plotter.XY{} - for _, s := range rawData { - c, ok := copaings[s.CopaingID] - if ok { - s.copaing = c - } else { - if err := gokord.DB.First(s.copaing, s.CopaingID).Error; err != nil { - logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", s.CopaingID) + for _, raw := range rawData { + _, ok := copaings[raw.CopaingID] + if !ok { + var cp user.Copaing + if err := gokord.DB.First(&cp, raw.CopaingID).Error; err != nil { + logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", raw.CopaingID) return } - copaings[s.CopaingID] = s.copaing + copaings[raw.CopaingID] = &cp } - pts, ok := stats[s.CopaingID] + pts, ok := stats[raw.CopaingID] if !ok { - pts = &[]*plotter.XY{} - stats[s.CopaingID] = pts + pts = &[]plotter.XY{} + stats[raw.CopaingID] = pts } - t := float64(s.CreatedAt.Unix()-time.Now().Unix()) / (24 * 60 * 60) - *pts = append(*pts, &plotter.XY{ + t := float64(raw.CreatedAt.Time.Unix()-time.Now().Unix()) / (24 * 60 * 60) + *pts = append(*pts, plotter.XY{ X: t, - Y: float64(s.XP), + Y: float64(raw.XP), }) } p := plot.New() - p.Title.Text = "XP" + p.Title.Text = "Évolution de l'XP" p.X.Label.Text = "Jours" 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()) return } - err = plotutil.AddLinePoints(p, m.DisplayName(), stats[in]) + err = plotutil.AddLinePoints(p, m.DisplayName(), plotter.XYs(*stats[in])) if err != nil { logger.Alert("commands/stats.go - Adding line points", err.Error()) return -- cgit v1.2.3 From adf1e6008e5166232c24b1de09640f9c9fb6ffba Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 18:41:13 +0200 Subject: feat(command): better plot for stats --- commands/stats.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 11 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 116e89f..e464890 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -2,6 +2,10 @@ package commands import ( "bytes" + "image/color" + "math" + "math/rand/v2" + "slices" "time" "git.anhgelus.world/anhgelus/les-copaings-bot/config" @@ -14,11 +18,16 @@ import ( "github.com/jackc/pgx/v5/pgtype" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" - "gonum.org/v1/plot/plotutil" "gonum.org/v1/plot/vg" ) type data struct { + CreatedAt time.Time + XP int + CopaingID int +} + +type dbData struct { CreatedAt *pgtype.Date XP int CopaingID int @@ -38,13 +47,40 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM days = uint(in) } var rawData []*data - res := gokord.DB.Raw( - `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"`, - i.GuildID, exp.TimeStampNDaysBefore(days), - ) - if err := res.Scan(&rawData).Error; err != nil { - logger.Alert("commands/stats.go - Fetching result", err.Error()) - return + if gokord.Debug { + var rawCopaingData []*user.CopaingXP + err := gokord.DB. + Where("guild_id = ? and created_at > ?", i.GuildID, exp.TimeStampNDaysBefore(days)). + Find(&rawCopaingData). + Error + if err != nil { + logger.Alert("commands/stats.go - Fetching result", err.Error()) + return + } + rawData = make([]*data, len(rawCopaingData)) + for in, d := range rawCopaingData { + rawData[in] = &data{ + CreatedAt: d.CreatedAt, + XP: int(d.XP), + CopaingID: int(d.CopaingID), + } + } + } else { + sql := `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"` + var rawDbData []dbData + res := gokord.DB.Raw(sql, i.GuildID, exp.TimeStampNDaysBefore(days)) + if err := res.Scan(&rawDbData).Error; err != nil { + logger.Alert("commands/stats.go - Fetching result", err.Error()) + return + } + rawData = make([]*data, len(rawDbData)) + for in, d := range rawDbData { + rawData[in] = &data{ + CreatedAt: d.CreatedAt.Time, + XP: d.XP, + CopaingID: d.CopaingID, + } + } } copaings := map[int]*user.Copaing{} @@ -65,7 +101,10 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM pts = &[]plotter.XY{} stats[raw.CopaingID] = pts } - t := float64(raw.CreatedAt.Time.Unix()-time.Now().Unix()) / (24 * 60 * 60) + t := float64(raw.CreatedAt.Unix() - time.Now().Unix()) + if !gokord.Debug { + t = math.Ceil(t / (24 * 60 * 60)) + } *pts = append(*pts, plotter.XY{ X: t, Y: float64(raw.XP), @@ -77,19 +116,48 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM p.X.Label.Text = "Jours" p.Y.Label.Text = "XP" + p.Add(plotter.NewGrid()) + + r := rand.New(rand.NewPCG(uint64(time.Now().Unix()), uint64(time.Now().Unix()))) for in, c := range copaings { m, err := s.GuildMember(i.GuildID, c.DiscordID) if err != nil { logger.Alert("commands/stats.go - Fetching guild member", err.Error()) return } - err = plotutil.AddLinePoints(p, m.DisplayName(), plotter.XYs(*stats[in])) + slices.SortFunc(*stats[in], func(a, b plotter.XY) int { + if a.X < b.X { + return -1 + } + if a.X > b.X { + return 1 + } + return 0 + }) + first := (*stats[in])[0] + if first.X > float64(-days) { + *stats[in] = append([]plotter.XY{{ + X: first.X - 1, Y: 0, + }}, *stats[in]...) + } + last := (*stats[in])[len(*stats[in])-1] + if last.X <= -1 { + *stats[in] = append(*stats[in], plotter.XY{ + X: last.X + 1, Y: 0, + }) + } + l, err := plotter.NewLine(plotter.XYs(*stats[in])) if err != nil { logger.Alert("commands/stats.go - Adding line points", err.Error()) return } + l.LineStyle.Width = vg.Points(1) + l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)} + l.LineStyle.Color = color.RGBA{R: uint8(r.UintN(255)), G: uint8(r.UintN(255)), B: uint8(r.UintN(255)), A: 255} + p.Add(l) + p.Legend.Add(m.DisplayName(), l) } - w, err := p.WriterTo(4*vg.Inch, 4*vg.Inch, "png") + w, err := p.WriterTo(8*vg.Inch, 6*vg.Inch, "png") if err != nil { logger.Alert("commands/stats.go - Generating png", err.Error()) return -- cgit v1.2.3 From 236f57fb1525400bd4b7b6304b9d04f44cd021cc Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 18:51:54 +0200 Subject: feat(command): days option for stats --- commands/stats.go | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index e464890..345f3ba 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -3,6 +3,7 @@ package commands import ( "bytes" "image/color" + "io" "math" "math/rand/v2" "slices" @@ -46,6 +47,29 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM } days = uint(in) } + w, err := statsAll(s, i, days) + if err != nil { + if err = resp.IsEphemeral().SetMessage("Il y a eu une erreur...").Send(); err != nil { + logger.Alert("commands/stats.go - Sending error occurred", err.Error()) + } + return + } + b := new(bytes.Buffer) + _, err = w.WriteTo(b) + if err != nil { + logger.Alert("commands/stats.go - Writing png", err.Error()) + } + err = resp.AddFile(&discordgo.File{ + Name: "plot.png", + ContentType: "image/png", + Reader: b, + }).Send() + if err != nil { + logger.Alert("commands/stats.go - Sending response", err.Error()) + } +} + +func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) (io.WriterTo, error) { var rawData []*data if gokord.Debug { var rawCopaingData []*user.CopaingXP @@ -55,7 +79,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM Error if err != nil { logger.Alert("commands/stats.go - Fetching result", err.Error()) - return + return nil, err } rawData = make([]*data, len(rawCopaingData)) for in, d := range rawCopaingData { @@ -71,7 +95,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM res := gokord.DB.Raw(sql, i.GuildID, exp.TimeStampNDaysBefore(days)) if err := res.Scan(&rawDbData).Error; err != nil { logger.Alert("commands/stats.go - Fetching result", err.Error()) - return + return nil, err } rawData = make([]*data, len(rawDbData)) for in, d := range rawDbData { @@ -92,7 +116,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM var cp user.Copaing if err := gokord.DB.First(&cp, raw.CopaingID).Error; err != nil { logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", raw.CopaingID) - return + return nil, err } copaings[raw.CopaingID] = &cp } @@ -111,6 +135,10 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM }) } + return generatePlot(s, i, days, copaings, stats) +} + +func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, copaings map[int]*user.Copaing, stats map[int]*[]plotter.XY) (io.WriterTo, error) { p := plot.New() p.Title.Text = "Évolution de l'XP" p.X.Label.Text = "Jours" @@ -123,7 +151,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM m, err := s.GuildMember(i.GuildID, c.DiscordID) if err != nil { logger.Alert("commands/stats.go - Fetching guild member", err.Error()) - return + return nil, err } slices.SortFunc(*stats[in], func(a, b plotter.XY) int { if a.X < b.X { @@ -149,7 +177,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM l, err := plotter.NewLine(plotter.XYs(*stats[in])) if err != nil { logger.Alert("commands/stats.go - Adding line points", err.Error()) - return + return nil, err } l.LineStyle.Width = vg.Points(1) l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)} @@ -160,19 +188,7 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM w, err := p.WriterTo(8*vg.Inch, 6*vg.Inch, "png") if err != nil { logger.Alert("commands/stats.go - Generating png", err.Error()) - return - } - b := new(bytes.Buffer) - _, err = w.WriteTo(b) - if err != nil { - logger.Alert("commands/stats.go - Writing png", err.Error()) - } - err = resp.AddFile(&discordgo.File{ - Name: "plot.png", - ContentType: "image/png", - Reader: b, - }).Send() - if err != nil { - logger.Alert("commands/stats.go - Sending response", err.Error()) + return nil, err } + return w, nil } -- cgit v1.2.3 From 26643cd271b7efeaab9a00d829e182297082555c Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 19:00:54 +0200 Subject: feat(command): user option for stats --- commands/stats.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 345f3ba..ccc5857 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -47,7 +47,13 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM } days = uint(in) } - w, err := statsAll(s, i, days) + var w io.WriterTo + var err error + if v, ok := opt["user"]; ok { + w, err = statsMember(s, i, days, v.UserValue(s).ID) + } else { + w, err = statsAll(s, i, days) + } if err != nil { if err = resp.IsEphemeral().SetMessage("Il y a eu une erreur...").Send(); err != nil { logger.Alert("commands/stats.go - Sending error occurred", err.Error()) @@ -138,6 +144,72 @@ func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) ( return generatePlot(s, i, days, copaings, stats) } +func statsMember(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, discordID string) (io.WriterTo, error) { + cp := user.GetCopaing(discordID, i.GuildID) + var rawData []*data + if gokord.Debug { + var rawCopaingData []*user.CopaingXP + err := gokord.DB. + Where( + "guild_id = ? and created_at > ? and copaing_id = ?", + i.GuildID, exp.TimeStampNDaysBefore(days), discordID, + ). + Find(&rawCopaingData). + Error + if err != nil { + logger.Alert("commands/stats.go - Fetching result", err.Error()) + return nil, err + } + rawData = make([]*data, len(rawCopaingData)) + for in, d := range rawCopaingData { + rawData[in] = &data{ + CreatedAt: d.CreatedAt, + XP: int(d.XP), + CopaingID: int(d.CopaingID), + } + } + } else { + sql := `SELECT "created_at"::date::text, sum("xp") as xp, "copaing_id" FROM copaing_xps WHERE "guild_id" = ? and "created_at" > ? and "copaing_id" = ? GROUP BY "created_at"::date, "copaing_id"` + var rawDbData []dbData + res := gokord.DB.Raw(sql, i.GuildID, exp.TimeStampNDaysBefore(days), discordID) + if err := res.Scan(&rawDbData).Error; err != nil { + logger.Alert("commands/stats.go - Fetching result", err.Error()) + return nil, err + } + rawData = make([]*data, len(rawDbData)) + for in, d := range rawDbData { + rawData[in] = &data{ + CreatedAt: d.CreatedAt.Time, + XP: d.XP, + CopaingID: d.CopaingID, + } + } + } + + copaings := map[int]*user.Copaing{ + int(cp.ID): cp, + } + stats := map[int]*[]plotter.XY{} + + for _, raw := range rawData { + pts, ok := stats[raw.CopaingID] + if !ok { + pts = &[]plotter.XY{} + stats[raw.CopaingID] = pts + } + t := float64(raw.CreatedAt.Unix() - time.Now().Unix()) + if !gokord.Debug { + t = math.Ceil(t / (24 * 60 * 60)) + } + *pts = append(*pts, plotter.XY{ + X: t, + Y: float64(raw.XP), + }) + } + + return generatePlot(s, i, days, copaings, stats) +} + func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, copaings map[int]*user.Copaing, stats map[int]*[]plotter.XY) (io.WriterTo, error) { p := plot.New() p.Title.Text = "Évolution de l'XP" -- cgit v1.2.3 From d63203709f421c2b0f093d8ee0045f0854f77dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 14:30:14 +0200 Subject: refactor(stats): rewrite data creation --- commands/stats.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index ccc5857..5d22483 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -128,17 +128,20 @@ func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) ( } pts, ok := stats[raw.CopaingID] if !ok { - pts = &[]plotter.XY{} + t := make([]plotter.XY, days) + pts = &t stats[raw.CopaingID] = pts } - t := float64(raw.CreatedAt.Unix() - time.Now().Unix()) + t := raw.CreatedAt.Unix() - time.Now().Unix() if !gokord.Debug { - t = math.Ceil(t / (24 * 60 * 60)) + t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) + } else { + t = int64(math.Ceil(float64(t) / 6)) } - *pts = append(*pts, plotter.XY{ - X: t, + (*pts)[t] = plotter.XY{ + X: float64(t), Y: float64(raw.XP), - }) + } } return generatePlot(s, i, days, copaings, stats) @@ -194,17 +197,20 @@ func statsMember(s *discordgo.Session, i *discordgo.InteractionCreate, days uint for _, raw := range rawData { pts, ok := stats[raw.CopaingID] if !ok { - pts = &[]plotter.XY{} + t := make([]plotter.XY, days) + pts = &t stats[raw.CopaingID] = pts } - t := float64(raw.CreatedAt.Unix() - time.Now().Unix()) + t := raw.CreatedAt.Unix() - time.Now().Unix() if !gokord.Debug { - t = math.Ceil(t / (24 * 60 * 60)) + t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) + } else { + t = int64(math.Ceil(float64(t) / 6)) } - *pts = append(*pts, plotter.XY{ - X: t, + (*pts)[t] = plotter.XY{ + X: float64(t), Y: float64(raw.XP), - }) + } } return generatePlot(s, i, days, copaings, stats) -- cgit v1.2.3 From 103bc0358fff171c949475406cec8161819a5a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 14:53:16 +0200 Subject: refactor(stats): unify all and one member --- commands/stats.go | 136 ++++++++++++++++-------------------------------------- 1 file changed, 39 insertions(+), 97 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 5d22483..1e55ddd 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -2,6 +2,7 @@ package commands import ( "bytes" + "gorm.io/gorm" "image/color" "io" "math" @@ -76,14 +77,26 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM } func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) (io.WriterTo, error) { + return stats(s, i, days, func(before, after string) *gorm.DB { + return gokord.DB.Raw(before+"WHERE guild_id = ? and created_at > ?"+after, i.GuildID, exp.TimeStampNDaysBefore(days)) + }) +} + +func statsMember(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, discordID string) (io.WriterTo, error) { + _, err := s.GuildMember(i.GuildID, i.Member.User.ID) + if err != nil { + return nil, err + } + return stats(s, i, days, func(before, after string) *gorm.DB { + return gokord.DB.Raw(before+"WHERE guild_id = ? and created_at > ? and copaing_id = ?"+after, i.GuildID, exp.TimeStampNDaysBefore(days), discordID) + }) +} + +func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, execSql func(before, after string) *gorm.DB) (io.WriterTo, error) { var rawData []*data if gokord.Debug { var rawCopaingData []*user.CopaingXP - err := gokord.DB. - Where("guild_id = ? and created_at > ?", i.GuildID, exp.TimeStampNDaysBefore(days)). - Find(&rawCopaingData). - Error - if err != nil { + if err := execSql("SELECT * FROM copaings_xps ", "").Scan(&rawCopaingData).Error; err != nil { logger.Alert("commands/stats.go - Fetching result", err.Error()) return nil, err } @@ -96,10 +109,10 @@ func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) ( } } } else { - sql := `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"` var rawDbData []dbData - res := gokord.DB.Raw(sql, i.GuildID, exp.TimeStampNDaysBefore(days)) - if err := res.Scan(&rawDbData).Error; err != nil { + if err := execSql( + `SELECT "created_at"::date::text, sum("xp") as xp, "copaing_id" FROM copaing_xps `, ` GROUP BY "created_at"::date, "copaing_id"`, + ).Scan(&rawDbData).Error; err != nil { logger.Alert("commands/stats.go - Fetching result", err.Error()) return nil, err } @@ -114,7 +127,7 @@ func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) ( } copaings := map[int]*user.Copaing{} - stats := map[int]*[]plotter.XY{} + stats := map[int][]plotter.XY{} for _, raw := range rawData { _, ok := copaings[raw.CopaingID] @@ -128,77 +141,7 @@ func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) ( } pts, ok := stats[raw.CopaingID] if !ok { - t := make([]plotter.XY, days) - pts = &t - stats[raw.CopaingID] = pts - } - t := raw.CreatedAt.Unix() - time.Now().Unix() - if !gokord.Debug { - t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) - } else { - t = int64(math.Ceil(float64(t) / 6)) - } - (*pts)[t] = plotter.XY{ - X: float64(t), - Y: float64(raw.XP), - } - } - - return generatePlot(s, i, days, copaings, stats) -} - -func statsMember(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, discordID string) (io.WriterTo, error) { - cp := user.GetCopaing(discordID, i.GuildID) - var rawData []*data - if gokord.Debug { - var rawCopaingData []*user.CopaingXP - err := gokord.DB. - Where( - "guild_id = ? and created_at > ? and copaing_id = ?", - i.GuildID, exp.TimeStampNDaysBefore(days), discordID, - ). - Find(&rawCopaingData). - Error - if err != nil { - logger.Alert("commands/stats.go - Fetching result", err.Error()) - return nil, err - } - rawData = make([]*data, len(rawCopaingData)) - for in, d := range rawCopaingData { - rawData[in] = &data{ - CreatedAt: d.CreatedAt, - XP: int(d.XP), - CopaingID: int(d.CopaingID), - } - } - } else { - sql := `SELECT "created_at"::date::text, sum("xp") as xp, "copaing_id" FROM copaing_xps WHERE "guild_id" = ? and "created_at" > ? and "copaing_id" = ? GROUP BY "created_at"::date, "copaing_id"` - var rawDbData []dbData - res := gokord.DB.Raw(sql, i.GuildID, exp.TimeStampNDaysBefore(days), discordID) - if err := res.Scan(&rawDbData).Error; err != nil { - logger.Alert("commands/stats.go - Fetching result", err.Error()) - return nil, err - } - rawData = make([]*data, len(rawDbData)) - for in, d := range rawDbData { - rawData[in] = &data{ - CreatedAt: d.CreatedAt.Time, - XP: d.XP, - CopaingID: d.CopaingID, - } - } - } - - copaings := map[int]*user.Copaing{ - int(cp.ID): cp, - } - stats := map[int]*[]plotter.XY{} - - for _, raw := range rawData { - pts, ok := stats[raw.CopaingID] - if !ok { - t := make([]plotter.XY, days) - pts = &t + pts = make([]plotter.XY, days) stats[raw.CopaingID] = pts } t := raw.CreatedAt.Unix() - time.Now().Unix() @@ -207,16 +150,15 @@ func statsMember(s *discordgo.Session, i *discordgo.InteractionCreate, days uint } else { t = int64(math.Ceil(float64(t) / 6)) } - (*pts)[t] = plotter.XY{ + pts[t] = plotter.XY{ X: float64(t), Y: float64(raw.XP), } } - return generatePlot(s, i, days, copaings, stats) } -func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, copaings map[int]*user.Copaing, stats map[int]*[]plotter.XY) (io.WriterTo, error) { +func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, copaings map[int]*user.Copaing, stats map[int][]plotter.XY) (io.WriterTo, error) { p := plot.New() p.Title.Text = "Évolution de l'XP" p.X.Label.Text = "Jours" @@ -231,7 +173,7 @@ func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uin logger.Alert("commands/stats.go - Fetching guild member", err.Error()) return nil, err } - slices.SortFunc(*stats[in], func(a, b plotter.XY) int { + slices.SortFunc(stats[in], func(a, b plotter.XY) int { if a.X < b.X { return -1 } @@ -240,19 +182,19 @@ func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uin } return 0 }) - first := (*stats[in])[0] - if first.X > float64(-days) { - *stats[in] = append([]plotter.XY{{ - X: first.X - 1, Y: 0, - }}, *stats[in]...) - } - last := (*stats[in])[len(*stats[in])-1] - if last.X <= -1 { - *stats[in] = append(*stats[in], plotter.XY{ - X: last.X + 1, Y: 0, - }) - } - l, err := plotter.NewLine(plotter.XYs(*stats[in])) + //first := stats[in][0] + //if first.X > float64(-days) { + // stats[in] = append([]plotter.XY{{ + // X: first.X - 1, Y: 0, + // }}, stats[in]...) + //} + //last := stats[in][len(stats[in])-1] + //if last.X <= -1 { + // stats[in] = append(stats[in], plotter.XY{ + // X: last.X + 1, Y: 0, + // }) + //} + l, err := plotter.NewLine(plotter.XYs(stats[in])) if err != nil { logger.Alert("commands/stats.go - Adding line points", err.Error()) return nil, err -- cgit v1.2.3 From 49e4fc5971e44305d99952438cf087b0b42d6592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 14:57:29 +0200 Subject: perf(stats): send deferred before creating plot --- commands/stats.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 1e55ddd..7a0da6b 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -48,8 +48,12 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM } days = uint(in) } + err := resp.IsDeferred().Send() + if err != nil { + logger.Alert("commands/stats.go - Sending deferred", err.Error()) + return + } var w io.WriterTo - var err error if v, ok := opt["user"]; ok { w, err = statsMember(s, i, days, v.UserValue(s).ID) } else { -- cgit v1.2.3 From 3f6b0717e2d1e954e0db324bc3a389d2f34e2a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 15:27:23 +0200 Subject: fix(stats): bad table name in debug --- commands/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 7a0da6b..26719dc 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -100,7 +100,7 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec var rawData []*data if gokord.Debug { var rawCopaingData []*user.CopaingXP - if err := execSql("SELECT * FROM copaings_xps ", "").Scan(&rawCopaingData).Error; err != nil { + if err := execSql("SELECT * FROM copaing_xps ", "").Scan(&rawCopaingData).Error; err != nil { logger.Alert("commands/stats.go - Fetching result", err.Error()) return nil, err } -- cgit v1.2.3 From 05941e243f9bef6c8f461bafae9dbd12a88362b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 15:35:55 +0200 Subject: fix(stats): index out of range and not showing no value --- commands/stats.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 26719dc..06b10bd 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -144,17 +144,24 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec copaings[raw.CopaingID] = &cp } pts, ok := stats[raw.CopaingID] + now := time.Now().Unix() if !ok { pts = make([]plotter.XY, days) + for i := 0; i < int(days); i++ { + pts[i] = plotter.XY{ + X: float64(i - int(days)), + Y: 0, + } + } stats[raw.CopaingID] = pts } - t := raw.CreatedAt.Unix() - time.Now().Unix() + t := raw.CreatedAt.Unix() - now if !gokord.Debug { t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) } else { t = int64(math.Ceil(float64(t) / 6)) } - pts[t] = plotter.XY{ + pts[int64(days)-t] = plotter.XY{ X: float64(t), Y: float64(raw.XP), } -- cgit v1.2.3 From 6ec3cf37300c1faf92c62640d1d5fe2638749c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 16:16:36 +0200 Subject: feat(debug): decrease reduce time --- commands/stats.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 06b10bd..3422e2a 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -158,8 +158,6 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec t := raw.CreatedAt.Unix() - now if !gokord.Debug { t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) - } else { - t = int64(math.Ceil(float64(t) / 6)) } pts[int64(days)-t] = plotter.XY{ X: float64(t), -- cgit v1.2.3 From 6ac00e89615838154607319bdff428f778bdd432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 16:19:57 +0200 Subject: fix(stats): another index out of range --- commands/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 3422e2a..3d487d3 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -159,7 +159,7 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec if !gokord.Debug { t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) } - pts[int64(days)-t] = plotter.XY{ + pts[int64(days)+t] = plotter.XY{ // because t <= 0 X: float64(t), Y: float64(raw.XP), } -- cgit v1.2.3 From 0fe0cf1c148316d667018d3f5b3e9d91f0f6c797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 16:26:59 +0200 Subject: fix(stats): bad where condition for member --- commands/stats.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 3d487d3..351090f 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -87,12 +87,15 @@ func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) ( } func statsMember(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, discordID string) (io.WriterTo, error) { - _, err := s.GuildMember(i.GuildID, i.Member.User.ID) + _, err := s.GuildMember(i.GuildID, discordID) if err != nil { return nil, err } return stats(s, i, days, func(before, after string) *gorm.DB { - return gokord.DB.Raw(before+"WHERE guild_id = ? and created_at > ? and copaing_id = ?"+after, i.GuildID, exp.TimeStampNDaysBefore(days), discordID) + return gokord.DB.Raw( + before+"WHERE guild_id = ? and created_at > ? and copaing_id = ?"+after, + i.GuildID, exp.TimeStampNDaysBefore(days), user.GetCopaing(discordID, i.GuildID).ID, + ) }) } @@ -171,6 +174,9 @@ func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uin p := plot.New() p.Title.Text = "Évolution de l'XP" p.X.Label.Text = "Jours" + if gokord.Debug { + p.X.Label.Text = "Secondes" + } p.Y.Label.Text = "XP" p.Add(plotter.NewGrid()) -- cgit v1.2.3 From a23130addc0a8e988a5b360b97d72d2917253831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 16:34:41 +0200 Subject: perf(stats): create goroutine for plot generation --- commands/stats.go | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 351090f..ccd9412 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -53,31 +53,33 @@ func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM logger.Alert("commands/stats.go - Sending deferred", err.Error()) return } - var w io.WriterTo - if v, ok := opt["user"]; ok { - w, err = statsMember(s, i, days, v.UserValue(s).ID) - } else { - w, err = statsAll(s, i, days) - } - if err != nil { - if err = resp.IsEphemeral().SetMessage("Il y a eu une erreur...").Send(); err != nil { - logger.Alert("commands/stats.go - Sending error occurred", err.Error()) + go func() { + var w io.WriterTo + if v, ok := opt["user"]; ok { + w, err = statsMember(s, i, days, v.UserValue(s).ID) + } else { + w, err = statsAll(s, i, days) } - return - } - b := new(bytes.Buffer) - _, err = w.WriteTo(b) - if err != nil { - logger.Alert("commands/stats.go - Writing png", err.Error()) - } - err = resp.AddFile(&discordgo.File{ - Name: "plot.png", - ContentType: "image/png", - Reader: b, - }).Send() - if err != nil { - logger.Alert("commands/stats.go - Sending response", err.Error()) - } + if err != nil { + if err = resp.IsEphemeral().SetMessage("Il y a eu une erreur...").Send(); err != nil { + logger.Alert("commands/stats.go - Sending error occurred", err.Error()) + } + return + } + b := new(bytes.Buffer) + _, err = w.WriteTo(b) + if err != nil { + logger.Alert("commands/stats.go - Writing png", err.Error()) + } + err = resp.AddFile(&discordgo.File{ + Name: "plot.png", + ContentType: "image/png", + Reader: b, + }).Send() + if err != nil { + logger.Alert("commands/stats.go - Sending response", err.Error()) + } + }() } func statsAll(s *discordgo.Session, i *discordgo.InteractionCreate, days uint) (io.WriterTo, error) { -- cgit v1.2.3 From 355db490b72f51e312d599bd38c7e412cbb94691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 16:47:01 +0200 Subject: fix(stats): index out of range again cannot be tested in debug mode --- commands/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index ccd9412..263d80c 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -164,7 +164,7 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec if !gokord.Debug { t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) } - pts[int64(days)+t] = plotter.XY{ // because t <= 0 + pts[int64(days)+t-1] = plotter.XY{ // because t <= 0 X: float64(t), Y: float64(raw.XP), } -- cgit v1.2.3 From 59ba978ba78b56e9195946b5bb12011a7a936294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 16:56:02 +0200 Subject: fix(stats): index out of range another time I think I have fixed every out out of range could be tested in debug mode, but is very hard to reproduce --- commands/stats.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 263d80c..34734b0 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -151,8 +151,8 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec pts, ok := stats[raw.CopaingID] now := time.Now().Unix() if !ok { - pts = make([]plotter.XY, days) - for i := 0; i < int(days); i++ { + pts = make([]plotter.XY, days+1) + for i := 0; i < len(pts); i++ { pts[i] = plotter.XY{ X: float64(i - int(days)), Y: 0, @@ -164,7 +164,7 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec if !gokord.Debug { t = int64(math.Ceil(float64(t) / (24 * 60 * 60))) } - pts[int64(days)+t-1] = plotter.XY{ // because t <= 0 + pts[int64(days)+t] = plotter.XY{ // because t <= 0 X: float64(t), Y: float64(raw.XP), } -- cgit v1.2.3 From 22ebfcf0c9b309a1b14ea9b8fa39f15b610eb0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 17:27:14 +0200 Subject: style(stats): remove useless parts --- commands/stats.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 34734b0..cde49a1 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -169,10 +169,10 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec Y: float64(raw.XP), } } - return generatePlot(s, i, days, copaings, stats) + return generatePlot(s, i, copaings, stats) } -func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, copaings map[int]*user.Copaing, stats map[int][]plotter.XY) (io.WriterTo, error) { +func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, copaings map[int]*user.Copaing, stats map[int][]plotter.XY) (io.WriterTo, error) { p := plot.New() p.Title.Text = "Évolution de l'XP" p.X.Label.Text = "Jours" @@ -199,18 +199,6 @@ func generatePlot(s *discordgo.Session, i *discordgo.InteractionCreate, days uin } return 0 }) - //first := stats[in][0] - //if first.X > float64(-days) { - // stats[in] = append([]plotter.XY{{ - // X: first.X - 1, Y: 0, - // }}, stats[in]...) - //} - //last := stats[in][len(stats[in])-1] - //if last.X <= -1 { - // stats[in] = append(stats[in], plotter.XY{ - // X: last.X + 1, Y: 0, - // }) - //} l, err := plotter.NewLine(plotter.XYs(stats[in])) if err != nil { logger.Alert("commands/stats.go - Adding line points", err.Error()) -- cgit v1.2.3 From 12585b20d504a664ce887d781bce1d299350d244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 31 Aug 2025 17:35:01 +0200 Subject: feat(stats): avoid stopping process if copaing is not found --- commands/stats.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index cde49a1..0114918 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -2,6 +2,7 @@ package commands import ( "bytes" + "errors" "gorm.io/gorm" "image/color" "io" @@ -143,8 +144,12 @@ func stats(s *discordgo.Session, i *discordgo.InteractionCreate, days uint, exec if !ok { var cp user.Copaing if err := gokord.DB.First(&cp, raw.CopaingID).Error; err != nil { - logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", raw.CopaingID) - return nil, err + if !errors.Is(err, gorm.ErrRecordNotFound) { + logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", raw.CopaingID) + return nil, err + } + logger.Warn("Copaing not found, skipping entry", "old_id", raw.CopaingID) + continue } copaings[raw.CopaingID] = &cp } -- cgit v1.2.3 From 30ecd60b041398390f11fccdf46444fa28690bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Thu, 4 Sep 2025 16:25:59 +0200 Subject: build(gokord): upgrade to latest nightly still use discordgo name before merging anhgelus/gokord with nyttikord/gokord --- commands/stats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands/stats.go') diff --git a/commands/stats.go b/commands/stats.go index 0114918..f93f6a0 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -17,8 +17,8 @@ import ( "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/cmd" "github.com/anhgelus/gokord/logger" - "github.com/bwmarrin/discordgo" "github.com/jackc/pgx/v5/pgtype" + discordgo "github.com/nyttikord/gokord" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/vg" -- cgit v1.2.3