Recalculate yesterday's and today's stats
This commit is contained in:
40
jobs/recalculateCurrentDayStatsJob.go
Normal file
40
jobs/recalculateCurrentDayStatsJob.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user