Files
gohoarder/pkg/metadata/gormstore/timeseries_test.go
T
lukaszraczylo 879ece368e fix(stats): include unaggregated downloads in the timeseries
The timeseries read only the aggregated tables. The hourly worker skips the
current hour and the daily rollup skips the current day, so fresh downloads
showed as zero. The query now merges raw download events, hourly rows and
daily rollups, with all buckets in UTC. The change in gormstore is limited to
GetTimeSeriesStats.
2026-11-23 02:29:14 +00:00

86 lines
2.6 KiB
Go

package gormstore
import (
"context"
"fmt"
"testing"
"time"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
"github.com/stretchr/testify/require"
)
func sumPoints(ts *metadata.TimeSeriesStats) int64 {
var total int64
for _, p := range ts.DataPoints {
total += p.Value
}
return total
}
func newTimeSeriesStore(t *testing.T) *GORMStoreV2 {
t.Helper()
store, err := NewV2(Config{
Driver: "sqlite",
DSN: fmt.Sprintf("file:ts_%d?mode=memory&cache=shared", time.Now().UnixNano()),
MaxOpenConns: 1,
MaxIdleConns: 1,
LogLevel: "silent",
})
require.NoError(t, err)
return store
}
func recordDownloads(t *testing.T, store *GORMStoreV2, n int) {
t.Helper()
ctx := context.Background()
now := time.Now()
require.NoError(t, store.SavePackage(ctx, &metadata.Package{
Registry: "npm", Name: "left-pad", Version: "1.3.0", StorageKey: "npm/left-pad/1.3.0",
Size: 10, CachedAt: now, LastAccessed: now,
}))
for i := 0; i < n; i++ {
require.NoError(t, store.UpdateDownloadCount(ctx, "npm", "left-pad", "1.3.0"))
}
}
func TestTimeSeriesIncludesFreshDownloads(t *testing.T) {
for _, period := range []string{"1h", "1day", "7day", "30day"} {
t.Run(period+"/before aggregation", func(t *testing.T) {
store := newTimeSeriesStore(t)
recordDownloads(t, store, 3)
ts, err := store.GetTimeSeriesStats(context.Background(), period, "")
require.NoError(t, err)
require.Equal(t, int64(3), sumPoints(ts))
})
t.Run(period+"/after aggregation", func(t *testing.T) {
store := newTimeSeriesStore(t)
recordDownloads(t, store, 3)
require.NoError(t, store.AggregateDownloadData(context.Background()))
require.NoError(t, store.AggregateDownloadData(context.Background()))
ts, err := store.GetTimeSeriesStats(context.Background(), period, "npm")
require.NoError(t, err)
require.Equal(t, int64(3), sumPoints(ts))
})
}
}
func TestTimeSeriesReadsAggregatedBuckets(t *testing.T) {
ctx := context.Background()
store := newTimeSeriesStore(t)
recordDownloads(t, store, 2)
// Move the events into completed hours so the worker aggregates them.
require.NoError(t, store.db.Exec("UPDATE download_events SET downloaded_at = ?", time.Now().UTC().Add(-3*time.Hour)).Error)
require.NoError(t, store.AggregateDownloadData(ctx))
var raw int64
require.NoError(t, store.db.Table("download_events").Count(&raw).Error)
require.Zero(t, raw, "aggregation must consume the events it counted")
for _, period := range []string{"1day", "7day"} {
ts, err := store.GetTimeSeriesStats(ctx, period, "")
require.NoError(t, err)
require.Equal(t, int64(2), sumPoints(ts), period)
}
}