Files
hamlab/internal/api/network.go
Mitchell-JohnsonandClaude Opus 5 2f59a54678 A Network tab, with an internet speed test
Per machine like every other tab: the interfaces as the kernel has them
(addresses, MAC, MTU, negotiated link speed, carrier, totals, live rates from
the sampler's own sensors), the default route and resolvers, what the internet
sees of this connection, and a speed test with the last fifty results kept.

The test runs in the daemon, not the browser, so the figure is the line of the
machine in the switcher — the router's, if you switch to gate — and it carries
on when the phone locks. It measures against Cloudflare's public speed
endpoints, so there is nothing to install on any host, which is what makes it
work the same on Debian and on OpenWrt where flash is scarce.

What the measurement does and does not count:

  - Several streams, HTTP/2 deliberately off. One stream over one connection
    measures a congestion window, and HTTP/2 would put every stream back onto
    one connection and measure the same thing.
  - The first second of each phase is thrown away as the ramp.
  - A phase ends on 64 MiB as well as on four seconds, so a fast line says what
    it is without moving a gigabyte. A minimum window keeps a very fast line
    from filling the cap before there is anything to divide by.
  - Latency is the round trip less the far end's own Server-Timing. A zero-byte
    response is mostly Cloudflare's worker thinking: left in, it reported 31 ms
    for a line the edge itself measures at 3.5. Jitter is the mean change
    between consecutive probes, so one slow probe cannot run away with it.
  - The far end rate-limits by volume. So: a 20 s cooldown counted from the last
    attempt rather than the last success, streams that retry twice, the rate
    computed over the window in which bytes were actually still arriving, and a
    plain sentence when it was refused. Coasting to the end of the clock with
    nothing arriving reported a third of the truth.

The last result is published as net.internet.* sensors, each stamped with when
the test ran, so the line can be a widget, a graph, an alert or a Prometheus
series like anything else. The public address is refreshed on its own quarter
hour, which is worth having here: it is a DHCP lease and it moves.

Tests are offline — the window, the byte cap, the refusal and the cancellation
run against a local server; the route parser's byte order is pinned by a case
that would have caught the bug it did. The live measurement is behind
HAMLAB_NET_TEST=1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-27 20:11:03 -07:00

124 lines
4.3 KiB
Go

package api
// The Network tab: the machine's interfaces, how the internet looks from here, and the speed
// test. One machine at a time, like every other tab — the test runs on the machine in the
// switcher, so the number is that machine's line and not the hub's.
import (
"encoding/json"
"log"
"net/http"
"time"
"hamlab/internal/network"
"hamlab/internal/sensors"
"hamlab/internal/store"
)
// networkOverview is everything the tab draws at once: the interfaces, the outside view, the
// last test and the ones before it.
func (s *Server) networkOverview(w http.ResponseWriter, _ *http.Request) {
run := s.net.Test.State()
jsonOK(w, 200, map[string]any{
"report": network.Look(s.hostName()),
"outside": s.net.Outside(),
"run": run,
"history": s.net.Test.History(),
"method": network.Method,
})
}
func (s *Server) speedtestState(w http.ResponseWriter, _ *http.Request) {
jsonOK(w, 200, s.net.Test.State())
}
func (s *Server) speedtestStart(w http.ResponseWriter, r *http.Request) {
run, err := s.net.Test.Start()
if err != nil {
jsonErr(w, 409, err.Error())
return
}
log.Printf("speed test started on %s (by %s)", s.hostName(), clientIP(r))
jsonOK(w, 200, run)
}
func (s *Server) speedtestCancel(w http.ResponseWriter, r *http.Request) {
if !s.net.Test.Cancel() {
jsonErr(w, 404, "no speed test is running")
return
}
log.Printf("speed test cancelled on %s (by %s)", s.hostName(), clientIP(r))
w.WriteHeader(204)
}
// ---- keeping the results ----
// savedNetwork is what survives a restart: the recent results, so the history is a history
// rather than whatever has happened since the daemon last started.
type savedNetwork struct {
Results []network.Result `json:"results,omitempty"`
}
func (s *Server) loadNetwork() []network.Result {
var saved savedNetwork
var raw json.RawMessage
s.store.View(func(st *store.State) { raw = st.Network })
if len(raw) > 0 {
if err := json.Unmarshal(raw, &saved); err != nil {
log.Printf("network: kept results unreadable (%v); starting a fresh history", err)
}
}
return saved.Results
}
// keepResult is called when a test finishes: append it to what is on disk.
func (s *Server) keepResult(r network.Result) {
if r.Error != "" {
log.Printf("speed test on %s failed: %s", s.hostName(), r.Error)
} else {
log.Printf("speed test on %s: down %s, up %s, latency %.1f ms via %s",
s.hostName(), network.Mbits(r.DownMbps), network.Mbits(r.UpMbps), r.LatencyMs, r.Colo)
}
results := s.net.Test.History()
b, err := json.Marshal(savedNetwork{Results: results})
if err != nil {
return
}
if err := s.store.Update(func(st *store.State) { st.Network = b }); err != nil {
log.Printf("network: %v", err)
}
}
// ---- the speed test as sensors ----
// netSensors publishes the last speed test as ordinary sensors, so the line can be a widget,
// a graph, an alert or a Prometheus series like anything else Ham Lab measures. Each one's
// timestamp is when the test ran, which is how a stale reading says so.
type netSensors struct{ w *network.Watcher }
func (n netSensors) Sensors() []sensors.Sensor {
var out []sensors.Sensor
if o := n.w.Outside(); o.IP != "" {
at := o.At.UnixMilli()
out = append(out, sensors.Sensor{ID: "net.internet.ip", Name: "Public address", Kind: "state", Value: o.IP, T: at})
if o.Colo != "" {
out = append(out, sensors.Sensor{ID: "net.internet.edge", Name: "Internet edge", Kind: "state", Value: o.Colo, T: at})
}
}
run := n.w.Test.State()
if run.Result == nil || run.Result.Error != "" {
return out
}
r := run.Result
at := r.At.UnixMilli()
return append(out,
sensors.Sensor{ID: "net.internet.download", Name: "Download", Kind: "rate", Unit: "Mbps", Value: r.DownMbps, T: at, Min: sensors.F(0)},
sensors.Sensor{ID: "net.internet.upload", Name: "Upload", Kind: "rate", Unit: "Mbps", Value: r.UpMbps, T: at, Min: sensors.F(0)},
sensors.Sensor{ID: "net.internet.latency", Name: "Latency", Kind: "duration", Unit: "ms", Value: r.LatencyMs, T: at, Min: sensors.F(0)},
sensors.Sensor{ID: "net.internet.jitter", Name: "Jitter", Kind: "duration", Unit: "ms", Value: r.JitterMs, T: at, Min: sensors.F(0)},
sensors.Sensor{ID: "net.internet.tested", Name: "Speed test age", Kind: "duration", Unit: "s", Value: round1(time.Since(r.At).Seconds()), T: at, Min: sensors.F(0)},
)
}
func round1(v float64) float64 { return float64(int64(v*10+0.5)) / 10 }