add realtime upload/download host stats

This commit is contained in:
legop3
2026-07-19 20:52:50 -04:00
parent cacd125fcb
commit eba4b1dc1d
10 changed files with 363 additions and 167 deletions
+93 -6
View File
@@ -3,6 +3,7 @@ package roverd
import (
"bufio"
"context"
"errors"
"fmt"
"math"
"os"
@@ -14,7 +15,7 @@ import (
)
const (
hostStatsInterval = 5 * time.Second
hostStatsInterval = 1 * time.Second
rootFilesystem = "/"
)
@@ -63,7 +64,24 @@ type WiFiStats struct {
TXBytes *uint64 `json:"txBytes,omitempty"`
RXPackets *uint64 `json:"rxPackets,omitempty"`
TXPackets *uint64 `json:"txPackets,omitempty"`
DownloadMbps *float64 `json:"downloadMbps,omitempty"`
UploadMbps *float64 `json:"uploadMbps,omitempty"`
InactiveMs *int `json:"inactiveMs,omitempty"`
// networkSampledAt records the instant associated with the kernel byte
// counters. Keeping it out of JSON lets the websocket loop calculate rates
// with monotonic Go timestamps without expanding the browser contract with
// an implementation-only value.
networkSampledAt time.Time
}
// networkRateSample is scoped to one rover websocket connection. A new
// connection intentionally starts a new baseline so counters from an old boot
// or network interface lifetime can never create an artificial traffic spike.
type networkRateSample struct {
rxBytes uint64
txBytes uint64
sampledAt time.Time
}
// CollectHostStats gathers every source independently so one missing kernel
@@ -370,12 +388,81 @@ func collectWiFiStats(ctx context.Context) (*WiFiStats, error) {
return nil, err
}
// The interface is used only to ask iw about the active connection. It is
// not copied into WiFiStats because the UI does not need to expose it.
if err := enrichWiFiWithIW(ctx, iface, stats); err != nil {
return stats, err
// The interface is used only for local collection. It is not copied into
// WiFiStats because the UI does not need to expose Linux device names.
iwErr := enrichWiFiWithIW(ctx, iface, stats)
// Read the kernel counters after iw because iw also provides cumulative
// station counters. The kernel interface values deliberately win: they are
// the host-traffic source used for both the cumulative display and Mbps math.
// Link capacity still comes independently from iw's bitrate fields.
counterErr := enrichWiFiWithNetworkCounters(iface, stats)
return stats, errors.Join(counterErr, iwErr)
}
func enrichWiFiWithNetworkCounters(iface string, stats *WiFiStats) error {
basePath := "/sys/class/net/" + iface + "/statistics/"
rxBytes, err := readUintFile(basePath + "rx_bytes")
if err != nil {
return fmt.Errorf("read %s receive bytes: %w", iface, err)
}
return stats, nil
txBytes, err := readUintFile(basePath + "tx_bytes")
if err != nil {
return fmt.Errorf("read %s transmit bytes: %w", iface, err)
}
stats.RXBytes = &rxBytes
stats.TXBytes = &txBytes
// Capture the timestamp immediately beside the counter reads so unrelated
// host-stat collection latency cannot distort the elapsed-time divisor.
stats.networkSampledAt = time.Now()
return nil
}
func readUintFile(path string) (uint64, error) {
raw, err := os.ReadFile(path)
if err != nil {
return 0, err
}
return strconv.ParseUint(strings.TrimSpace(string(raw)), 10, 64)
}
func applyNetworkThroughput(stats *WiFiStats, previous *networkRateSample) *networkRateSample {
if stats == nil || stats.RXBytes == nil || stats.TXBytes == nil || stats.networkSampledAt.IsZero() {
// Do not discard the last valid baseline during a temporary read failure.
// The next successful calculation then covers the full elapsed interval and
// remains an accurate average for all traffic transferred during the gap.
return previous
}
current := &networkRateSample{
rxBytes: *stats.RXBytes,
txBytes: *stats.TXBytes,
sampledAt: stats.networkSampledAt,
}
if previous == nil {
return current
}
elapsed := current.sampledAt.Sub(previous.sampledAt).Seconds()
// Linux counters can return to zero after an interface reset. Re-baselining
// on any decrease prevents unsigned underflow from becoming a huge false
// throughput spike in the host-stat card.
if elapsed <= 0 || current.rxBytes < previous.rxBytes || current.txBytes < previous.txBytes {
return current
}
downloadMbps := bytesToMbps(current.rxBytes-previous.rxBytes, elapsed)
uploadMbps := bytesToMbps(current.txBytes-previous.txBytes, elapsed)
stats.DownloadMbps = &downloadMbps
stats.UploadMbps = &uploadMbps
return current
}
func bytesToMbps(byteDelta uint64, elapsedSeconds float64) float64 {
// Mbps uses decimal megabits, matching network equipment and link-rate
// conventions: eight bits per byte and 1,000,000 bits per megabit.
return roundOneDecimal((float64(byteDelta) * 8) / elapsedSeconds / 1_000_000)
}
func readWirelessStats() (string, *WiFiStats, error) {
+79
View File
@@ -0,0 +1,79 @@
package roverd
import (
"testing"
"time"
)
func TestApplyNetworkThroughputCalculatesMbpsFromActualElapsedTime(t *testing.T) {
startedAt := time.Unix(100, 0)
previous := &networkRateSample{rxBytes: 1_000, txBytes: 2_000, sampledAt: startedAt}
rxBytes := uint64(2_001_000)
txBytes := uint64(1_002_000)
stats := &WiFiStats{
RXBytes: &rxBytes,
TXBytes: &txBytes,
networkSampledAt: startedAt.Add(2 * time.Second),
}
next := applyNetworkThroughput(stats, previous)
if stats.DownloadMbps == nil || *stats.DownloadMbps != 8.0 {
t.Fatalf("expected 8.0 Mbps download, got %v", stats.DownloadMbps)
}
if stats.UploadMbps == nil || *stats.UploadMbps != 4.0 {
t.Fatalf("expected 4.0 Mbps upload, got %v", stats.UploadMbps)
}
if next == nil || next.rxBytes != rxBytes || next.txBytes != txBytes {
t.Fatalf("expected current counters to become the next baseline, got %#v", next)
}
}
func TestApplyNetworkThroughputFirstSampleOnlyEstablishesBaseline(t *testing.T) {
rxBytes := uint64(100)
txBytes := uint64(200)
stats := &WiFiStats{RXBytes: &rxBytes, TXBytes: &txBytes, networkSampledAt: time.Unix(100, 0)}
next := applyNetworkThroughput(stats, nil)
if stats.DownloadMbps != nil || stats.UploadMbps != nil {
t.Fatalf("expected no rates for the first sample, got download=%v upload=%v", stats.DownloadMbps, stats.UploadMbps)
}
if next == nil {
t.Fatal("expected the first valid sample to establish a baseline")
}
}
func TestApplyNetworkThroughputCounterResetEstablishesNewBaseline(t *testing.T) {
startedAt := time.Unix(100, 0)
previous := &networkRateSample{rxBytes: 10_000, txBytes: 20_000, sampledAt: startedAt}
rxBytes := uint64(10)
txBytes := uint64(20)
stats := &WiFiStats{RXBytes: &rxBytes, TXBytes: &txBytes, networkSampledAt: startedAt.Add(time.Second)}
next := applyNetworkThroughput(stats, previous)
if stats.DownloadMbps != nil || stats.UploadMbps != nil {
t.Fatalf("expected no rates after a counter reset, got download=%v upload=%v", stats.DownloadMbps, stats.UploadMbps)
}
if next == nil || next.rxBytes != rxBytes || next.txBytes != txBytes {
t.Fatalf("expected reset counters to become the new baseline, got %#v", next)
}
}
func TestApplyNetworkThroughputInvalidElapsedTimeEstablishesNewBaseline(t *testing.T) {
sampledAt := time.Unix(100, 0)
previous := &networkRateSample{rxBytes: 100, txBytes: 200, sampledAt: sampledAt}
rxBytes := uint64(200)
txBytes := uint64(300)
stats := &WiFiStats{RXBytes: &rxBytes, TXBytes: &txBytes, networkSampledAt: sampledAt}
next := applyNetworkThroughput(stats, previous)
if stats.DownloadMbps != nil || stats.UploadMbps != nil {
t.Fatalf("expected no rates with zero elapsed time, got download=%v upload=%v", stats.DownloadMbps, stats.UploadMbps)
}
if next == nil || next.sampledAt != sampledAt {
t.Fatalf("expected invalid timing sample to become the new baseline, got %#v", next)
}
}
+8 -1
View File
@@ -531,14 +531,21 @@ func (c *WSClient) forwardEvents(ctx context.Context, conn *websocket.Conn) {
}
func (c *WSClient) forwardHostStats(ctx context.Context, conn *websocket.Conn) {
var previousNetworkSample *networkRateSample
send := func() bool {
// Host stats are collected on demand so each outbound message describes
// the current Pi state. Collection failures are encoded into the stats
// payload, which keeps this telemetry path from closing the rover socket.
stats := CollectHostStats(ctx)
// Throughput is derived here because this loop owns the ordered, periodic
// samples for one connection. CollectHostStats stays independent, while a
// reconnect automatically receives a clean counter baseline.
previousNetworkSample = applyNetworkThroughput(stats.WiFi, previousNetworkSample)
msg := hostStatsMessage{
Type: "hostStats",
Timestamp: time.Now().UnixMilli(),
Stats: CollectHostStats(ctx),
Stats: stats,
}
if err := writeJSON(ctx, conn, msg); err != nil {
c.log.Printf("host stats send failed: %v", err)
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -78,7 +78,7 @@
<script defer src="https://analytics.otter.land/script.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land"></script>
<script defer src="https://analytics.otter.land/recorder.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land" data-sample-rate="0.15" data-mask-level="moderate" data-max-duration="300000"></script>
<title>Roomba Rover</title>
<script type="module" crossorigin src="/assets/index-DY2RJqPm.js"></script>
<script type="module" crossorigin src="/assets/index-C-g10Rjz.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BwjDTdpq.css">
</head>
<body>
+7 -3
View File
@@ -1,9 +1,13 @@
1. assign rovers based on battery percentage, give people highest one
2. add admin ui for VIP and private requests instead of only through discord
3. add flag in roverd for video aspect ratio
1. either 4:3 or 16:9
2. default is 4:3
3. all it does is tell the web UI to make the rover video 16:9 or 4:3 shaped
1. maybe dont? whats the point anyway? why do we exist at all? is there purpose to life?
1. just removing the black bars, doesnt do anything practical for the driver page
2. would only actually help for keeping spectate page compact
1. maybe just make the spectate videos be fixed width and match the height of the media
2. either 4:3 or 16:9
3. default is 4:3
4. all it does is tell the web UI to make the rover video 16:9 or 4:3 shaped
1. web UI should default to 4:3 if that rover doesnt yet have that config yet
4. fix this:
`Jun 18 15:14:18 roombaserver.local node[216731]: /home/daniel/MultiRoombaRover/server/src/services/roverManager/socketHandlers.js:92
+26 -7
View File
@@ -35,6 +35,8 @@ const PLACEHOLDER_STATS = Object.freeze({
qualityMax: 70,
rxBitrateMbit: 72.2,
txBitrateMbit: 58.5,
downloadMbps: 12.4,
uploadMbps: 3.7,
rxBytes: 12400000,
txBytes: 2300000,
rxPackets: 12640,
@@ -304,14 +306,22 @@ export default function PiHostStatsCard() {
<section className="min-w-0 space-y-0.5">
<ColumnTitle label="WiFi" />
<div className="surface grid min-w-0 grid-cols-[minmax(0,1fr)_auto] items-start gap-1">
<div className="min-w-0">
<div className="truncate text-base leading-tight text-slate-100">SSID: {valueOrDash(wifi.ssidSample)}</div>
<div className="text-xs text-slate-400">{formatFrequency(wifi.frequencyMhz)}</div>
<div className="grid min-w-0 grid-cols-2 gap-0.5">
<div className="surface grid min-w-0 grid-cols-[minmax(0,1fr)_auto] items-start gap-1">
<div className="min-w-0">
<div className="truncate text-base leading-tight text-slate-100">SSID: {valueOrDash(wifi.ssidSample)}</div>
<div className="text-xs text-slate-400">{formatFrequency(wifi.frequencyMhz)}</div>
</div>
<div className="text-right">
<div className={`font-semibold leading-tight ${toneTextClass(currentSignalTone)}`}>{formatDbm(wifi.signalDbm)}</div>
<SignalBars bars={bars} tone={currentSignalTone} />
</div>
</div>
<div className="text-right">
<div className={`font-semibold leading-tight ${toneTextClass(currentSignalTone)}`}>{formatDbm(wifi.signalDbm)}</div>
<SignalBars bars={bars} tone={currentSignalTone} />
<div className="surface flex min-w-0 flex-col justify-center gap-0.5 text-xs">
{/* Actual traffic belongs beside connection identity and signal,
while negotiated link rates remain in their existing rows. */}
<ThroughputRow label="Download" value={formatBitrate(wifi.downloadMbps)} />
<ThroughputRow label="Upload" value={formatBitrate(wifi.uploadMbps)} />
</div>
</div>
<BarRow
@@ -347,6 +357,15 @@ function StatRow({ label, value, compact = false, valueClassName = 'text-slate-1
);
}
function ThroughputRow({ label, value }) {
return (
<div className="flex min-w-0 items-center justify-between gap-1">
<span className="shrink-0 text-slate-400">{label}</span>
<span className="min-w-0 truncate text-right text-slate-100">{value}</span>
</div>
);
}
function BarRow({ label, value, detail = null, percent, tone = 'neutral' }) {
return (
<div className="surface">