2026-10-03 18:36:03 +03:00
|
|
|
package analytics
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-10-06 14:23:48 +03:00
|
|
|
"net/netip"
|
2026-10-03 18:36:03 +03:00
|
|
|
|
|
|
|
|
"cloudipvalidator/internal/db"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-06 14:23:48 +03:00
|
|
|
// Load reads a finished run from the database and computes its analysis,
|
|
|
|
|
// narrowed to subnet unless that is the zero prefix.
|
|
|
|
|
func Load(ctx context.Context, d *db.DB, runID int64, subnet netip.Prefix) (*Analysis, error) {
|
2026-10-03 18:36:03 +03:00
|
|
|
run, err := d.GetRun(ctx, runID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
results, err := d.ListRunResults(ctx, runID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
subnets, err := d.ListSubnets(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
sites, err := d.ListSites(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
names := map[int]string{}
|
|
|
|
|
for _, s := range sites {
|
|
|
|
|
names[s.Index] = s.SiteID
|
|
|
|
|
}
|
|
|
|
|
rechecked, err := d.CountRecheckedInRun(ctx, runID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-10-06 14:23:48 +03:00
|
|
|
// With a subnet only the checks of its addresses are read; Compute narrows
|
|
|
|
|
// the results by the same rule.
|
|
|
|
|
var ids []int64
|
|
|
|
|
if subnet.IsValid() {
|
|
|
|
|
ids = []int64{}
|
|
|
|
|
for _, r := range results {
|
|
|
|
|
if ip, err := netip.ParseAddr(r.IPAddress); err == nil && subnet.Contains(ip) {
|
|
|
|
|
ids = append(ids, r.RegistryID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-03 18:36:03 +03:00
|
|
|
return Compute(Input{
|
2026-10-06 14:23:48 +03:00
|
|
|
Run: *run, Results: results, Subnets: subnets, SiteNames: names, Rechecked: rechecked, Subnet: subnet,
|
|
|
|
|
Each: func(fn func(db.RunCheck)) error { return d.EachRunCheck(ctx, runID, ids, fn) },
|
2026-10-03 18:36:03 +03:00
|
|
|
})
|
|
|
|
|
}
|