Recalculate yesterday's and today's stats

This commit is contained in:
Pijus Kamandulis
2025-06-24 09:41:24 +03:00
parent b89a1a2a7e
commit 844f7fa08b
4 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package jobs
import (
"pool-stats/constants"
"pool-stats/database"
"time"
"github.com/ostafen/clover/v2"
)
type RecalculateCurrentDayStatsJob struct {
DB *clover.DB
}
func NewRecalculateCurrentDayStatsJob(db *clover.DB) *RecalculateCurrentDayStatsJob {
return &RecalculateCurrentDayStatsJob{DB: db}
}
func (job *RecalculateCurrentDayStatsJob) Run() error {
ticker := time.NewTicker(constants.RecalculateCurrentDayStatsJobInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
job.recalculateCurrentDayStats()
}
}
}
func (job *RecalculateCurrentDayStatsJob) recalculateCurrentDayStats() {
today := time.Now().Truncate(24 * time.Hour)
yesterday := today.Add(-24 * time.Hour)
database.GetDailyStats(job.DB, today)
// Need to keep yesterday's stats cache updated
database.DeleteDailyStatsForDay(job.DB, yesterday)
database.GetDailyStats(job.DB, yesterday)
}