store compact RSSI stats and skip real-time collection

Stats.WiFi is now map[string]int8 (json "wf") holding only available
RSSI
readings; SSID and unavailable signals stay in Info.WiFi. Averages are
rounded to whole dBm.

Wi-Fi is collected only on the default 60s interval; real-time requests
reuse the last snapshot to avoid spawning osascript / dumping the BSS
cache every second.
This commit is contained in:
henrygd
2026-09-25 18:02:34 -04:00
parent 86ab0fae8b
commit 16e3fbadce
12 changed files with 86 additions and 37 deletions

View File

@@ -267,8 +267,7 @@ func AverageSystemStatsSlice(records []system.Stats) system.Stats {
return sum
}
// RSSI averages exclude absent and unavailable samples.
wifiSums := make(map[string]float64)
wifiSums := make(map[string]int)
wifiCounts := make(map[string]int)
// necessary because uint8 is not big enough for the sum
batterySum := 0
@@ -288,15 +287,9 @@ func AverageSystemStatsSlice(records []system.Stats) system.Stats {
// Accumulate totals
for i := range records {
stats := &records[i]
for id, reading := range stats.WiFi {
if sum.WiFi == nil {
sum.WiFi = make(map[string]system.WiFi)
}
sum.WiFi[id] = system.WiFi{SSID: reading.SSID}
if reading.Signal != nil {
wifiSums[id] += *reading.Signal
wifiCounts[id]++
}
for id, signal := range stats.WiFi {
wifiSums[id] += int(signal)
wifiCounts[id]++
}
sum.Cpu += stats.Cpu
@@ -627,11 +620,11 @@ func AverageSystemStatsSlice(records []system.Stats) system.Stats {
sum.CpuBreakdown = avg
}
for id, reading := range sum.WiFi {
if wifiCounts[id] > 0 {
average := wifiSums[id] / float64(wifiCounts[id])
reading.Signal = &average
sum.WiFi[id] = reading
// RSSI averages exclude records where the interface was absent.
if len(wifiSums) > 0 {
sum.WiFi = make(map[string]int8, len(wifiSums))
for id, total := range wifiSums {
sum.WiFi[id] = int8(math.Round(float64(total) / float64(wifiCounts[id])))
}
}

View File

@@ -7,17 +7,16 @@ import (
)
func TestWiFiAverageAvailableSamples(t *testing.T) {
a, b, c := -40.0, -60.0, -80.0
input := []system.Stats{
{WiFi: map[string]system.WiFi{"wlan0": {SSID: "old", Signal: &a}}},
{WiFi: map[string]int8{"wlan0": -40}},
{},
{WiFi: map[string]system.WiFi{"wlan0": {SSID: "new", Signal: &b}, "wlan1": {Signal: &c}, "unknown": {}}},
{WiFi: map[string]int8{"wlan0": -61, "wlan1": -80}},
}
result := AverageSystemStatsSlice(input)
if len(result.WiFi) != 3 || *result.WiFi["wlan0"].Signal != -50 || *result.WiFi["wlan1"].Signal != -80 || result.WiFi["unknown"].Signal != nil || result.WiFi["wlan0"].SSID != "new" {
if len(result.WiFi) != 2 || result.WiFi["wlan0"] != -51 || result.WiFi["wlan1"] != -80 {
t.Fatalf("%#v", result.WiFi)
}
if *input[0].WiFi["wlan0"].Signal != -40 {
if input[0].WiFi["wlan0"] != -40 {
t.Fatal("mutated input")
}
if len(AverageSystemStatsSlice([]system.Stats{{}, {}}).WiFi) != 0 {