Files
cloud-ip-validator/internal/httpapi/handlers_registry.go
T
ayurishchevandClaude Sonnet 5.5 b7669c9e41 Add the Analytics section: check runs, analytics API and page
Runs (migration 0011): a run groups the cycles of one launch. It opens when an
address enters an idle queue, takes everything submitted or re-checked while it
is open and is finalized when all its addresses are done; a re-check after that
opens a new run, so results of different runs never mix. check_runs,
run_results (one result per address and run, with the verdict and the expected
and stored check counts), subnets, run_id on ip_queue and checks. Existing data
is split into runs at pauses of more than an hour; ingress checks get the
validator that held the address (also at write time from now on).

Analytics (internal/analytics): figures computed from the stored checks of the
latest cycle of each address in the run, as facts next to the verdict: summary,
reasons of partial, data quality, subnets, targets and the subnet x target
matrix by check type, ingress by site, error classes, validators, and the
address lists behind the indicators and error classes. API: analytics runs,
report, lists (JSON or CSV), subnet list; run and subnet filters for the
registry.

Dashboard: /analytics matching the approved mockup (run selector, indicators
with address lists and CSV, error-class dialogs, drill-down to the registry),
subnet list on /settings. Sidebar: the control-api link state, theme toggle and
logout moved to the top, the three dots next to the logo removed, sections
grouped.

Rebuilt bin/control-api and bin/admin-dashboard to match. Plan, summary and the
updated README, API, USAGE, DASHBOARD and ADMIN_CLEANUP docs are in docs/.

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

113 lines
3.7 KiB
Go

package httpapi
import (
"net/http"
"net/netip"
"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"), Subnet: strings.TrimSpace(q.Get("subnet"))}
if filter.Subnet != "" {
if _, err := netip.ParsePrefix(filter.Subnet); err != nil {
writeError(w, http.StatusBadRequest, "invalid subnet "+strconv.Quote(filter.Subnet)+" (a CIDR such as 203.0.113.0/24 is expected)")
return
}
}
if v := q.Get("run"); v != "" {
id, err := strconv.ParseInt(v, 10, 64)
if err != nil || id <= 0 {
writeError(w, http.StatusBadRequest, "invalid run "+strconv.Quote(v))
return
}
filter.RunID = id
}
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
}