Files
gohoarder/pkg/metadata/gormstore/snapshots_pg_test.go
T
lukaszraczylo 1512230ab3 feat(stats): record daily snapshots and serve a trend API
The aggregation worker now writes one row per UTC day to
stats_snapshots. The row holds package count, total size, vulnerable
package count, the four severity counts and the blocked count. The
write is an idempotent upsert per day.

A migration backfills package counts from cached_at and deleted_at as
the number of packages alive at the end of each day. Vulnerability
fields backfill from scan_results timestamps and stay empty before the
first scan. GET /api/stats/trend returns daily points for 30day, 90day
or 1year with a constant number of queries.

The Stats page shows a chart of cached and vulnerable packages with a
period selector and severity toggles. The Dashboard shows a compact
version. The Lean model gains theorems for the backfill and upsert.
2026-11-23 14:55:19 +00:00

44 lines
1.3 KiB
Go

//go:build pgintegration
package gormstore
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPostgresTrendSeverityAndPurge(t *testing.T) {
store, _ := newPostgresStore(t)
reset := func(t *testing.T) {
t.Helper()
require.NoError(t, store.db.Exec(
"TRUNCATE packages, stats_snapshots, download_stats_daily RESTART IDENTITY CASCADE").Error)
}
for name, check := range map[string]func(*testing.T, *GORMStoreV2){
"backfill": checkBackfillStatsSnapshots,
"backfill without scans": checkBackfillWithoutScans,
"snapshot": checkSnapshotStats,
"trend window": checkTrendWindow,
"trend statements constant": checkTrendStatementsAreConstant,
"severity filter": checkSeverityFilter,
"severity statements constant": checkSeverityFilterStatementsAreConstant,
} {
t.Run(name, func(t *testing.T) {
reset(t)
check(t, store)
})
}
}
func TestPostgresStatsSnapshotsHaveNoPackageForeignKey(t *testing.T) {
store, _ := newPostgresStore(t)
var referencing []string
require.NoError(t, store.db.Raw(
"SELECT conrelid::regclass::text FROM pg_constraint WHERE contype = 'f' AND confrelid = 'packages'::regclass",
).Scan(&referencing).Error)
require.NotContains(t, referencing, "stats_snapshots", "a package purge must not cascade over history")
}