Files
gohoarder/pkg/metadata/gormstore/dashboard_pg_test.go
T
lukaszraczylo db5ca2539f perf(api): serve package lists and stats in constant queries
The dashboard waited seconds for /api/packages and /api/stats. Both
handlers ran queries per package, so the cost grew with the cache.

Baseline, measured live through a port-forward (median of 5):
- /api/packages: 3033 ms, 594 items, limit ignored
- /api/packages?limit=50&offset=0: 2813 ms
- /api/stats: 3813 ms
- /api/stats/timeseries?period=1day: 12 ms

Baseline queries per request, counted on SQLite with 20 and 120 rows:
- /api/packages: 141 and 841 queries (7 per package)
- /api/stats: 103 and 603 queries (5 per package)

Both handlers now run a constant number of queries: 4 for the full list,
6 for a page, 6 for stats. The query-count tests fail on the old code.

- /api/packages pages (registry, name) groups with page, page_size,
  registry, q, sort and order, and returns total_groups. Without page
  and page_size it returns every package, so old clients keep working.
- A batched scan lookup replaces the per-package GetScanResult and
  CheckVulnerabilities calls. ScanDecisions returns the same verdicts as
  CheckVulnerabilities, proven by a policy and bypass matrix test.
- The silent 1000-row cap is gone from the list and the Go delete.
- /api/stats sums per registry with GROUP BY and judges blocked
  packages in one batch.
- Migration 202611240001 adds scan_results (package_id, scanned_at) on
  SQLite, PostgreSQL and MySQL. The existing (registry_id, name, version)
  index already serves the group lookups.
2026-11-23 13:26:50 +00:00

51 lines
1.5 KiB
Go

//go:build pgintegration
package gormstore
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func TestPostgresDashboardQueries(t *testing.T) {
store, _ := newPostgresStore(t)
reset := func(t *testing.T) {
t.Helper()
require.NoError(t, store.db.Exec("TRUNCATE packages RESTART IDENTITY CASCADE").Error)
}
for name, check := range map[string]func(*testing.T, *GORMStoreV2){
"groups match the reference": checkListPackageGroupsMatchesReference,
"group behaviour": checkListPackageGroupsBehaviour,
"statement count is constant": checkDashboardStatementsAreConstant,
"scan summaries": checkScanSummaries,
"registry stats": checkRegistryStats,
} {
t.Run(name, func(t *testing.T) {
reset(t)
check(t, store)
})
}
}
// With sequential scans disabled the latest-scan lookup must run on the new index.
func TestPostgresLatestScanLookupUsesIndex(t *testing.T) {
store, _ := newPostgresStore(t)
seedScanned(t, store, 0, 20)
var plan []string
require.NoError(t, store.db.Connection(func(tx *gorm.DB) error {
if err := tx.Exec("SET enable_seqscan = off").Error; err != nil {
return err
}
return tx.Raw(`EXPLAIN SELECT id, package_id FROM scan_results
WHERE package_id IN (1, 2, 3)
AND scanned_at = (SELECT MAX(x.scanned_at) FROM scan_results x WHERE x.package_id = scan_results.package_id AND x.deleted_at IS NULL)
AND deleted_at IS NULL`).Scan(&plan).Error
}))
require.Contains(t, strings.Join(plan, "\n"), scanPackageScannedAtIndex)
}