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
+48
View File
@@ -0,0 +1,48 @@
package metadata
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCleanName(t *testing.T) {
require.Equal(t, "github.com/a/b", CleanName("github.com/a/b/@v/v1.0.0.zip"))
require.Equal(t, "left-pad", CleanName("left-pad"))
require.Equal(t, "@scope/pkg", CleanName("@scope/pkg"))
}
func TestDedupeVersionsKeepsLargestAndRenames(t *testing.T) {
rows := []*Package{
{Registry: "go", Name: "m/@v/v1.zip", Version: "v1", Size: 5},
{Registry: "go", Name: "m/@v/v1.mod", Version: "v1", Size: 50},
{Registry: "go", Name: "m/@v/v1.info", Version: "v1", Size: 1},
{Registry: "npm", Name: "m", Version: "v1", Size: 9},
}
got := DedupeVersions(rows)
require.Len(t, got, 2, "the same name in another registry is a different package")
require.Equal(t, "m", got[0].Name)
require.EqualValues(t, 50, got[0].Size)
require.Equal(t, "m/@v/v1.zip", rows[0].Name, "input rows stay untouched")
}
func TestPageGroupsPagesWholeGroups(t *testing.T) {
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
rows := []*Package{
{Registry: "npm", Name: "b", Version: "1", CachedAt: at.Add(1 * time.Hour)},
{Registry: "npm", Name: "b", Version: "2", CachedAt: at.Add(5 * time.Hour)},
{Registry: "npm", Name: "a", Version: "1", CachedAt: at.Add(2 * time.Hour)},
{Registry: "npm", Name: "c", Version: "list", CachedAt: at},
}
page := PageGroups(rows, GroupListOptions{Page: 1, PageSize: 1})
require.EqualValues(t, 2, page.TotalGroups, "the list page forms no group")
require.Len(t, page.Packages, 1)
require.Equal(t, "a", page.Packages[0].Name)
recent := PageGroups(rows, GroupListOptions{SortBy: GroupSortCachedAt, SortDesc: true, Page: 1, PageSize: 1})
require.Equal(t, []string{"2", "1"}, []string{recent.Packages[0].Version, recent.Packages[1].Version}, "group b, newest version first")
require.Empty(t, PageGroups(rows, GroupListOptions{Page: 9, PageSize: 1}).Packages)
require.Len(t, PageGroups(rows, GroupListOptions{Query: "B"}).Packages, 2)
}