mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +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.
124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
package file
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
|
)
|
|
|
|
// packageKey identifies a file-store record, which has no numeric id.
|
|
func packageKey(p *metadata.Package) string {
|
|
return p.Registry + "/" + p.Name + "@" + p.Version
|
|
}
|
|
|
|
// listRecords returns package records only: the store's walk also yields scan result files.
|
|
func (s *Store) listRecords(ctx context.Context) ([]*metadata.Package, error) {
|
|
all, err := s.ListPackages(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := all[:0]
|
|
for _, p := range all {
|
|
if p.Registry != "" && p.Name != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ListPackageGroups pages groups in memory.
|
|
func (s *Store) ListPackageGroups(ctx context.Context, opts metadata.GroupListOptions) (*metadata.GroupPage, error) {
|
|
all, err := s.listRecords(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, p := range all {
|
|
if p.ID == "" {
|
|
p.ID = packageKey(p)
|
|
}
|
|
}
|
|
return metadata.PageGroups(all, opts), nil
|
|
}
|
|
|
|
// GetScanSummaries reads each scanned package's latest scan file.
|
|
func (s *Store) GetScanSummaries(ctx context.Context, packageIDs []string, withDetails bool) ([]*metadata.ScanSummary, error) {
|
|
all, err := s.listRecords(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
want := make(map[string]struct{}, len(packageIDs))
|
|
for _, id := range packageIDs {
|
|
want[id] = struct{}{}
|
|
}
|
|
var out []*metadata.ScanSummary
|
|
for _, p := range all {
|
|
id := p.ID
|
|
if id == "" {
|
|
id = packageKey(p)
|
|
}
|
|
if _, ok := want[id]; packageIDs != nil && !ok || !p.SecurityScanned {
|
|
continue
|
|
}
|
|
sum := &metadata.ScanSummary{
|
|
Registry: p.Registry, PackageID: id, Name: p.Name, Version: p.Version, ReleaseVersion: p.ReleaseVersion,
|
|
}
|
|
res, err := s.GetScanResult(ctx, p.Registry, p.Name, p.Version)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if res != nil {
|
|
sum.HasScan = true
|
|
sum.Status = res.Status
|
|
sum.ScannedAt = res.ScannedAt
|
|
sum.Total = res.VulnerabilityCount
|
|
sum.Details = res.Details
|
|
for _, v := range res.Vulnerabilities {
|
|
switch metadata.NormalizeSeverity(v.Severity) {
|
|
case "CRITICAL":
|
|
sum.Critical++
|
|
case "HIGH":
|
|
sum.High++
|
|
case "MODERATE":
|
|
sum.Moderate++
|
|
default:
|
|
sum.Low++
|
|
}
|
|
}
|
|
if withDetails {
|
|
sum.Vulnerabilities = res.Vulnerabilities
|
|
}
|
|
}
|
|
out = append(out, sum)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// GetRegistryStats totals artifacts per registry.
|
|
func (s *Store) GetRegistryStats(ctx context.Context) ([]metadata.RegistryStat, error) {
|
|
all, err := s.listRecords(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byRegistry := map[string]*metadata.RegistryStat{}
|
|
var order []string
|
|
for _, p := range all {
|
|
if metadata.IsMetadataVersion(p.Version) {
|
|
continue
|
|
}
|
|
st, ok := byRegistry[p.Registry]
|
|
if !ok {
|
|
st = &metadata.RegistryStat{Registry: p.Registry}
|
|
byRegistry[p.Registry] = st
|
|
order = append(order, p.Registry)
|
|
}
|
|
st.Count++
|
|
st.Size += p.Size
|
|
st.Downloads += p.DownloadCount
|
|
}
|
|
out := make([]metadata.RegistryStat, 0, len(order))
|
|
for _, r := range order {
|
|
out = append(out, *byRegistry[r])
|
|
}
|
|
return out, nil
|
|
}
|