Skip to content

Commit

Permalink
Optimize user points fetching.
Browse files Browse the repository at this point in the history
Greatly speeds up $roulette and $points commands.
airforce270 committed Sep 20, 2023
1 parent 50bed38 commit 9f74f70
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions commands/gamba/gamba.go
Original file line number Diff line number Diff line change
@@ -563,15 +563,9 @@ func roulette(msg *base.IncomingMessage, args []arg.Arg) ([]*base.Message, error
}

func fetchUserPoints(db *gorm.DB, user models.User) (int64, error) {
var transactions []*models.GambaTransaction
if err := db.Where(models.GambaTransaction{UserID: user.ID}).Find(&transactions).Error; err != nil {
return 0, fmt.Errorf("failed to fetch points for user %d: %w", user.ID, err)
}

var points int64
for _, txn := range transactions {
points += txn.Delta
if err := db.Model(&models.GambaTransaction{}).Select("COALESCE(SUM(delta), 0)").Where(models.GambaTransaction{UserID: user.ID}).Scan(&points).Error; err != nil {
return 0, fmt.Errorf("failed to fetch points for user %d: %w", user.ID, err)
}

return points, nil
}

0 comments on commit 9f74f70

Please sign in to comment.