2026-09-23 09:52:01 +03:00
|
|
|
package httpapi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2026-10-01 19:31:11 +03:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2026-09-23 09:52:01 +03:00
|
|
|
|
|
|
|
|
"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
|
2026-10-01 19:31:11 +03:00
|
|
|
// 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).
|
2026-09-23 09:52:01 +03:00
|
|
|
func (s *Server) handleAdminRegistry(w http.ResponseWriter, r *http.Request) {
|
2026-10-01 19:31:11 +03:00
|
|
|
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)
|
|
|
|
|
}
|
2026-09-23 09:52:01 +03:00
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
out := make([]registryDTO, len(items))
|
|
|
|
|
for i, it := range items {
|
|
|
|
|
out[i] = registrySummaryToDTO(it)
|
|
|
|
|
}
|
2026-10-01 19:31:11 +03:00
|
|
|
if !paged {
|
|
|
|
|
writeJSON(w, http.StatusOK, out)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, registryPageResponse{Items: out, Total: total, Limit: limit, Offset: offset})
|
2026-09-23 09:52:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
2026-10-03 17:59:52 +03:00
|
|
|
LastCycleID: s.LastCycleID,
|
|
|
|
|
Egress: levelResultToDTO(s.Egress),
|
|
|
|
|
Ingress: levelResultToDTO(s.Ingress),
|
2026-09-23 09:52:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-03 17:59:52 +03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|