Live page: the speed of every peer right now

A new Live page shows current download and upload per peer, updated
every 2 seconds, with the last 2 minutes as a chart and a small chart
per peer. The server reads the WireGuard counters every 2 seconds and
keeps 2 minutes in memory; GET /api/v1/live serves them, with since=
for only the newer steps. The dev simulator now adds traffic in
proportion to the time between samples.
This commit is contained in:
Daniel Redetzke
2026-10-05 14:28:29 +03:00
parent c78824b0e9
commit e01b73d677
8 changed files with 364 additions and 10 deletions
+14
View File
@@ -21,6 +21,7 @@ type App struct {
kernel Kernel
recon *Reconciler
stats *Stats
speeds *Speeds // nil in tests
auth *Auth
tls *webTLS
logPath string
@@ -144,6 +145,7 @@ func (a *App) routes() http.Handler {
g("GET /api/v1/status", a.status)
g("GET /api/v1/stats", a.allStats)
g("GET /api/v1/live", a.liveSpeeds)
g("GET /api/v1/server", a.getServer)
g("PATCH /api/v1/server", a.patchServer)
@@ -398,6 +400,18 @@ func (a *App) allStats(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{"range": rng, "points": a.stats.series(nil, rng)})
}
// liveSpeeds returns each peer's speed over the last 2 minutes; with since
// (unix seconds) only the newer steps.
func (a *App) liveSpeeds(w http.ResponseWriter, r *http.Request) {
var since int64
fmt.Sscan(r.URL.Query().Get("since"), &since)
points := []SpeedPoint{}
if a.speeds != nil {
points = a.speeds.Since(since)
}
writeJSON(w, http.StatusOK, map[string]any{"step": int(speedStep / time.Second), "size": speedPoints, "points": points})
}
func (a *App) peerStats(w http.ResponseWriter, r *http.Request) {
cfg := a.store.Get()
if _, p := cfg.peerByID(r.PathValue("id")); p == nil {