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
+25 -3
View File
@@ -887,10 +887,29 @@ Access the web dashboard at **http://localhost:8080**
**List Packages:**
```bash
GET /api/packages
GET /api/packages?registry=npm
GET /api/packages?status=vulnerable
GET /api/packages?page=1&page_size=25
GET /api/packages?registry=npm&q=axios&page_size=25
GET /api/packages?sort=cached_at&order=desc&page_size=10
```
The dashboard groups versions under a package, so paging counts packages (a
registry and name pair), not rows. Without `page` and `page_size` the call
returns every package in one response, with no row cap.
| Parameter | Meaning |
|---|---|
| `page` | 1-based page of groups. Default 1. |
| `page_size` | Groups per page. Default 25 when `page` is set, at most 200. |
| `registry` | Only this registry, for example `npm`, `pypi` or `go`. |
| `q` | Case-insensitive search in name, version and release version. |
| `sort` | `name` (default, ascending), `cached_at`, `downloads` or `size` (both descending). |
| `order` | `asc` or `desc`, to override the default direction of `sort`. |
`packages` holds every version row of each group on the page, in group order
and newest first within a group. `total` is the number of rows returned.
`total_groups` is the number of groups that match the filters across all pages.
A paged response also echoes `page` and `page_size`.
**Package Details:**
```bash
GET /api/packages/:registry/:name/:version
@@ -920,7 +939,10 @@ GET /api/packages/:registry/:name/:version
}
}
],
"total": 42
"total": 1,
"total_groups": 42,
"page": 1,
"page_size": 25
}
```