mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-09-11 14:25:54 +00:00
Rebase of #1881 by @SaarMesh-Bot onto current master. Their three commits are preserved, two of them cherry-picked with authorship intact; the sweep itself had to be regenerated. Opened as a new PR rather than force-pushing their branch. Closes #1881 once merged. Addresses parts 1 and 3 of #1859; part 2 landed as #1937. ## Why regenerated rather than merged The sweep in #1881 was cut on 2026-09-02 07:13 and roughly forty PRs landed after it, so it went `CONFLICTING/DIRTY`. Re-running `gofmt` on current master is cheaper and less error-prone than resolving 72 conflicts that are all whitespace. The drift it fixes also grew in the meantime: 66 files now, against 72 then, but spread differently. ## The three commits 1. **`style(#1859)`** — `gofmt -w` across the 14 modules. 66 files. 2. **`test(#1859)`** — @SaarMesh-Bot's fix for the one `go vet` copylocks finding, `cmd/ingestor/coverage_boost_test.go`: the range variable copied a `Config` embedding `sync.Once`. Cherry-picked unchanged. 3. **`ci(#1859)`** — @SaarMesh-Bot's CI step that fails on gofmt drift or vet findings, plus `.git-blame-ignore-revs`. Cherry-picked with one change, noted in the commit message: the ignore file pointed at `04bc80ee`, the sweep commit on their branch, which does not exist on this base and would make `git blame --ignore-revs-file` error. Repointed at `d3a02599`, the sweep here. ## Verification The claim "formatting only" is checked twice rather than asserted: - Every changed file is byte-identical to `gofmt(previous content)`. 0 of 66 deviate. - With line comments and all whitespace stripped, 0 of 66 files differ, so no code outside comments changed. 14 of the 66 also show doc-comment reflow. Since Go 1.19 `gofmt` re-indents indented comment blocks to tabs and inserts a blank comment line before them; the behavior matrix above `resolveHopWithContext` in `cmd/ingestor/path_resolver.go` is a clear example. That is gofmt's own output, not an edit, but it is worth naming because it makes the diff look larger than "whitespace" suggests. The gate was run locally exactly as the workflow runs it: `gofmt` clean, and `go vet` clean in all 14 modules, including `cmd/ingestor` which is what commit 2 fixes. Suites: `cmd/server` ok (80.7s), `internal/packetpath` ok (2.3s), `cmd/ingestor` passes except `TestWriteStatsAtomic_SymlinkAtDestIsReplaced`, which fails identically on bare master with "A required privilege is not held by the client" (Windows symlink privilege on my host, not code). ## Sequencing This should go last in the queue. The sweep touches 66 files, so merging it before the remaining open Go PRs gives each of them a conflict about nothing but formatting. After it lands the gate is active, and any PR with drift fails CI until it runs `gofmt -w`. Excluded from the sweep: the misnamed `Dockerfile.go`, which is a Dockerfile that gofmt cannot parse (the workflow excludes it too), and `docs/DEPLOYMENT.md`, which a case-insensitive filesystem surfaces as a spurious modification against `docs/deployment.md` and is unrelated. --------- Co-authored-by: SaarMesh-Bot <300107934+SaarMesh-Bot@users.noreply.github.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
167 lines
5.1 KiB
Go
167 lines
5.1 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. Reads from the
|
|
// steady-state recomputer snapshot (issue #1256) so the request never
|
|
// holds s.mu.RLock for a full clock-skew recompute over the advert
|
|
// transmissions — that path hung >60s on staging with 78k tx.
|
|
func (s *Server) handleAnalyticsRoles(w http.ResponseWriter, r *http.Request) {
|
|
if s.store == nil {
|
|
writeJSON(w, RoleAnalyticsResponse{Roles: []RoleStats{}})
|
|
return
|
|
}
|
|
writeJSON(w, s.store.GetAnalyticsRoles())
|
|
}
|
|
|
|
// GetAnalyticsRoles returns the role-distribution analytics, preferring
|
|
// the steady-state recomputer snapshot (issue #1256). Falls back to an
|
|
// on-request compute path if the recomputer is not yet running (e.g.
|
|
// during the brief startup window before the initial compute completes
|
|
// — Start runs it synchronously, so this fallback is effectively only
|
|
// hit in tests that skip the recomputer entirely).
|
|
func (s *PacketStore) GetAnalyticsRoles() RoleAnalyticsResponse {
|
|
s.analyticsRecomputerMu.RLock()
|
|
rc := s.recompRoles
|
|
s.analyticsRecomputerMu.RUnlock()
|
|
if rc != nil {
|
|
if v := rc.Load(); v != nil {
|
|
if r, ok := v.(RoleAnalyticsResponse); ok {
|
|
s.cacheMu.Lock()
|
|
s.cacheHits++
|
|
s.cacheMu.Unlock()
|
|
return r
|
|
}
|
|
}
|
|
}
|
|
return s.computeAnalyticsRoles()
|
|
}
|
|
|
|
// computeAnalyticsRoles runs the actual role aggregation. Used by the
|
|
// background recomputer (issue #1256) and as a fallback for callers
|
|
// arriving before the snapshot is populated.
|
|
func (s *PacketStore) computeAnalyticsRoles() RoleAnalyticsResponse {
|
|
nodes, _ := s.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.GetFleetClockSkew("") {
|
|
if cs == nil {
|
|
continue
|
|
}
|
|
skewMap[cs.Pubkey] = cs
|
|
}
|
|
return computeRoleAnalytics(roles, skewMap)
|
|
}
|