fix(command): wrong sql and weird gorm behaviors
This commit is contained in:
parent
a795e62723
commit
cf4e7a6efd
1 changed files with 20 additions and 26 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue