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.
123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package metadata
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// IsMetadataVersion reports whether version names a metadata page rather than an artifact.
|
|
func IsMetadataVersion(version string) bool {
|
|
for _, v := range MetadataVersions {
|
|
if version == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// MatchesQuery reports whether a package matches a case-insensitive search on name, version or release version.
|
|
func MatchesQuery(p *Package, query string) bool {
|
|
q := strings.ToLower(strings.TrimSpace(query))
|
|
if q == "" {
|
|
return true
|
|
}
|
|
return strings.Contains(strings.ToLower(p.Name), q) ||
|
|
strings.Contains(strings.ToLower(p.Version), q) ||
|
|
strings.Contains(strings.ToLower(p.ReleaseVersion), q)
|
|
}
|
|
|
|
type groupAgg struct {
|
|
registry, name string
|
|
rows []*Package
|
|
last int64
|
|
size, dl int64
|
|
}
|
|
|
|
// PageGroups filters, groups, sorts and pages rows in memory. It is the reference
|
|
// behaviour that SQL backends of ListPackageGroups must match.
|
|
func PageGroups(all []*Package, opts GroupListOptions) *GroupPage {
|
|
groups := map[string]*groupAgg{}
|
|
for _, p := range all {
|
|
if IsMetadataVersion(p.Version) || (opts.Registry != "" && p.Registry != opts.Registry) || !MatchesQuery(p, opts.Query) {
|
|
continue
|
|
}
|
|
name := CleanName(p.Name)
|
|
key := p.Registry + "\x00" + name
|
|
g, ok := groups[key]
|
|
if !ok {
|
|
g = &groupAgg{registry: p.Registry, name: name}
|
|
groups[key] = g
|
|
}
|
|
if ts := p.CachedAt.UnixNano(); ts > g.last {
|
|
g.last = ts
|
|
}
|
|
g.size += p.Size
|
|
g.dl += p.DownloadCount
|
|
g.rows = append(g.rows, p)
|
|
}
|
|
|
|
ordered := make([]*groupAgg, 0, len(groups))
|
|
for _, g := range groups {
|
|
ordered = append(ordered, g)
|
|
}
|
|
sort.Slice(ordered, func(i, j int) bool {
|
|
a, b := ordered[i], ordered[j]
|
|
var cmp int
|
|
switch opts.SortBy {
|
|
case GroupSortCachedAt:
|
|
cmp = compareInt(a.last, b.last)
|
|
case GroupSortDownloads:
|
|
cmp = compareInt(a.dl, b.dl)
|
|
case GroupSortSize:
|
|
cmp = compareInt(a.size, b.size)
|
|
}
|
|
if cmp != 0 {
|
|
if opts.SortDesc {
|
|
return cmp > 0
|
|
}
|
|
return cmp < 0
|
|
}
|
|
if c := strings.Compare(strings.ToLower(a.name), strings.ToLower(b.name)); c != 0 {
|
|
if opts.SortBy == "" || opts.SortBy == GroupSortName {
|
|
if opts.SortDesc {
|
|
return c > 0
|
|
}
|
|
}
|
|
return c < 0
|
|
}
|
|
return a.registry < b.registry
|
|
})
|
|
|
|
page := &GroupPage{TotalGroups: int64(len(ordered))}
|
|
if opts.PageSize > 0 {
|
|
pageNo := opts.Page
|
|
if pageNo < 1 {
|
|
pageNo = 1
|
|
}
|
|
start := (pageNo - 1) * opts.PageSize
|
|
if start >= len(ordered) {
|
|
return page
|
|
}
|
|
end := start + opts.PageSize
|
|
if end > len(ordered) {
|
|
end = len(ordered)
|
|
}
|
|
ordered = ordered[start:end]
|
|
}
|
|
for _, g := range ordered {
|
|
sort.SliceStable(g.rows, func(i, j int) bool { return g.rows[i].CachedAt.After(g.rows[j].CachedAt) })
|
|
page.Packages = append(page.Packages, DedupeVersions(g.rows)...)
|
|
}
|
|
return page
|
|
}
|
|
|
|
func compareInt(a, b int64) int {
|
|
switch {
|
|
case a < b:
|
|
return -1
|
|
case a > b:
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|