mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
fix(stats): keep downloads that a daily rollup has not counted yet
The time series let a day's rollup replace the hourly rows and events for that day. A download aggregated after the rollup ran vanished from the chart until the next pass. The chart now takes the larger of the two figures.
This commit is contained in:
@@ -24,8 +24,8 @@ type GORMStoreV2 struct {
|
|||||||
registryCache map[string]int32 // Cache registry name -> ID mapping
|
registryCache map[string]int32 // Cache registry name -> ID mapping
|
||||||
aggregationWorker *AggregationWorker
|
aggregationWorker *AggregationWorker
|
||||||
partitionManager *PartitionManager
|
partitionManager *PartitionManager
|
||||||
registryCacheMu sync.RWMutex // Protects registryCache for concurrent access
|
|
||||||
now func() time.Time // nil means time.Now
|
now func() time.Time // nil means time.Now
|
||||||
|
registryCacheMu sync.RWMutex // Protects registryCache for concurrent access
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewV2 creates a new GORM-based metadata store with V2 schema
|
// NewV2 creates a new GORM-based metadata store with V2 schema
|
||||||
@@ -764,13 +764,17 @@ func (s *GORMStoreV2) GetTimeSeriesStats(ctx context.Context, period string, reg
|
|||||||
}
|
}
|
||||||
|
|
||||||
if daily {
|
if daily {
|
||||||
// A completed day's rollup is authoritative over its leftover hourly rows.
|
|
||||||
rollups, err := s.aggregateRows(ctx, "download_stats_daily", since, now, registryID)
|
rollups, err := s.aggregateRows(ctx, "download_stats_daily", since, now, registryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Hourly rows and events only ever undercount a day, so the larger of the
|
||||||
|
// two is the better figure; a rollup alone covers days already trimmed.
|
||||||
for _, r := range rollups {
|
for _, r := range rollups {
|
||||||
counts[r.Timestamp.UTC().Truncate(bucketDuration).Unix()] = r.Value
|
day := r.Timestamp.UTC().Truncate(bucketDuration).Unix()
|
||||||
|
if r.Value > counts[day] {
|
||||||
|
counts[day] = r.Value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,3 +79,27 @@ func TestDailyRollupCoversMissedDay(t *testing.T) {
|
|||||||
|
|
||||||
require.EqualValues(t, 3, totalAt(t, store, day.Add(9*24*time.Hour+time.Hour), "30day"))
|
require.EqualValues(t, 3, totalAt(t, store, day.Add(9*24*time.Hour+time.Hour), "30day"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hourly rows and events never overcount a day, so a rollup that is behind them must not win.
|
||||||
|
func TestTimeSeriesKeepsLateDownloadsAheadOfRollup(t *testing.T) {
|
||||||
|
store := newRollupStore(t)
|
||||||
|
day := time.Date(2026, 3, 10, 0, 0, 0, 0, time.UTC)
|
||||||
|
seedEventsAt(t, store, day.Add(10*time.Hour), day.Add(23*time.Hour+30*time.Minute))
|
||||||
|
|
||||||
|
tick(t, store, day.Add(24*time.Hour+2*time.Minute), true) // rollup counts one download
|
||||||
|
tick(t, store, day.Add(25*time.Hour+2*time.Minute), true) // the 23:30 download reaches the hourly table
|
||||||
|
|
||||||
|
require.EqualValues(t, 2, totalAt(t, store, day.Add(26*time.Hour), "7day"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A rollup larger than the hourly rows still wins once the hourly rows have been trimmed.
|
||||||
|
func TestTimeSeriesKeepsRollupWhenHourlyRowsAreGone(t *testing.T) {
|
||||||
|
store := newRollupStore(t)
|
||||||
|
day := time.Date(2026, 3, 10, 0, 0, 0, 0, time.UTC)
|
||||||
|
seedEventsAt(t, store, day.Add(10*time.Hour), day.Add(11*time.Hour), day.Add(12*time.Hour))
|
||||||
|
|
||||||
|
tick(t, store, day.Add(24*time.Hour+30*time.Minute), true)
|
||||||
|
require.NoError(t, store.db.Exec("DELETE FROM download_stats_hourly WHERE time_bucket = ?", day.Add(12*time.Hour)).Error)
|
||||||
|
|
||||||
|
require.EqualValues(t, 3, totalAt(t, store, day.Add(26*time.Hour), "7day"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user