This commit is contained in:
Pijus Kamandulis 2025-07-05 08:44:52 +03:00
parent effe887b3b
commit ac0285c2e6
2 changed files with 12 additions and 11 deletions

View File

@ -216,13 +216,12 @@ func GetTimeWindowHighShares(db *clover.DB) []models.TimeWindowHighShare {
} }
func SetTimeWindowHighShare(db *clover.DB, share models.TimeWindowHighShare) error { func SetTimeWindowHighShare(db *clover.DB, share models.TimeWindowHighShare) error {
doc := document.NewDocumentOf(&share)
db.Delete( db.Delete(
c.NewQuery(TimeWindowHighShareCollectionName). c.NewQuery(TimeWindowHighShareCollectionName).
Where(c.Field("TimeWindowID"). Where(c.Field("TimeWindowID").
Eq(share.TimeWindowID))) Eq(share.TimeWindowID)))
doc := document.NewDocumentOf(&share)
db.InsertOne(TimeWindowHighShareCollectionName, doc) db.InsertOne(TimeWindowHighShareCollectionName, doc)
return nil return nil
@ -261,12 +260,16 @@ func GetDailyStats(db *clover.DB, date time.Time) (*models.DailyStats, error) {
existingDoc, err := db.FindFirst(c.NewQuery(DailyStatsCollectionName). existingDoc, err := db.FindFirst(c.NewQuery(DailyStatsCollectionName).
Where(c.Field("Date").Eq(dateStr))) Where(c.Field("Date").Eq(dateStr)))
if err == nil && existingDoc != nil { if err == nil && existingDoc != nil {
if existingDoc.ExpiresAt().After(time.Now()) {
DeleteDailyStatsForDay(db, date)
} else {
var stats models.DailyStats var stats models.DailyStats
if err := existingDoc.Unmarshal(&stats); err != nil { if err := existingDoc.Unmarshal(&stats); err != nil {
return nil, fmt.Errorf("failed to unmarshal daily stats: %v", err) return nil, fmt.Errorf("failed to unmarshal daily stats: %v", err)
} }
return &stats, nil return &stats, nil
} }
}
// Get shares in range // Get shares in range
since := date.Truncate(24 * time.Hour) since := date.Truncate(24 * time.Hour)

View File

@ -21,15 +21,13 @@ func (job *RecalculateCurrentDayStatsJob) Run() error {
defer ticker.Stop() defer ticker.Stop()
for { for {
select { <-ticker.C
case <-ticker.C:
job.recalculateCurrentDayStats() job.recalculateCurrentDayStats()
} }
}
} }
func (job *RecalculateCurrentDayStatsJob) recalculateCurrentDayStats() { func (job *RecalculateCurrentDayStatsJob) recalculateCurrentDayStats() {
today := time.Now().Truncate(24 * time.Hour) today := time.Now()
yesterday := today.Add(-24 * time.Hour) yesterday := today.Add(-24 * time.Hour)
database.DeleteDailyStatsForDay(job.DB, today) database.DeleteDailyStatsForDay(job.DB, today)