mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-05-11 18:54:42 +00:00
51b9fed15e
## Summary Implements `/#/roles` per QA #809 §5.4 / issue #818. The page previously showed "Page not yet implemented." ### Backend - New `GET /api/analytics/roles` returns `{ totalNodes, roles: [{ role, nodeCount, withSkew, meanAbsSkewSec, medianAbsSkewSec, okCount, warningCount, criticalCount, absurdCount, noClockCount }] }`. - Pure `computeRoleAnalytics(nodesByPubkey, skewByPubkey)` does the bucketing/aggregation — no store/lock dependency, fully unit-testable. - Roles are normalised (lowercased + trimmed; empty bucketed as `unknown`). ### Frontend - New `public/roles-page.js` renders a distribution table: count, share, distribution bar, w/ skew, median |skew|, mean |skew|, severity breakdown (OK / Warning / Critical / Absurd / No-clock). - Registered as the `roles` page in the SPA router and linked from the main nav. - Auto-refreshes every 60 s, with a manual refresh button. ### Tests (TDD) - **Red commit** (`9726d5b`): two assertion-failing tests against a stub `computeRoleAnalytics` that returns an empty result. Compiles, runs, fails on `TotalNodes = 0, want 5` and `len(Roles) = 0, want 1`. - **Green commit** (`7efb76a`): full implementation, route wiring, frontend page + nav, plus E2E test in `test-e2e-playwright.js` covering both the empty-state contract (no "Page not yet implemented" placeholder) and the populated-table case (header columns, body rows, API response shape). ### Verification - `go test ./cmd/server/...` green. - Local server with the e2e fixture: `GET /api/analytics/roles` returns `{"totalNodes":200,"roles":[{"role":"repeater","nodeCount":168,...},{"role":"room","nodeCount":23,...},{"role":"companion","nodeCount":9,...}]}`. Fixes #818 --------- Co-authored-by: corescope-bot <bot@corescope>
134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"math"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// RoleStats summarises one role's population and clock-skew posture.
|
|
type RoleStats struct {
|
|
Role string `json:"role"`
|
|
NodeCount int `json:"nodeCount"`
|
|
WithSkew int `json:"withSkew"`
|
|
MeanAbsSkewSec float64 `json:"meanAbsSkewSec"`
|
|
MedianAbsSkewSec float64 `json:"medianAbsSkewSec"`
|
|
OkCount int `json:"okCount"`
|
|
WarningCount int `json:"warningCount"`
|
|
CriticalCount int `json:"criticalCount"`
|
|
AbsurdCount int `json:"absurdCount"`
|
|
NoClockCount int `json:"noClockCount"`
|
|
}
|
|
|
|
// RoleAnalyticsResponse is the payload returned by /api/analytics/roles.
|
|
type RoleAnalyticsResponse struct {
|
|
TotalNodes int `json:"totalNodes"`
|
|
Roles []RoleStats `json:"roles"`
|
|
}
|
|
|
|
// normalizeRole canonicalises a role string so empty/unknown roles bucket
|
|
// together and case differences don't fragment the distribution.
|
|
func normalizeRole(r string) string {
|
|
r = strings.ToLower(strings.TrimSpace(r))
|
|
if r == "" {
|
|
return "unknown"
|
|
}
|
|
return r
|
|
}
|
|
|
|
// computeRoleAnalytics groups nodes by role and aggregates clock-skew per
|
|
// role. Pure function: takes the node roster and the per-pubkey skew map and
|
|
// returns the response — no store / lock dependencies, easy to unit test.
|
|
//
|
|
// `nodesByPubkey` lists every known node (pubkey → role). `skewByPubkey`
|
|
// is the subset of pubkeys that have clock-skew data with their severity and
|
|
// most-recent corrected skew (in seconds, signed — we take |x| for averages).
|
|
func computeRoleAnalytics(nodesByPubkey map[string]string, skewByPubkey map[string]*NodeClockSkew) RoleAnalyticsResponse {
|
|
type bucket struct {
|
|
stats RoleStats
|
|
absSkews []float64
|
|
}
|
|
buckets := make(map[string]*bucket)
|
|
for pk, rawRole := range nodesByPubkey {
|
|
role := normalizeRole(rawRole)
|
|
b, ok := buckets[role]
|
|
if !ok {
|
|
b = &bucket{stats: RoleStats{Role: role}}
|
|
buckets[role] = b
|
|
}
|
|
b.stats.NodeCount++
|
|
cs, has := skewByPubkey[pk]
|
|
if !has || cs == nil {
|
|
continue
|
|
}
|
|
b.stats.WithSkew++
|
|
abs := math.Abs(cs.RecentMedianSkewSec)
|
|
if abs == 0 {
|
|
abs = math.Abs(cs.LastSkewSec)
|
|
}
|
|
b.absSkews = append(b.absSkews, abs)
|
|
switch cs.Severity {
|
|
case SkewOK:
|
|
b.stats.OkCount++
|
|
case SkewWarning:
|
|
b.stats.WarningCount++
|
|
case SkewCritical:
|
|
b.stats.CriticalCount++
|
|
case SkewAbsurd:
|
|
b.stats.AbsurdCount++
|
|
case SkewNoClock:
|
|
b.stats.NoClockCount++
|
|
}
|
|
}
|
|
resp := RoleAnalyticsResponse{Roles: make([]RoleStats, 0, len(buckets))}
|
|
for _, b := range buckets {
|
|
if n := len(b.absSkews); n > 0 {
|
|
sum := 0.0
|
|
for _, v := range b.absSkews {
|
|
sum += v
|
|
}
|
|
b.stats.MeanAbsSkewSec = round(sum/float64(n), 2)
|
|
sorted := make([]float64, n)
|
|
copy(sorted, b.absSkews)
|
|
sort.Float64s(sorted)
|
|
if n%2 == 1 {
|
|
b.stats.MedianAbsSkewSec = round(sorted[n/2], 2)
|
|
} else {
|
|
b.stats.MedianAbsSkewSec = round((sorted[n/2-1]+sorted[n/2])/2, 2)
|
|
}
|
|
}
|
|
resp.TotalNodes += b.stats.NodeCount
|
|
resp.Roles = append(resp.Roles, b.stats)
|
|
}
|
|
// Sort: largest population first, then role name for stable output.
|
|
sort.Slice(resp.Roles, func(i, j int) bool {
|
|
if resp.Roles[i].NodeCount != resp.Roles[j].NodeCount {
|
|
return resp.Roles[i].NodeCount > resp.Roles[j].NodeCount
|
|
}
|
|
return resp.Roles[i].Role < resp.Roles[j].Role
|
|
})
|
|
return resp
|
|
}
|
|
|
|
// handleAnalyticsRoles serves /api/analytics/roles.
|
|
func (s *Server) handleAnalyticsRoles(w http.ResponseWriter, r *http.Request) {
|
|
if s.store == nil {
|
|
writeJSON(w, RoleAnalyticsResponse{Roles: []RoleStats{}})
|
|
return
|
|
}
|
|
nodes, _ := s.store.getCachedNodesAndPM()
|
|
roles := make(map[string]string, len(nodes))
|
|
for _, n := range nodes {
|
|
roles[n.PublicKey] = n.Role
|
|
}
|
|
skewMap := make(map[string]*NodeClockSkew)
|
|
for _, cs := range s.store.GetFleetClockSkew() {
|
|
if cs == nil {
|
|
continue
|
|
}
|
|
skewMap[cs.Pubkey] = cs
|
|
}
|
|
writeJSON(w, computeRoleAnalytics(roles, skewMap))
|
|
}
|