mirror of
https://github.com/henrygd/beszel.git
synced 2026-10-02 06:17:47 +02:00
feat: monitor connected Wi-Fi signal per interface (#2367)
This commit is contained in:
@@ -267,6 +267,9 @@ func AverageSystemStatsSlice(records []system.Stats) system.Stats {
|
||||
return sum
|
||||
}
|
||||
|
||||
// RSSI averages exclude absent and unavailable samples.
|
||||
wifiSums := make(map[string]float64)
|
||||
wifiCounts := make(map[string]int)
|
||||
// necessary because uint8 is not big enough for the sum
|
||||
batterySum := 0
|
||||
batteryCount := 0
|
||||
@@ -285,6 +288,16 @@ 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]++
|
||||
}
|
||||
}
|
||||
|
||||
sum.Cpu += stats.Cpu
|
||||
// accumulate cpu time breakdowns if present
|
||||
@@ -614,6 +627,14 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
|
||||
26
internal/records/records_wifi_test.go
Normal file
26
internal/records/records_wifi_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package records
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/henrygd/beszel/internal/entities/system"
|
||||
)
|
||||
|
||||
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]system.WiFi{"wlan0": {SSID: "new", Signal: &b}, "wlan1": {Signal: &c}, "unknown": {}}},
|
||||
}
|
||||
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" {
|
||||
t.Fatalf("%#v", result.WiFi)
|
||||
}
|
||||
if *input[0].WiFi["wlan0"].Signal != -40 {
|
||||
t.Fatal("mutated input")
|
||||
}
|
||||
if len(AverageSystemStatsSlice([]system.Stats{{}, {}}).WiFi) != 0 {
|
||||
t.Fatal("invented wifi")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user