Show collapsible IP check cycles in the registry

This commit is contained in:
ayurishchev committed 2026-11-23 11:53:08 +03:00
1 parent 94124747ce
commit b58d5f5b82
16 files changed
+332 -36

No files matched your search

+14
View File
@@ -37,6 +37,20 @@ func (d *DB) ListEventsForIP(ctx context.Context, ipID int64) ([]Event, error) {
return scanEvents(rows)
}
// ListEventsForRegistry includes events whose queue row has been deleted.
func (d *DB) ListEventsForRegistry(ctx context.Context, registryID int64) ([]Event, error) {
rows, err := d.QueryContext(ctx, `
SELECT id, source_type, source_id, ip_id, registry_id, cycle_id, event_type, payload, occurred_at, created_at
FROM events WHERE registry_id=? AND cycle_id>0
ORDER BY julianday(occurred_at) DESC, id DESC
`, registryID)
if err != nil {
return nil, err
}
defer rows.Close()
return scanEvents(rows)
}
func (d *DB) ListRecentEvents(ctx context.Context, limit int) ([]Event, error) {
rows, err := d.QueryContext(ctx, `
SELECT id, source_type, source_id, ip_id, registry_id, cycle_id, event_type, payload, occurred_at, created_at
+55
View File
@@ -603,6 +603,61 @@ func lastCycleResultFromChecks(ctx context.Context, d *DB, registryID int64) (st
}
}
type RegistryCycle struct {
CycleID int `json:"cycle_id"`
RunID int64 `json:"run_id"`
Verdict string `json:"verdict"`
AggregatedAt *time.Time `json:"aggregated_at"`
SelfCheckFailedOn []string `json:"self_check_failed_on"`
}
// ListRegistryCycles adds run verdicts and self-check failures to the cycles
// that still have queue data, checks, or events. Analytics can keep results
// after registry history is pruned.
func (d *DB) ListRegistryCycles(ctx context.Context, registryID int64) ([]RegistryCycle, error) {
rows, err := d.QueryContext(ctx, `
WITH evidence AS (
SELECT cycle_id, run_id FROM checks WHERE registry_id=?1
UNION ALL
SELECT DISTINCT cycle_id, NULL FROM events WHERE registry_id=?1 AND cycle_id>0
UNION ALL
SELECT cycle_id, run_id FROM ip_queue WHERE registry_id=?1
), cycles AS (
SELECT cycle_id, MAX(run_id) AS run_id FROM evidence GROUP BY cycle_id
)
SELECT c.cycle_id, COALESCE(c.run_id, r.run_id, 0),
COALESCE(CASE WHEN r.verdict_derived=0 THEN r.verdict END, ''),
CASE WHEN r.verdict_derived=0 THEN r.aggregated_at END,
(SELECT json_group_array(validator_id) FROM (
SELECT DISTINCT validator_id FROM ip_self_check_failures
WHERE registry_id=?1 AND cycle_id=c.cycle_id ORDER BY validator_id
))
FROM cycles c LEFT JOIN run_results r ON r.registry_id=?1 AND r.cycle_id=c.cycle_id
ORDER BY c.cycle_id DESC
`, registryID)
if err != nil {
return nil, err
}
defer rows.Close()
var cycles []RegistryCycle
for rows.Next() {
var c RegistryCycle
var aggregatedAt sql.NullString
var failures string
if err := rows.Scan(&c.CycleID, &c.RunID, &c.Verdict, &aggregatedAt, &failures); err != nil {
return nil, err
}
if c.AggregatedAt, err = nullStringToTimePtr(aggregatedAt); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(failures), &c.SelfCheckFailedOn); err != nil {
return nil, err
}
cycles = append(cycles, c)
}
return cycles, rows.Err()
}
func scanRegistryItems(rows *sql.Rows) ([]RegistryItem, error) {
var out []RegistryItem
for rows.Next() {