diff --git a/bin/SHA256SUMS b/bin/SHA256SUMS
index 5c49ddf..4b2fc32 100644
--- a/bin/SHA256SUMS
+++ b/bin/SHA256SUMS
@@ -1,4 +1,4 @@
-69a60a3ba48e83640b6d8b122401e7204cfaac6c5d60976e19904c81eb37f838 control-api
+a20dc6b6714cfcc204a6ff12830c8d7fc960e03c9a7590de73150b621e0fdd3e control-api
ec6959ae151d8ad1ad5b68faca420159ca6ca6b3dd1359b7f9b0d8489ac0a27b validator-agent
7ef8785769d946d81c338f53928bbe18f243ec5b96ab8b874846504b8ac4e98c prober
-dd3fcccf595608f477726d4818abe4e4fb9c7212ab6df73c68fdc346222ab047 admin-dashboard
+de1fcc94ab356b6b9ef560feb9f9f25e7bd9f5c74fa9fd544360c586713af761 admin-dashboard
diff --git a/bin/admin-dashboard b/bin/admin-dashboard
index f2102a1..7d3dd64 100755
Binary files a/bin/admin-dashboard and b/bin/admin-dashboard differ
diff --git a/bin/control-api b/bin/control-api
index 3780a09..ab62db5 100755
Binary files a/bin/control-api and b/bin/control-api differ
diff --git a/internal/dashboard/dashboard_test.go b/internal/dashboard/dashboard_test.go
index 5a532b0..c90e21b 100644
--- a/internal/dashboard/dashboard_test.go
+++ b/internal/dashboard/dashboard_test.go
@@ -63,6 +63,8 @@ type fakeControlAPI struct {
scanFreeAddresses []string
registry map[string]registryItem
registryChecks map[string][]check
+ registryEvents []event
+ registryCycles []registryCycle
// The chart of /registry: the breakdown served (breakdownStatus != 0 makes
// it fail with that status), the table behind a row and the raw queries
// of both endpoints.
@@ -553,7 +555,10 @@ func (f *fakeControlAPI) handler() http.Handler {
writeAPIErr(w, http.StatusNotFound, "unknown ip: "+addr)
return
}
- writeJSON(w, http.StatusOK, registryHistoryResponse{Registry: item, Checks: f.registryChecks[addr], SelfCheckFailedOn: f.selfCheckFailedOn})
+ writeJSON(w, http.StatusOK, registryHistoryResponse{
+ Registry: item, Checks: f.registryChecks[addr], SelfCheckFailedOn: f.selfCheckFailedOn,
+ Events: f.registryEvents, Cycles: f.registryCycles,
+ })
})
mux.HandleFunc("GET /api/v1/admin/config/subnets", func(w http.ResponseWriter, r *http.Request) {
diff --git a/internal/dashboard/dto.go b/internal/dashboard/dto.go
index 3150553..e4caa1a 100644
--- a/internal/dashboard/dto.go
+++ b/internal/dashboard/dto.go
@@ -62,6 +62,7 @@ type ipQueueItem struct {
FIPAssociatedAt *time.Time
AggregatedAt *time.Time
FIPReleasedAt *time.Time
+ CycleID int
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -88,6 +89,7 @@ type event struct {
SourceType string
SourceID string
IPID *int64
+ CycleID int
EventType string
Payload string
OccurredAt time.Time
@@ -373,7 +375,18 @@ type registryHistoryResponse struct {
Checks []check `json:"checks"`
// SelfCheckFailedOn lists the validators whose self-check of this address
// failed, over every run.
- SelfCheckFailedOn []string `json:"self_check_failed_on"`
+ SelfCheckFailedOn []string `json:"self_check_failed_on"`
+ Events []event `json:"events"`
+ Cycles []registryCycle `json:"cycles"`
+ CurrentIP *ipQueueItem `json:"current_ip"`
+}
+
+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"`
}
type validatorDTO struct {
diff --git a/internal/dashboard/handlers_ips.go b/internal/dashboard/handlers_ips.go
index dc3b2ce..78a085b 100644
--- a/internal/dashboard/handlers_ips.go
+++ b/internal/dashboard/handlers_ips.go
@@ -122,8 +122,13 @@ type ipsPageData struct {
type ipDetailData struct {
PageData
- Detail ipDetailResponse
- Events []eventView
+ ipDetailView
+}
+
+type ipDetailView struct {
+ Detail ipDetailResponse
+ Events []eventView
+ HasQueueData bool
}
// tracerouteView is a parsed traceroute_result event payload.
@@ -227,7 +232,7 @@ func (s *Server) handleIPsPage(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleIPDetail(w http.ResponseWriter, r *http.Request) {
ip := r.PathValue("ip")
detail, err := s.CA.GetIP(r.Context(), ip)
- data := ipDetailData{Detail: detail, Events: buildEventViews(detail.Events)}
+ data := ipDetailData{ipDetailView: ipDetailView{Detail: detail, Events: buildEventViews(detail.Events), HasQueueData: true}}
data.ActiveNav = "ips"
data.Banner = bannerFor(err)
s.renderPage(w, r, "ip_detail_page", data)
diff --git a/internal/dashboard/handlers_registry.go b/internal/dashboard/handlers_registry.go
index 5cdc3c1..c6c1c36 100644
--- a/internal/dashboard/handlers_registry.go
+++ b/internal/dashboard/handlers_registry.go
@@ -44,6 +44,7 @@ type registryPageData struct {
type registryDetailData struct {
PageData
History registryHistoryResponse
+ Cycles []registryCycleView
}
// handleRegistryPage lists every address ever submitted to the check
@@ -347,7 +348,7 @@ func registryRunOptions(runs []analyticsRun, chosen int64) []runOption {
func (s *Server) handleRegistryDetail(w http.ResponseWriter, r *http.Request) {
ip := r.PathValue("ip")
history, err := s.CA.GetRegistryHistory(r.Context(), ip)
- data := registryDetailData{History: history}
+ data := registryDetailData{History: history, Cycles: buildRegistryCycles(history)}
data.ActiveNav = "registry"
data.Banner = bannerFor(err)
s.renderPage(w, r, "registry_detail_page", data)
diff --git a/internal/dashboard/handlers_test.go b/internal/dashboard/handlers_test.go
index 507cf2f..07690c0 100644
--- a/internal/dashboard/handlers_test.go
+++ b/internal/dashboard/handlers_test.go
@@ -637,8 +637,13 @@ func TestRegistryPageAndDetail(t *testing.T) {
TotalCycles: 2, LastResult: "pass", InQueue: false,
}
fake.registryChecks["9.9.9.9"] = []check{
- {CycleID: 2, Source: "egress", CheckType: "https", Target: "https://example.test", Success: true, CheckedAt: now},
- {CycleID: 1, Source: "egress", CheckType: "https", Target: "https://example.test", Success: false, CheckedAt: now},
+ {CycleID: 2, AttemptNumber: 1, Source: "egress", CheckType: "https", Target: "https://new.example.test", Success: true, CheckedAt: now},
+ {CycleID: 1, AttemptNumber: 1, Source: "egress", CheckType: "https", Target: "https://old.example.test", Success: false, CheckedAt: now.Add(-time.Hour)},
+ }
+ fake.registryEvents = []event{{CycleID: 2, EventType: "traceroute_result", OccurredAt: now, Payload: `{"output":"trace of cycle two"}`}}
+ fake.registryCycles = []registryCycle{
+ {CycleID: 2, RunID: 20, Verdict: "pass", AggregatedAt: &now},
+ {CycleID: 1, RunID: 19, Verdict: "partial", SelfCheckFailedOn: []string{"older-validator"}},
}
ts := newTestServer(t, caURL)
@@ -651,8 +656,31 @@ func TestRegistryPageAndDetail(t *testing.T) {
if !strings.Contains(detail, "9.9.9.9") {
t.Fatalf("expected detail page to show the address, got:\n%s", detail)
}
- if !strings.Contains(detail, "https://example.test") {
- t.Fatalf("expected detail page to list retained checks, got:\n%s", detail)
+ const collapsedCycle = `
Полная история проверок этого адреса за всё время →
+{{template "ip_check_cycle" .}} +{{end}} + +{{define "ip_check_cycle"}}-попытка {{.Detail.IP.AttemptNumber}} (повторов: {{.Detail.IP.RetryCount}}) +попытка {{if .Detail.IP.AttemptNumber}}{{.Detail.IP.AttemptNumber}}{{else}}—{{end}} (повторов: {{if .HasQueueData}}{{.Detail.IP.RetryCount}}{{else}}—{{end}}) · валидатор {{deref .Detail.IP.OwnerValidatorID}}
@@ -36,7 +40,7 @@
{{if .Detail.SelfCheckFailedOn}}Self-check не прошёл на: {{join .Detail.SelfCheckFailedOn ", "}}
{{end}} -Self-check не прошёл на: {{join .History.SelfCheckFailedOn ", "}}
{{end}} -| Цикл | Источник | Тип | Цель | Успех | Задержка, мс | Время | Детали |
|---|---|---|---|---|---|---|---|
| {{.CycleID}} | -{{.Source}} | {{.CheckType}} | {{.Target}} | -{{if .Success}}ok{{else}}fail{{end}} | -{{.LatencyMS}} | {{fmtTime .CheckedAt}} | {{.Detail}} | -
Если глубина истории (см. настройки) ограничена, здесь показаны только сохранённые циклы — более старые уже вытеснены.
diff --git a/internal/db/queries_events.go b/internal/db/queries_events.go index 20460e2..e50b314 100644 --- a/internal/db/queries_events.go +++ b/internal/db/queries_events.go @@ -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 diff --git a/internal/db/queries_registry.go b/internal/db/queries_registry.go index 163cf80..58fce80 100644 --- a/internal/db/queries_registry.go +++ b/internal/db/queries_registry.go @@ -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() { diff --git a/internal/httpapi/handlers_registry.go b/internal/httpapi/handlers_registry.go index 6d816c5..536a536 100644 --- a/internal/httpapi/handlers_registry.go +++ b/internal/httpapi/handlers_registry.go @@ -1,6 +1,8 @@ package httpapi import ( + "database/sql" + "errors" "fmt" "net/http" "net/netip" @@ -111,11 +113,29 @@ func (s *Server) handleAdminRegistryHistory(w http.ResponseWriter, r *http.Reque writeError(w, http.StatusInternalServerError, err.Error()) return } + events, err := s.DB.ListEventsForRegistry(r.Context(), summary.ID) + if err != nil { + writeDBError(w, err) + return + } + cycles, err := s.DB.ListRegistryCycles(r.Context(), summary.ID) + if err != nil { + writeDBError(w, err) + return + } + currentIP, err := s.DB.GetIPByAddress(r.Context(), summary.IPAddress) + if err != nil && !errors.Is(err, sql.ErrNoRows) { + writeDBError(w, err) + return + } writeJSON(w, http.StatusOK, struct { - Registry registryDTO `json:"registry"` - Checks []db.Check `json:"checks"` - SelfCheckFailedOn []string `json:"self_check_failed_on"` - }{registrySummaryToDTO(*summary), checks, failedOn}) + Registry registryDTO `json:"registry"` + Checks []db.Check `json:"checks"` + SelfCheckFailedOn []string `json:"self_check_failed_on"` + Events []db.Event `json:"events"` + Cycles []db.RegistryCycle `json:"cycles"` + CurrentIP *db.IPQueueItem `json:"current_ip,omitempty"` + }{registrySummaryToDTO(*summary), checks, failedOn, events, cycles, currentIP}) } func registrySummaryToDTO(s db.RegistrySummary) registryDTO { diff --git a/internal/httpapi/handlers_registry_test.go b/internal/httpapi/handlers_registry_test.go index 2d09367..6a6d614 100644 --- a/internal/httpapi/handlers_registry_test.go +++ b/internal/httpapi/handlers_registry_test.go @@ -106,8 +106,11 @@ func TestRegistryHistoryOutlivesIPDeletion(t *testing.T) { t.Fatalf("registry history: status=%d body=%s", resp.StatusCode, body) } var hist struct { - Registry registryDTO `json:"registry"` - Checks []db.Check `json:"checks"` + Registry registryDTO `json:"registry"` + Checks []db.Check `json:"checks"` + Events []db.Event `json:"events"` + Cycles []db.RegistryCycle `json:"cycles"` + CurrentIP *db.IPQueueItem `json:"current_ip"` } if err := json.Unmarshal(body, &hist); err != nil { t.Fatalf("unmarshal registry history: %v", err) @@ -121,6 +124,18 @@ func TestRegistryHistoryOutlivesIPDeletion(t *testing.T) { if hist.Registry.InQueue { t.Fatalf("expected registry entry to report not-in-queue after delete, got %+v", hist.Registry) } + if hist.CurrentIP != nil || len(hist.Cycles) != 1 || hist.Cycles[0].CycleID != item.CycleID || + hist.Cycles[0].RunID == 0 || hist.Cycles[0].Verdict != item.OverallResult || hist.Cycles[0].AggregatedAt == nil { + t.Fatalf("expected archived cycle with its own verdict and timestamp, got %+v", hist.Cycles) + } + if len(hist.Events) == 0 { + t.Fatal("expected archived events") + } + for _, event := range hist.Events { + if event.CycleID != item.CycleID || event.IPID != nil { + t.Fatalf("event should stay attached to the archived cycle, got %+v", event) + } + } // Resubmitting starts a second, distinct cycle on the same registry // entry rather than colliding with the first. @@ -141,6 +156,10 @@ func TestRegistryHistoryOutlivesIPDeletion(t *testing.T) { if !hist.Registry.InQueue { t.Fatalf("expected registry entry to report in-queue again, got %+v", hist.Registry) } + if hist.CurrentIP == nil || len(hist.Cycles) != 2 || hist.Cycles[0].CycleID != hist.CurrentIP.CycleID || + hist.Cycles[1].CycleID != item.CycleID || hist.Cycles[0].RunID == hist.Cycles[1].RunID { + t.Fatalf("expected current and archived cycles from different runs, got %+v", hist.Cycles) + } _, body = fc.do(http.MethodGet, "/api/v1/admin/registry", nil) var all []registryDTO