Fix registry badge showing pass despite a partial/failed cycle

fillRegistrySummary derived LastResult from whichever single checks row
happened to have the latest checked_at, not the cycle's actual aggregated
result — a cycle with a mix of passing and failing checks (e.g. one egress
target timed out while the rest, including the chronologically-last check,
succeeded) rendered as a green "pass" badge on /registry, disagreeing with
the correct "partial" badge already shown on /ips for the same address.

Now prefers ip_queue.overall_result (the orchestrator's own aggregation)
when a live queue row has a finished cycle, leaves the badge blank while a
cycle is still in progress, and only falls back to classifying the most
recent cycle's own checks (pass/fail/partial) once the address has been
deleted from the queue and overall_result is no longer available.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-23 11:03:56 +03:00
1 parent 3b6b2d04c9
commit db97b83cd6
9 files changed
+207 -17

No files matched your search

+69 -13
View File
@@ -110,6 +110,13 @@ func (d *DB) GetRegistryByAddress(ctx context.Context, address string) (*Registr
return s, nil
}
// fillRegistrySummary computes the badge-level summary shown on the
// registry list/detail pages. LastResult must reflect the *aggregated*
// outcome of the most recent cycle (pass/partial/fail/cancelled), not the
// success flag of whichever individual check happens to have the latest
// checked_at — a cycle with a mix of passing and failing checks (e.g. one
// egress target timed out while the rest succeeded) is "partial", even
// though the chronologically-last check to report in might have passed.
func (d *DB) fillRegistrySummary(ctx context.Context, s *RegistrySummary) error {
if err := d.QueryRowContext(ctx, `
SELECT COUNT(DISTINCT cycle_id) FROM checks WHERE registry_id=?
@@ -117,37 +124,86 @@ func (d *DB) fillRegistrySummary(ctx context.Context, s *RegistrySummary) error
return err
}
var lastResult sql.NullBool
var lastCheckedAt sql.NullString
if err := d.QueryRowContext(ctx, `
SELECT success, checked_at FROM checks WHERE registry_id=? ORDER BY cycle_id DESC, checked_at DESC LIMIT 1
`, s.ID).Scan(&lastResult, &lastCheckedAt); err != nil && err != sql.ErrNoRows {
SELECT checked_at FROM checks WHERE registry_id=? ORDER BY cycle_id DESC, checked_at DESC LIMIT 1
`, s.ID).Scan(&lastCheckedAt); err != nil && err != sql.ErrNoRows {
return err
}
if lastResult.Valid {
if lastResult.Bool {
s.LastResult = "pass"
} else {
s.LastResult = "fail"
}
}
t, err := nullStringToTimePtr(lastCheckedAt)
if err != nil {
return err
}
s.LastCheckedAt = t
var state sql.NullString
if err := d.QueryRowContext(ctx, `SELECT state FROM ip_queue WHERE registry_id=?`, s.ID).Scan(&state); err != nil && err != sql.ErrNoRows {
var state, overallResult sql.NullString
err = d.QueryRowContext(ctx, `SELECT state, overall_result FROM ip_queue WHERE registry_id=?`, s.ID).
Scan(&state, &overallResult)
if err != nil && err != sql.ErrNoRows {
return err
}
if state.Valid {
if err == nil {
s.InQueue = true
s.CurrentState = state.String
}
switch {
case overallResult.Valid && overallResult.String != "":
// The address has a live ip_queue row with a finished cycle
// (done/failed) — overall_result is the orchestrator's own
// aggregation (internal/orchestrator.aggregateAndRelease /
// db.CancelIP), the authoritative source of truth. Use it as-is
// rather than re-deriving it from raw check rows.
s.LastResult = overallResult.String
case s.InQueue:
// A live ip_queue row exists but its current cycle hasn't finished
// yet (still queued/checking/etc, overall_result not set) — no
// verdict to show yet; leave LastResult empty rather than guessing
// from a still-incomplete set of checks.
default:
// No live ip_queue row (deleted from the queue) — fall back to
// classifying the most recent recorded cycle from its own checks,
// the same pass/fail/partial rule aggregateAndRelease uses (minus
// "missing" checks, which aren't knowable after the fact).
result, err := lastCycleResultFromChecks(ctx, d, s.ID)
if err != nil {
return err
}
s.LastResult = result
}
return nil
}
// lastCycleResultFromChecks classifies the most recent cycle recorded for
// registryID directly from its checks rows: pass if every recorded check
// succeeded, fail if every one failed, partial on a mix. Returns "" if no
// checks are recorded at all.
func lastCycleResultFromChecks(ctx context.Context, d *DB, registryID int64) (string, error) {
var cycle sql.NullInt64
if err := d.QueryRowContext(ctx, `SELECT MAX(cycle_id) FROM checks WHERE registry_id=?`, registryID).Scan(&cycle); err != nil {
return "", err
}
if !cycle.Valid {
return "", nil
}
var total, passed int
if err := d.QueryRowContext(ctx, `
SELECT COUNT(*), COALESCE(SUM(success), 0) FROM checks WHERE registry_id=? AND cycle_id=?
`, registryID, cycle.Int64).Scan(&total, &passed); err != nil {
return "", err
}
switch {
case total == 0:
return "", nil
case passed == 0:
return ResultFail, nil
case passed == total:
return ResultPass, nil
default:
return ResultPartial, nil
}
}
func scanRegistryItems(rows *sql.Rows) ([]RegistryItem, error) {
var out []RegistryItem
for rows.Next() {