Files
cloud-ip-validator/internal/httpapi/handlers_registry.go
T
ayurishchevandClaude Sonnet 5.5 864208238f Show egress/ingress levels in the registry; freeze checks at the verdict
Registry: the "last result" column now also shows, per level (egress,
ingress), how many of the recorded checks of the latest cycle succeeded, split
by check family (tcp-22 and tcp-443 are both "tcp"). One grouped query per
chunk of addresses; new fields last_cycle_id, egress, ingress in
GET /admin/registry; the dashboard renders them under the verdict.

Verdict integrity (migration 0010):
- the prober is handed an address once per site and attempt, not on every
  poll, so results are no longer overwritten by later probe rounds;
- UpsertCheckIfOpen refuses writes once the address is aggregating or has its
  verdict, or for an older attempt; senders get {"ok":true,"ignored":N} and a
  result_dropped event is recorded;
- the checking window counts from checking_started_at, not from assigned_at;
- checks.recorded_at (server clock) and checks.after_verdict (flag for rows
  written after the verdict in existing data);
- the verdict rule is a pure function (computeVerdict) and the aggregated
  event carries the egress/ingress check counts.

Rebuilt bin/control-api and bin/admin-dashboard to match. Plans and summaries
are in docs/changes; README, API, USAGE, DASHBOARD and DIAGRAMS are updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-03 17:59:52 +03:00

98 lines
3.2 KiB
Go

package httpapi
import (
"net/http"
"strconv"
"strings"
"cloudipvalidator/internal/db"
)
// handleAdminRegistry lists every address ever submitted to the check
// queue, each with a summary of its accumulated check history — the
// durable record that survives an address being deleted from ip_queue and
// later re-added. See migrations/0007_ip_registry.sql. Without `limit` it is
// the bare array of every row; with `limit` (1..1000) it returns the envelope
// {items,total,limit,offset} (filters: offset, q = substring of the address,
// last_result = pass|partial|fail|cancelled).
func (s *Server) handleAdminRegistry(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
limit, offset, paged, err := parsePaging(q)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
filter := db.RegistryFilter{Query: strings.TrimSpace(q.Get("q")), LastResult: q.Get("last_result")}
if filter.LastResult != "" && !db.IsValidResult(filter.LastResult) {
writeError(w, http.StatusBadRequest, "invalid last_result "+strconv.Quote(filter.LastResult)+" (valid: pass, partial, fail, cancelled)")
return
}
var items []db.RegistrySummary
var total int
if len(q) == 0 {
items, err = s.DB.ListRegistry(r.Context())
} else {
items, total, err = s.DB.ListRegistryPage(r.Context(), filter, limit, offset)
}
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
out := make([]registryDTO, len(items))
for i, it := range items {
out[i] = registrySummaryToDTO(it)
}
if !paged {
writeJSON(w, http.StatusOK, out)
return
}
writeJSON(w, http.StatusOK, registryPageResponse{Items: out, Total: total, Limit: limit, Offset: offset})
}
// handleAdminRegistryHistory returns one address's registry record plus its
// full retained check history (across every cycle still kept — see
// db.PruneRegistryHistory / settings.history_retention_cycles), newest
// cycle first.
func (s *Server) handleAdminRegistryHistory(w http.ResponseWriter, r *http.Request) {
address := r.PathValue("ip")
summary, err := s.DB.GetRegistryByAddress(r.Context(), address)
if err != nil {
writeDBError(w, err)
return
}
checks, err := s.DB.ListChecksForRegistry(r.Context(), summary.ID, nil)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, http.StatusOK, struct {
Registry registryDTO `json:"registry"`
Checks []db.Check `json:"checks"`
}{registrySummaryToDTO(*summary), checks})
}
func registrySummaryToDTO(s db.RegistrySummary) registryDTO {
return registryDTO{
IPAddress: s.IPAddress,
FirstSeenAt: s.FirstSeenAt,
LastSeenAt: s.LastSeenAt,
TotalCycles: s.TotalCycles,
LastResult: s.LastResult,
LastCheckedAt: s.LastCheckedAt,
InQueue: s.InQueue,
CurrentState: s.CurrentState,
LastCycleID: s.LastCycleID,
Egress: levelResultToDTO(s.Egress),
Ingress: levelResultToDTO(s.Ingress),
}
}
func levelResultToDTO(l db.LevelResult) levelResultDTO {
out := levelResultDTO{Total: l.Total, OK: l.OK, ByType: make([]typeStatDTO, len(l.ByType))}
for i, t := range l.ByType {
out.ByType[i] = typeStatDTO{Type: t.Type, Total: t.Total, OK: t.OK}
}
return out
}