From e80489e55c7dd0f457c348728348beadbed1a5e7 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Nov 2026 02:44:09 +0000 Subject: [PATCH] fix(analytics): make the trending windows contiguous Hourly rows are kept for 7 days, so the previous week came from daily rollups that ended at a day boundary. Downloads between that boundary and the start of the recent week counted in neither. The boundary day is now its rollup minus the hourly rows after the boundary. --- pkg/metadata/gormstore/trending.go | 40 ++++++++++++++++++++----- pkg/metadata/gormstore/trending_test.go | 29 +++++++++++++++++- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/pkg/metadata/gormstore/trending.go b/pkg/metadata/gormstore/trending.go index 11a92e3..065c88b 100644 --- a/pkg/metadata/gormstore/trending.go +++ b/pkg/metadata/gormstore/trending.go @@ -22,15 +22,22 @@ type trendingRow struct { } // GetTrendingPackages ranks packages by downloads in the last 7 days, with the 7 days before -// for the change. Downloads sit in raw events (current hour), hourly rows (last 7 days) and -// daily rollups (older), the same layering GetTimeSeriesStats merges. +// for the change. Downloads sit in raw events (current hour), hourly rows (kept 7 days) and +// daily rollups (older), the same layering GetTimeSeriesStats merges. The previous window ends +// exactly where the recent one starts: whole days come from the rollups, and the day that +// holds the boundary is its rollup minus the hourly rows after the boundary. func (s *GORMStoreV2) GetTrendingPackages(ctx context.Context, limit int) ([]*metadata.TrendingPackage, error) { now := s.clock().UTC() - since := now.Add(-trendingWindow) - // Daily rows hold whole days, so the previous window ends at the day boundary at or before since. + since := now.Add(-trendingWindow).Truncate(time.Hour) + boundaryDay := since.Truncate(24 * time.Hour) + // Whole days only: pick the count that keeps the previous window closest to 7 days. + fullDays := 7 + if since.Sub(boundaryDay) >= 12*time.Hour { + fullDays = 6 + } // The 12h shift keeps midnight rows on the right side whether the driver stores a date or a timestamp. - prevEnd := since.Truncate(24 * time.Hour).Add(-12 * time.Hour) - prevStart := prevEnd.Add(-trendingWindow) + shift := 12 * time.Hour + fullFrom, fullTo := boundaryDay.AddDate(0, 0, -fullDays).Add(-shift), boundaryDay.Add(-shift) hourly, err := s.packageDownloads(ctx, "download_stats_hourly s", "SUM(s.download_count)", "s.time_bucket >= ?", since) if err != nil { @@ -40,10 +47,29 @@ func (s *GORMStoreV2) GetTrendingPackages(ctx context.Context, limit int) ([]*me if err != nil { return nil, err } - daily, err := s.packageDownloads(ctx, "download_stats_daily s", "SUM(s.download_count)", "s.time_bucket >= ? AND s.time_bucket < ?", prevStart, prevEnd) + daily, err := s.packageDownloads(ctx, "download_stats_daily s", "SUM(s.download_count)", "s.time_bucket >= ? AND s.time_bucket < ?", fullFrom, fullTo) if err != nil { return nil, err } + partialDay, err := s.packageDownloads(ctx, "download_stats_daily s", "SUM(s.download_count)", "s.time_bucket >= ? AND s.time_bucket < ?", fullTo, boundaryDay.Add(shift)) + if err != nil { + return nil, err + } + afterBoundary, err := s.packageDownloads(ctx, "download_stats_hourly s", "SUM(s.download_count)", "s.time_bucket >= ? AND s.time_bucket < ?", since, boundaryDay.Add(24*time.Hour)) + if err != nil { + return nil, err + } + // Hourly rows trail the rollup, so the difference can dip below zero. + after := make(map[trendingRow]int64, len(afterBoundary)) + for _, r := range afterBoundary { + after[trendingRow{Registry: r.Registry, Name: r.Name, Version: r.Version}] += r.Count + } + for _, r := range partialDay { + left := r.Count - after[trendingRow{Registry: r.Registry, Name: r.Name, Version: r.Version}] + if left > 0 { + daily = append(daily, trendingRow{Registry: r.Registry, Name: r.Name, Version: r.Version, Count: left}) + } + } totals := make(map[trendingKey]*metadata.TrendingPackage) add := func(rows []trendingRow, recent bool) { diff --git a/pkg/metadata/gormstore/trending_test.go b/pkg/metadata/gormstore/trending_test.go index 891cc2b..45d94d0 100644 --- a/pkg/metadata/gormstore/trending_test.go +++ b/pkg/metadata/gormstore/trending_test.go @@ -70,7 +70,7 @@ func TestTrendingExcludesDownloadsOutsideBothWindows(t *testing.T) { store := trendingStore(t) pad := trendingPackage(t, store, "npm", "left-pad", "1.3.0") trendingStat(t, store, &DownloadStatsDailyModel{}, pad, time.Date(2026, 11, 5, 0, 0, 0, 0, time.UTC), 50) - trendingStat(t, store, &DownloadStatsDailyModel{}, pad, time.Date(2026, 11, 17, 0, 0, 0, 0, time.UTC), 60) + trendingStat(t, store, &DownloadStatsDailyModel{}, pad, time.Date(2026, 11, 9, 0, 0, 0, 0, time.UTC), 60) trendingEvents(t, store, pad, trendingNow.Add(-time.Minute), 1) got, err := store.GetTrendingPackages(context.Background(), 10) @@ -143,3 +143,30 @@ func TestDailyTimeIndexExists(t *testing.T) { store := trendingStore(t) require.True(t, store.db.Migrator().HasIndex(&DownloadStatsDailyModel{}, dailyTimeIndex)) } + +// The day that holds the boundary is split: its rollup minus the hourly rows after the boundary. +func TestTrendingCountsTheBoundaryDayOnce(t *testing.T) { + store := trendingStore(t) + pad := trendingPackage(t, store, "npm", "left-pad", "1.3.0") + // 5 downloads on Nov 17; 2 of them at 12:00, after the boundary at 10:00. + trendingStat(t, store, &DownloadStatsDailyModel{}, pad, time.Date(2026, 11, 17, 0, 0, 0, 0, time.UTC), 5) + trendingStat(t, store, &DownloadStatsHourlyModel{}, pad, time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC), 2) + + got, err := store.GetTrendingPackages(context.Background(), 10) + require.NoError(t, err) + require.Equal(t, []*metadata.TrendingPackage{{Registry: "npm", Name: "left-pad", Recent: 2, Previous: 3}}, got) +} + +// Past noon the previous window takes six whole days, so it stays near 7 days long. +func TestTrendingPreviousWindowStaysNearAWeek(t *testing.T) { + store := trendingStore(t) + store.now = func() time.Time { return time.Date(2026, 11, 24, 14, 30, 0, 0, time.UTC) } + pad := trendingPackage(t, store, "npm", "left-pad", "1.3.0") + trendingEvents(t, store, pad, store.now().Add(-time.Minute), 1) + trendingStat(t, store, &DownloadStatsDailyModel{}, pad, time.Date(2026, 11, 10, 0, 0, 0, 0, time.UTC), 40) + trendingStat(t, store, &DownloadStatsDailyModel{}, pad, time.Date(2026, 11, 11, 0, 0, 0, 0, time.UTC), 6) + + got, err := store.GetTrendingPackages(context.Background(), 10) + require.NoError(t, err) + require.Equal(t, int64(6), got[0].Previous) +}