feat(security): size the rescan from measured scan times

The confirmation for a full rescan used a fixed guess of 10 seconds per
artifact. Scans now record their wall time in scan_results. The new
GET /api/admin/rescan/estimate returns the artifact count of each scope
and the time at the configured concurrency, from the average of recent
scans. While a rescan runs, the status reports elapsed time and an ETA,
and the panel shows the live rate.
This commit is contained in:
2026-11-24 02:49:28 +00:00
parent e80489e55c
commit c685131865
14 changed files with 360 additions and 19 deletions
+1
View File
@@ -913,6 +913,7 @@ func (s *GORMStoreV2) SaveScanResult(ctx context.Context, result *metadata.ScanR
HighCount: highCount,
MediumCount: mediumCount,
LowCount: lowCount,
ScanDuration: result.DurationMs,
Details: JSONBField(details),
}
@@ -0,0 +1,41 @@
package gormstore
import (
"context"
"testing"
"time"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
"github.com/stretchr/testify/require"
)
func saveScan(t *testing.T, store *GORMStoreV2, name string, at time.Time, ms int) {
t.Helper()
trendingPackage(t, store, "npm", name, "1.0.0")
require.NoError(t, store.SaveScanResult(context.Background(), &metadata.ScanResult{
Registry: "npm", PackageName: name, PackageVersion: "1.0.0", Scanner: "test",
ScannedAt: at, Status: metadata.ScanStatusClean, DurationMs: ms,
}))
}
func TestAverageScanDurationUsesTheLatestRecordedScans(t *testing.T) {
store := trendingStore(t)
ctx := context.Background()
avg, err := store.AverageScanDuration(ctx, 10)
require.NoError(t, err)
require.Zero(t, avg, "no scans yet")
saveScan(t, store, "old", trendingNow.Add(-3*time.Hour), 9000)
saveScan(t, store, "a", trendingNow.Add(-2*time.Hour), 1000)
saveScan(t, store, "b", trendingNow.Add(-time.Hour), 3000)
saveScan(t, store, "unrecorded", trendingNow, 0)
avg, err = store.AverageScanDuration(ctx, 2)
require.NoError(t, err)
require.Equal(t, 2*time.Second, avg, "the two latest recorded scans, the zero ignored")
avg, err = store.AverageScanDuration(ctx, 10)
require.NoError(t, err)
require.InDelta(t, float64(13000)/3, float64(avg.Milliseconds()), 1, "mean of 9000, 1000 and 3000")
}
+11
View File
@@ -136,3 +136,14 @@ func (s *GORMStoreV2) packageDownloads(ctx context.Context, table, sum, cond str
}
return rows, nil
}
// AverageScanDuration averages the latest sample scans that recorded a duration.
func (s *GORMStoreV2) AverageScanDuration(ctx context.Context, sample int) (time.Duration, error) {
recent := s.db.WithContext(ctx).Model(&ScanResultModel{}).
Select("scan_duration").Where("scan_duration > 0").Order("scanned_at DESC").Limit(sample)
var avgMs float64
if err := s.db.WithContext(ctx).Table("(?) AS r", recent).Select("COALESCE(AVG(scan_duration), 0)").Scan(&avgMs).Error; err != nil {
return 0, errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to average scan durations")
}
return time.Duration(avgMs * float64(time.Millisecond)), nil
}
+7
View File
@@ -211,6 +211,7 @@ type ScanResult struct {
Status ScanStatus `json:"status"`
Vulnerabilities []Vulnerability `json:"vulnerabilities"`
VulnerabilityCount int `json:"vulnerability_count"`
DurationMs int `json:"duration_ms"` // wall time of the scan that produced it
}
// Vulnerability represents a security vulnerability
@@ -452,6 +453,12 @@ type TrendingPackage struct {
Previous int64 `json:"previous"`
}
// ScanDurationSource reports how long recent scans took, for backends that keep it.
type ScanDurationSource interface {
// AverageScanDuration averages the latest sample scans that recorded a duration; zero means none did.
AverageScanDuration(ctx context.Context, sample int) (time.Duration, error)
}
// TrendingSource ranks packages from persisted download history. Backends without
// persisted downloads do not implement it.
type TrendingSource interface {