package db import ( "context" "fmt" "net/netip" "sort" "time" ) // AlwaysPass is the always-pass indicator of a registry period: of the // Addresses that have checks in the slice during the Runs of the period, Passed // never failed one. FirstRunAt and LastRunAt are the start of the oldest and the // newest run of the period (nil without runs). type AlwaysPass struct { Runs int Truncated bool FirstRunAt *time.Time LastRunAt *time.Time Addresses int Passed int } // AlwaysPassRow is one address that passed every check of the slice: in how many // runs it was checked, how many checks that made, and the first and the last of // those runs. type AlwaysPassRow struct { IPAddress string RunsChecked int Checks int FirstRunAt time.Time LastRunAt time.Time } // alwaysPassScan is the per-address result of alwaysPassRows. type alwaysPassScan struct { ip string runs, total, ok int first, last time.Time } // alwaysPassRows reads, for the runs of the period [from, to] (as in // RegistryTrend), the checks of f.Level (required, else ErrValidation) and // optionally f.Family of every address inside f.Subnet whose IP contains // f.Query, one row per address with at least one such check, ordered by IP. As // in RegistryTrend it reads the address's cycle in the run. func (d *DB) alwaysPassRows(ctx context.Context, f RegistryFilter, from, to *time.Time) ([]TrendRun, bool, []alwaysPassScan, error) { sl := registrySlice{Level: f.Level, Family: f.Family} if err := sl.validate(); err != nil { return nil, false, nil, err } if sl.Level == "" { return nil, false, nil, fmt.Errorf("direction is required: %w", ErrValidation) } runs, truncated, err := d.trendRuns(ctx, from, to) if err != nil || len(runs) == 0 { return runs, truncated, nil, err } cond, args, ok, err := d.trendRunsCond(ctx, runs, f.Subnet) if err != nil || !ok { return runs, truncated, nil, err } if f.Query != "" { cond += ` AND instr(r.ip_address, ?) > 0` args = append(args, f.Query) } started := make(map[int64]time.Time, len(runs)) for _, r := range runs { started[r.ID] = r.StartedAt } // CROSS JOIN keeps the checks as the inner table, as in RegistryTrend: the // run's results drive the lookups over idx_checks_run. sc, scArgs := sl.scopeCond() rows, err := d.QueryContext(ctx, ` SELECT r.ip_address, COUNT(DISTINCT rr.run_id), COUNT(*), COALESCE(SUM(c.success), 0), MIN(rr.run_id), MAX(rr.run_id) FROM run_results rr JOIN ip_registry r ON r.id = rr.registry_id CROSS JOIN checks c ON c.run_id = rr.run_id AND c.registry_id = rr.registry_id AND c.cycle_id = rr.cycle_id WHERE `+cond+` AND `+sc+` GROUP BY rr.registry_id`, append(args, scArgs...)...) if err != nil { return nil, false, nil, err } defer rows.Close() var out []alwaysPassScan for rows.Next() { var s alwaysPassScan var first, last int64 if err := rows.Scan(&s.ip, &s.runs, &s.total, &s.ok, &first, &last); err != nil { return nil, false, nil, err } s.first, s.last = started[first], started[last] out = append(out, s) } if err := rows.Err(); err != nil { return nil, false, nil, err } sort.Slice(out, func(i, j int) bool { a, errA := netip.ParseAddr(out[i].ip) b, errB := netip.ParseAddr(out[j].ip) if errA != nil || errB != nil { return out[i].ip < out[j].ip } return a.Less(b) }) return runs, truncated, out, nil } // RegistryAlwaysPass counts, over the finalized runs of the period [from, to] // (as RegistryTrend picks them), the addresses that have checks of f.Level // (required) and f.Family in the slice and the ones among them that never // failed one. Subnet, Level, Family and Query of f count; the run and status // filters do not, as a verdict of the current cycle says nothing of a history. func (d *DB) RegistryAlwaysPass(ctx context.Context, f RegistryFilter, from, to *time.Time) (*AlwaysPass, error) { runs, truncated, scans, err := d.alwaysPassRows(ctx, f, from, to) if err != nil { return nil, err } ap := &AlwaysPass{Runs: len(runs), Truncated: truncated, Addresses: len(scans)} if n := len(runs); n > 0 { ap.FirstRunAt, ap.LastRunAt = &runs[0].StartedAt, &runs[n-1].StartedAt } for _, s := range scans { if s.ok == s.total { ap.Passed++ } } return ap, nil } // RegistryAlwaysPassList returns the addresses RegistryAlwaysPass counts as // passed, by ascending IP, and the number of runs in the period. func (d *DB) RegistryAlwaysPassList(ctx context.Context, f RegistryFilter, from, to *time.Time) ([]AlwaysPassRow, int, error) { runs, _, scans, err := d.alwaysPassRows(ctx, f, from, to) if err != nil { return nil, 0, err } var out []AlwaysPassRow for _, s := range scans { if s.ok == s.total { out = append(out, AlwaysPassRow{IPAddress: s.ip, RunsChecked: s.runs, Checks: s.total, FirstRunAt: s.first, LastRunAt: s.last}) } } return out, len(runs), nil }