mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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.
96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/lukaszraczylo/gohoarder/internal/dbcount"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/config"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata/gormstore"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// seedScanned stores n scanned npm packages, every fifth with a critical finding.
|
|
func seedScanned(t *testing.T, a *App, from, n int) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
for i := from; i < from+n; i++ {
|
|
name := fmt.Sprintf("pkg-%03d", i)
|
|
err := a.metadata.SavePackage(ctx, &metadata.Package{
|
|
Registry: "npm", Name: name, Version: "1.0.0", StorageKey: "npm/" + name + "/1.0.0",
|
|
Size: 100, CachedAt: now.Add(-time.Duration(i) * time.Minute), LastAccessed: now,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SavePackage: %v", err)
|
|
}
|
|
res := &metadata.ScanResult{
|
|
Registry: "npm", PackageName: name, PackageVersion: "1.0.0", Scanner: "test",
|
|
Status: metadata.ScanStatusClean, ScannedAt: now,
|
|
}
|
|
if i%5 == 0 {
|
|
res.Status = metadata.ScanStatusVulnerable
|
|
res.VulnerabilityCount = 1
|
|
res.Vulnerabilities = []metadata.Vulnerability{{ID: fmt.Sprintf("CVE-2026-%04d", i), Severity: "critical"}}
|
|
}
|
|
if err := a.metadata.SaveScanResult(ctx, res); err != nil {
|
|
t.Fatalf("SaveScanResult: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func newScanningApp(t *testing.T) (*App, *dbcount.Counter) {
|
|
t.Helper()
|
|
a := newTestApp(t, func(c *config.Config) {
|
|
c.Security.Enabled = true
|
|
c.Security.Scanners.Static.Enabled = false
|
|
c.Security.UpdateDBOnStartup = false
|
|
})
|
|
store, ok := a.metadata.(*gormstore.GORMStoreV2)
|
|
if !ok {
|
|
t.Fatalf("metadata store is %T, want *gormstore.GORMStoreV2", a.metadata)
|
|
}
|
|
var counter *dbcount.Counter
|
|
store.WrapDB(func(db *gorm.DB) *gorm.DB {
|
|
counted, c := dbcount.Attach(db)
|
|
counter = c
|
|
return counted
|
|
})
|
|
return a, counter
|
|
}
|
|
|
|
// queriesFor reports how many SQL statements one GET runs.
|
|
func queriesFor(t *testing.T, a *App, c *dbcount.Counter, target string) int {
|
|
t.Helper()
|
|
c.Reset()
|
|
if code, _, body := doRequest(t, a, target, nil); code != http.StatusOK {
|
|
t.Fatalf("GET %s = %d %s", target, code, body)
|
|
}
|
|
return c.Count()
|
|
}
|
|
|
|
// The dashboard endpoints must cost the same number of statements for 20 rows and for 120.
|
|
func TestDashboardEndpointsQueryCountIsConstant(t *testing.T) {
|
|
a, counter := newScanningApp(t)
|
|
targets := []string{"/api/packages", "/api/packages?page=1&page_size=25", "/api/stats"}
|
|
|
|
seedScanned(t, a, 0, 20)
|
|
small := map[string]int{}
|
|
for _, target := range targets {
|
|
small[target] = queriesFor(t, a, counter, target)
|
|
}
|
|
|
|
seedScanned(t, a, 20, 100)
|
|
for _, target := range targets {
|
|
large := queriesFor(t, a, counter, target)
|
|
t.Logf("%-40s 20 rows: %3d queries, 120 rows: %3d queries", target, small[target], large)
|
|
if large != small[target] {
|
|
t.Errorf("%s: %d queries for 20 rows but %d for 120 rows; the count must not grow with rows", target, small[target], large)
|
|
}
|
|
}
|
|
}
|