perf(db): remove useless XP data
This commit is contained in:
parent
067e149e72
commit
eaf9fa51cd
3 changed files with 34 additions and 70 deletions
|
@ -1,8 +1,11 @@
|
||||||
package exp
|
package exp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/anhgelus/gokord"
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
"slices"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MessageXP(length uint, diversity uint) uint {
|
func MessageXP(length uint, diversity uint) uint {
|
||||||
|
@ -42,3 +45,15 @@ func LevelXP(level uint) uint {
|
||||||
math.Pow(float64(5*level), 2),
|
math.Pow(float64(5*level), 2),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TimeStampNDaysBefore returns the timestamp (year-month-day) n days before today
|
||||||
|
func TimeStampNDaysBefore(n uint) string {
|
||||||
|
var y, d int
|
||||||
|
var m time.Month
|
||||||
|
if gokord.Debug {
|
||||||
|
y, m, d = time.Unix(time.Now().Unix()-int64(24*60*60), 0).Date() // reduce time for debug
|
||||||
|
} else {
|
||||||
|
y, m, d = time.Unix(time.Now().Unix()-int64(n*24*60*60), 0).Date()
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d-%d-%d", y, m, d)
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func onNewLevel(dg *discordgo.Session, m *discordgo.Member, level uint) {
|
func onNewLevel(dg *discordgo.Session, m *discordgo.Member, level uint) {
|
||||||
|
@ -55,68 +54,22 @@ func (c *Copaing) OnNewLevel(dg *discordgo.Session, level uint) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func PeriodicReducer(dg *discordgo.Session) {
|
func PeriodicReducer(dg *discordgo.Session) {
|
||||||
var wg sync.WaitGroup
|
wg := &sync.WaitGroup{}
|
||||||
for _, g := range dg.State.Guilds {
|
for _, g := range dg.State.Guilds {
|
||||||
var cs []*Copaing
|
wg.Add(1)
|
||||||
err := gokord.DB.Where("guild_id = ?", g.ID).Find(&cs).Error
|
go func() {
|
||||||
if err != nil {
|
defer wg.Done()
|
||||||
utils.SendAlert("user/level.go - Querying all copaings in Guild", err.Error(), "guild_id", g.ID)
|
cfg := config.GetGuildConfig(g.ID)
|
||||||
continue
|
err := gokord.DB.
|
||||||
}
|
Model(&CopaingXP{}).
|
||||||
for i, c := range cs {
|
Where("guild_id = ? and created_at < ?", g.ID, exp.TimeStampNDaysBefore(cfg.DaysXPRemains)).
|
||||||
if i%50 == 49 {
|
Delete(&CopaingXP{}).
|
||||||
time.Sleep(15 * time.Second) // sleep prevents from spamming the Discord API and the database
|
Error
|
||||||
}
|
|
||||||
var u *discordgo.User
|
|
||||||
u, err = dg.User(c.DiscordID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendAlert(
|
utils.SendAlert("user/level.go - Removing old XP", err.Error(), "guild_id", g.ID)
|
||||||
"user/level.go - Fetching user", err.Error(),
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", g.ID,
|
|
||||||
)
|
|
||||||
utils.SendWarn("Removing user from database", "discord_id", c.DiscordID)
|
|
||||||
if err = gokord.DB.Delete(c).Error; err != nil {
|
|
||||||
utils.SendAlert(
|
|
||||||
"user/level.go - Removing user from database", err.Error(),
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", g.ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
if u.Bot {
|
}()
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, err = dg.GuildMember(g.ID, c.DiscordID); err != nil {
|
|
||||||
utils.SendAlert(
|
|
||||||
"user/level.go - Fetching member", err.Error(),
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", g.ID,
|
|
||||||
)
|
|
||||||
utils.SendWarn(
|
|
||||||
"Removing user from guild in database",
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", g.ID,
|
|
||||||
)
|
|
||||||
if err = gokord.DB.Where("guild_id = ?", g.ID).Delete(c).Error; err != nil {
|
|
||||||
utils.SendAlert(
|
|
||||||
"user/level.go - Removing user from guild in database", err.Error(),
|
|
||||||
"discord_id", c.DiscordID,
|
|
||||||
"guild_id", g.ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
//do things
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
wg.Wait() // finish the entire guild before starting another
|
|
||||||
utils.SendDebug("Periodic reduce, guild finished", "guild", g.Name)
|
|
||||||
time.Sleep(15 * time.Second) // sleep prevents from spamming the Discord API and the database
|
|
||||||
}
|
}
|
||||||
|
wg.Wait()
|
||||||
utils.SendDebug("Periodic reduce finished", "len(guilds)", len(dg.State.Guilds))
|
utils.SendDebug("Periodic reduce finished", "len(guilds)", len(dg.State.Guilds))
|
||||||
}
|
}
|
||||||
|
|
16
user/xp.go
16
user/xp.go
|
@ -1,7 +1,6 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/anhgelus/gokord"
|
"github.com/anhgelus/gokord"
|
||||||
"github.com/anhgelus/gokord/utils"
|
"github.com/anhgelus/gokord/utils"
|
||||||
"github.com/anhgelus/les-copaings-bot/config"
|
"github.com/anhgelus/les-copaings-bot/config"
|
||||||
|
@ -9,7 +8,6 @@ import (
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type cXP struct {
|
type cXP struct {
|
||||||
|
@ -61,16 +59,14 @@ func (c *Copaing) GetXP() (uint, error) {
|
||||||
|
|
||||||
func (c *Copaing) GetXPForDays(n uint) (uint, error) {
|
func (c *Copaing) GetXPForDays(n uint) (uint, error) {
|
||||||
xp := uint(0)
|
xp := uint(0)
|
||||||
var y, d int
|
|
||||||
var m time.Month
|
|
||||||
if gokord.Debug {
|
|
||||||
y, m, d = time.Unix(time.Now().Unix()-int64(24*60*60), 0).Date() // reduce time for debug
|
|
||||||
} else {
|
|
||||||
y, m, d = time.Unix(time.Now().Unix()-int64(n*24*60*60), 0).Date()
|
|
||||||
}
|
|
||||||
rows, err := gokord.DB.
|
rows, err := gokord.DB.
|
||||||
Model(&CopaingXP{}).
|
Model(&CopaingXP{}).
|
||||||
Where(fmt.Sprintf("created_at >= '%d-%d-%d' and guild_id = ? and copaing_id = ?", y, m, d), c.GuildID, c.ID).
|
Where(
|
||||||
|
"created_at >= '?' and guild_id = ? and copaing_id = ?",
|
||||||
|
exp.TimeStampNDaysBefore(n),
|
||||||
|
c.GuildID,
|
||||||
|
c.ID,
|
||||||
|
).
|
||||||
Rows()
|
Rows()
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue