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.
This commit is contained in:
2026-11-23 13:26:50 +00:00
parent a7b9917696
commit db5ca2539f
24 changed files with 2049 additions and 244 deletions
+54
View File
@@ -82,3 +82,57 @@ func TestCleanupExpiredBypasses_DoesNotFollowSymlinkOutsideStore(t *testing.T) {
require.Error(t, err, "escaping symlink must be refused, not read")
require.FileExists(t, target)
}
func TestListPackagesZeroLimitMeansNoLimit(t *testing.T) {
store, err := New(Config{Path: t.TempDir()})
require.NoError(t, err)
ctx := context.Background()
for _, v := range []string{"1", "2", "3"} {
require.NoError(t, store.SavePackage(ctx, &metadata.Package{Registry: "npm", Name: "p", Version: v}))
}
got, err := store.ListPackages(ctx, &metadata.ListOptions{Offset: 1})
require.NoError(t, err)
require.Len(t, got, 2)
}
func TestFileStoreDashboardQueries(t *testing.T) {
store, err := New(Config{Path: t.TempDir()})
require.NoError(t, err)
ctx := context.Background()
now := time.Now()
for _, p := range []*metadata.Package{
{Registry: "npm", Name: "a", Version: "1", Size: 10, DownloadCount: 2, SecurityScanned: true, CachedAt: now},
{Registry: "npm", Name: "a", Version: "list", Size: 1, CachedAt: now},
{Registry: "go", Name: "m", Version: "v1", Size: 30, DownloadCount: 1, CachedAt: now},
} {
require.NoError(t, store.SavePackage(ctx, p))
}
require.NoError(t, store.SaveScanResult(ctx, &metadata.ScanResult{
Registry: "npm", PackageName: "a", PackageVersion: "1", Scanner: "t", Status: metadata.ScanStatusVulnerable, ScannedAt: now,
VulnerabilityCount: 1, Vulnerabilities: []metadata.Vulnerability{{ID: "CVE-1", Severity: "high"}},
}))
page, err := store.ListPackageGroups(ctx, metadata.GroupListOptions{Page: 1, PageSize: 10})
require.NoError(t, err)
require.EqualValues(t, 2, page.TotalGroups)
require.Len(t, page.Packages, 2)
sums, err := store.GetScanSummaries(ctx, nil, true)
require.NoError(t, err)
require.Len(t, sums, 1)
require.True(t, sums[0].HasScan)
require.Equal(t, 1, sums[0].High)
require.Len(t, sums[0].Vulnerabilities, 1)
byID, err := store.GetScanSummaries(ctx, []string{sums[0].PackageID}, false)
require.NoError(t, err)
require.Len(t, byID, 1)
require.Nil(t, byID[0].Vulnerabilities)
stats, err := store.GetRegistryStats(ctx)
require.NoError(t, err)
require.ElementsMatch(t, []metadata.RegistryStat{
{Registry: "npm", Count: 1, Size: 10, Downloads: 2},
{Registry: "go", Count: 1, Size: 30, Downloads: 1},
}, stats)
}