Registry: trend chart, always-pass indicator, subnet columns in the address list dialog
- Registry: "Динамика по запускам" chart (Chart.js 4.5.1, vendored) over finalized runs per target or site, % or count, subnet and calendar period filters; GET /admin/registry/trend - Registry: clickable "Всегда проходили" indicator with the address list and CSV; GET /admin/registry/always-pass and /always-pass/list - Address list dialog: "Подсеть" and "Метка подсети" columns from the administrator's subnet list (shared db.SubnetMatcher), per-subnet summary chips with a client-side filter; "прочие" renamed to "вне списка подсетей" - Rebuild control-api and admin-dashboard binaries; plans and summaries in docs/changes, README, API and USAGE updated Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c13139cd41
commit
1536723196
50 files changed
+3560
-154
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloudipvalidator/internal/analytics"
|
||||
"cloudipvalidator/internal/db"
|
||||
@@ -240,11 +241,17 @@ func (s *Server) handleAdminRegistryBreakdownList(w http.ResponseWriter, r *http
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
subnets, err := s.DB.ListSubnets(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
matcher := db.NewSubnetMatcher(subnets)
|
||||
group := db.BreakdownTarget
|
||||
columns := []string{"Адрес", "Результат", "Тип проверки", "Цель", "Валидатор", "Задержка, мс", "Детали", "Проверено (UTC)"}
|
||||
columns := []string{"Адрес", "Подсеть", "Метка подсети", "Результат", "Тип проверки", "Цель", "Валидатор", "Задержка, мс", "Детали", "Проверено (UTC)"}
|
||||
if f.Level == db.LevelIngress {
|
||||
group = db.BreakdownSite
|
||||
columns = []string{"Адрес", "Результат", "Тип проверки", "Площадка", "Задержка, мс", "Детали", "Проверено (UTC)"}
|
||||
columns = []string{"Адрес", "Подсеть", "Метка подсети", "Результат", "Тип проверки", "Площадка", "Задержка, мс", "Детали", "Проверено (UTC)"}
|
||||
}
|
||||
rows := make([][]string, len(checks))
|
||||
for i, c := range checks {
|
||||
@@ -252,7 +259,8 @@ func (s *Server) handleAdminRegistryBreakdownList(w http.ResponseWriter, r *http
|
||||
if c.Success {
|
||||
result = "успешно"
|
||||
}
|
||||
row := []string{c.IPAddress, result, c.CheckType, breakdownLabel(group, key, names)}
|
||||
cidr, label := matcher.Lookup(c.IPAddress)
|
||||
row := []string{c.IPAddress, cidr, label, result, c.CheckType, breakdownLabel(group, key, names)}
|
||||
if group == db.BreakdownTarget {
|
||||
row = append(row, analytics.ShortValidator(c.ValidatorID))
|
||||
}
|
||||
@@ -267,3 +275,146 @@ func (s *Server) handleAdminRegistryBreakdownList(w http.ResponseWriter, r *http
|
||||
Rows [][]string `json:"rows"`
|
||||
}{columns, rows})
|
||||
}
|
||||
|
||||
// parseTrendDate reads a YYYY-MM-DD parameter of the trend; empty is nil.
|
||||
func parseTrendDate(q url.Values, name string) (*time.Time, string) {
|
||||
v := q.Get(name)
|
||||
if v == "" {
|
||||
return nil, ""
|
||||
}
|
||||
t, err := time.Parse("2006-01-02", v)
|
||||
if err != nil {
|
||||
return nil, "invalid " + name + " " + strconv.Quote(v) + " (YYYY-MM-DD is expected)"
|
||||
}
|
||||
return &t, ""
|
||||
}
|
||||
|
||||
// periodFor reads the filter and the period (from/to, YYYY-MM-DD) of the trend
|
||||
// and always-pass endpoints, which need a direction, and writes the 400
|
||||
// response itself when it cannot.
|
||||
func periodFor(w http.ResponseWriter, r *http.Request) (f db.RegistryFilter, from, to *time.Time, ok bool) {
|
||||
q := r.URL.Query()
|
||||
f, msg := parseRegistryFilter(q)
|
||||
if msg == "" && f.Level == "" {
|
||||
msg = "direction is required"
|
||||
}
|
||||
from, fmsg := parseTrendDate(q, "from")
|
||||
to, tmsg := parseTrendDate(q, "to")
|
||||
switch {
|
||||
case msg != "":
|
||||
case fmsg != "":
|
||||
msg = fmsg
|
||||
case tmsg != "":
|
||||
msg = tmsg
|
||||
case from != nil && to != nil && from.After(*to):
|
||||
msg = "from is after to"
|
||||
}
|
||||
if msg != "" {
|
||||
writeError(w, http.StatusBadRequest, msg)
|
||||
return f, nil, nil, false
|
||||
}
|
||||
return f, from, to, true
|
||||
}
|
||||
|
||||
// handleAdminRegistryTrend returns the dynamics of the checks of one direction
|
||||
// (required) and protocol over the finalized runs: a series per target (egress)
|
||||
// or site (ingress). Filters: direction, protocol, subnet and the period
|
||||
// from/to (YYYY-MM-DD, UTC, to inclusive); the run, status and address filters
|
||||
// are ignored. Without a period the last 30 runs are returned, at most 200
|
||||
// with one; see db.RegistryTrend.
|
||||
func (s *Server) handleAdminRegistryTrend(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
f, from, to, ok := periodFor(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
t, err := s.DB.RegistryTrend(r.Context(), f, from, to)
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
names, err := s.siteNames(r)
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
out := registryTrendDTO{Group: t.Group, Direction: f.Level, Protocol: f.Family, From: q.Get("from"), To: q.Get("to"),
|
||||
Truncated: t.Truncated, Runs: make([]trendRunDTO, len(t.Runs)), Series: []trendSeriesDTO{}}
|
||||
for i, x := range t.Runs {
|
||||
out.Runs[i] = trendRunDTO{ID: x.ID, StartedAt: x.StartedAt, FinalizedAt: x.FinalizedAt, Addresses: x.Addresses}
|
||||
}
|
||||
// Points arrive grouped by key; the series are ordered by successful checks,
|
||||
// most first, then by label, as the breakdown rows.
|
||||
okSum := map[string]int{}
|
||||
for _, p := range t.Points {
|
||||
if n := len(out.Series); n == 0 || out.Series[n-1].Key != p.Key {
|
||||
out.Series = append(out.Series, trendSeriesDTO{Key: p.Key, Label: breakdownLabel(t.Group, p.Key, names)})
|
||||
}
|
||||
sr := &out.Series[len(out.Series)-1]
|
||||
sr.Points = append(sr.Points, trendPointDTO{RunID: p.RunID, Total: p.Total, OK: p.OK})
|
||||
okSum[p.Key] += p.OK
|
||||
}
|
||||
sort.SliceStable(out.Series, func(i, j int) bool {
|
||||
a, b := out.Series[i], out.Series[j]
|
||||
if okSum[a.Key] != okSum[b.Key] {
|
||||
return okSum[a.Key] > okSum[b.Key]
|
||||
}
|
||||
return a.Label < b.Label
|
||||
})
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// handleAdminRegistryAlwaysPass counts the addresses that passed every check of
|
||||
// one direction (required) and protocol during the period: the same runs as
|
||||
// the trend, filters direction, protocol, subnet, q and from/to; the run and
|
||||
// status filters are ignored. See db.RegistryAlwaysPass.
|
||||
func (s *Server) handleAdminRegistryAlwaysPass(w http.ResponseWriter, r *http.Request) {
|
||||
f, from, to, ok := periodFor(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ap, err := s.DB.RegistryAlwaysPass(r.Context(), f, from, to)
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
writeJSON(w, http.StatusOK, registryAlwaysPassDTO{Direction: f.Level, Protocol: f.Family, From: q.Get("from"), To: q.Get("to"),
|
||||
Truncated: ap.Truncated, Runs: ap.Runs, FirstRunAt: ap.FirstRunAt, LastRunAt: ap.LastRunAt, Addresses: ap.Addresses, AlwaysPass: ap.Passed})
|
||||
}
|
||||
|
||||
// handleAdminRegistryAlwaysPassList serves the addresses behind the always-pass
|
||||
// indicator (same parameters) as a table by ascending IP, or with ?format=csv
|
||||
// as a downloadable CSV file.
|
||||
func (s *Server) handleAdminRegistryAlwaysPassList(w http.ResponseWriter, r *http.Request) {
|
||||
f, from, to, ok := periodFor(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
list, runs, err := s.DB.RegistryAlwaysPassList(r.Context(), f, from, to)
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
subnets, err := s.DB.ListSubnets(r.Context())
|
||||
if err != nil {
|
||||
writeDBError(w, err)
|
||||
return
|
||||
}
|
||||
matcher := db.NewSubnetMatcher(subnets)
|
||||
columns := []string{"Адрес", "Подсеть", "Метка подсети", "Запусков с проверками", "Запусков в периоде", "Проверок (все успешны)", "Первый запуск (UTC)", "Последний запуск (UTC)"}
|
||||
rows := make([][]string, len(list))
|
||||
for i, x := range list {
|
||||
cidr, label := matcher.Lookup(x.IPAddress)
|
||||
rows[i] = []string{x.IPAddress, cidr, label, strconv.Itoa(x.RunsChecked), strconv.Itoa(runs), strconv.Itoa(x.Checks),
|
||||
x.FirstRunAt.UTC().Format("2006-01-02 15:04:05"), x.LastRunAt.UTC().Format("2006-01-02 15:04:05")}
|
||||
}
|
||||
if r.URL.Query().Get("format") == "csv" {
|
||||
writeCSV(w, columns, rows, fmt.Sprintf("registry_always_pass_%s_%s.csv", f.Level, f.Family))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Columns []string `json:"columns"`
|
||||
Rows [][]string `json:"rows"`
|
||||
}{columns, rows})
|
||||
}
|
||||
Reference in new issue
Block a user