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, } }