Sensor health in the API and UI; balloon tips when something starts going wrong
- telemetry: _source() now remembers each failing source's error message, not just the log timestamp; health() reports them and every snapshot carries a "health" key, so /api/state shows which sensor source is failing and since when. A source that reads again clears its entry. - Web UI: a header pill counts the failing sources; hovering lists the errors. - tray: notify() shows a balloon tip from any thread (queued on the tray window, NIF_INFO). app.py grows an alert monitor: every 5 s it diffs the current bad states (fan failsafes/control errors, AI backend errors, failing sensor sources) against the last look and balloons only what is new. The first look is the baseline: problems that predate the tray icon stay quiet. - Tests: test_telemetry.py (5) and test_alerts.py (7); README updated.
This commit is contained in:
1 parent
b2da2213b7
commit
ba39016d52
7 files changed
+292
-10
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from kurgan import tray
|
||||
from kurgan.app import alert_states, new_alerts
|
||||
|
||||
|
||||
def fake_fans(gpu_status="GPU 50 C, 40% PWM", top_status="55 C, 45%"):
|
||||
group = Mock()
|
||||
group.name = "GPU"
|
||||
group.status = gpu_status
|
||||
top = Mock()
|
||||
top.status = top_status
|
||||
fans = Mock()
|
||||
fans.groups = [group]
|
||||
fans.top = top
|
||||
return fans
|
||||
|
||||
|
||||
def fake_ai(*errors):
|
||||
ai = Mock()
|
||||
ai.snapshot.return_value = {"backends": [{"key": key, "label": key.capitalize(), "error": error}
|
||||
for key, error in errors]}
|
||||
return ai
|
||||
|
||||
|
||||
def fake_health(entries=None):
|
||||
telemetry = Mock()
|
||||
telemetry.health.return_value = entries or {}
|
||||
return telemetry
|
||||
|
||||
|
||||
class AlertStatesTest(unittest.TestCase):
|
||||
def test_healthy_everything_is_quiet(self):
|
||||
self.assertEqual(alert_states(fake_fans(), fake_ai(("strata", None), ("llama", None)),
|
||||
fake_health()), {})
|
||||
|
||||
def test_failsafes_errors_and_failing_sources_are_collected(self):
|
||||
states = alert_states(
|
||||
fake_fans(gpu_status="FAILSAFE 100%: GPU temperature unavailable",
|
||||
top_status="UNAVAILABLE (lost); BIOS curve keeps control"),
|
||||
fake_ai(("strata", "exited (code 3); restarting."), ("llama", None)),
|
||||
fake_health({"GPU sensors": {"error": "NVMLError: uninitialised", "age_s": 3.0}}))
|
||||
self.assertEqual(states, {
|
||||
"fans/GPU": "GPU case fans: FAILSAFE 100%: GPU temperature unavailable",
|
||||
"fans/top": "Top fans: UNAVAILABLE (lost); BIOS curve keeps control",
|
||||
"ai/strata": "Strata: exited (code 3); restarting.",
|
||||
"telemetry/GPU sensors": "GPU sensors read failed (NVMLError: uninitialised); continuing without it",
|
||||
})
|
||||
|
||||
def test_fans_or_ai_being_off_is_not_an_error(self):
|
||||
self.assertEqual(alert_states(None, None, fake_health()), {})
|
||||
|
||||
|
||||
class NewAlertsTest(unittest.TestCase):
|
||||
def test_only_new_keys_alert_and_recovery_is_quiet(self):
|
||||
known = {"fans/GPU": "an earlier message"}
|
||||
states = {"fans/GPU": "still failing", "ai/strata": "exited"}
|
||||
self.assertEqual(new_alerts(known, states), [("ai/strata", "exited")])
|
||||
self.assertEqual(new_alerts(states, {}), []) # everything recovered: nothing new
|
||||
self.assertEqual(new_alerts({}, states), [("ai/strata", "exited"), ("fans/GPU", "still failing")])
|
||||
|
||||
|
||||
class TrayNotifyTest(unittest.TestCase):
|
||||
def make_icon(self):
|
||||
return tray.TrayIcon("The Kurgan", "unused.ico", on_exit=lambda: None)
|
||||
|
||||
def test_notify_queues_a_balloon_and_pings_the_tray_thread(self):
|
||||
icon = self.make_icon()
|
||||
icon.notify("The Kurgan", "Top fans: FAILSAFE 100%", warning=True) # no window yet: just stored
|
||||
self.assertEqual(icon._info, ("The Kurgan", "Top fans: FAILSAFE 100%", tray.NIIF_WARNING))
|
||||
icon._hwnd = 123
|
||||
with patch.object(tray, "_u32") as u32:
|
||||
icon.notify("The Kurgan", "hello")
|
||||
u32.PostMessageW.assert_called_once_with(123, tray.WM_TRAY_INFO, 0, 0)
|
||||
self.assertEqual(icon._info[2], tray.NIIF_NONE)
|
||||
|
||||
def test_messages_are_cut_to_what_the_structure_holds(self):
|
||||
icon = self.make_icon()
|
||||
icon.notify("t" * 100, "m" * 300)
|
||||
title, message, _ = icon._info
|
||||
self.assertEqual((len(title), len(message)), (63, 255))
|
||||
|
||||
def test_the_balloon_fields_are_filled_and_shown(self):
|
||||
icon = self.make_icon()
|
||||
icon._hwnd = 1
|
||||
icon._info = ("The Kurgan", "GPU case fans: FAILSAFE 100%", tray.NIIF_WARNING)
|
||||
nid = icon._info_nid()
|
||||
self.assertEqual((nid.uFlags, nid.szInfoTitle, nid.szInfo, nid.dwInfoFlags),
|
||||
(tray.NIF_INFO, "The Kurgan", "GPU case fans: FAILSAFE 100%", tray.NIIF_WARNING))
|
||||
with patch.object(tray, "_shell32") as shell:
|
||||
icon._show_info()
|
||||
self.assertEqual(shell.Shell_NotifyIconW.call_args.args[0], tray.NIM_MODIFY)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,89 @@
|
||||
import threading
|
||||
import unittest
|
||||
|
||||
from kurgan.telemetry.readings import PowerReadings, Readings
|
||||
from kurgan.telemetry.service import Telemetry
|
||||
|
||||
|
||||
class FakeSource:
|
||||
"""A cpu/gpu-shaped source: readings, plus the memory query the GPU one provides."""
|
||||
|
||||
def __init__(self):
|
||||
self.fail = False
|
||||
self.memory_fails = False
|
||||
|
||||
def read(self, *args):
|
||||
if self.fail:
|
||||
raise OSError("device gone")
|
||||
return Readings(temp=50, load=10, clock=4000, power=30)
|
||||
|
||||
def memory(self):
|
||||
if self.memory_fails:
|
||||
raise OSError("nvml busy")
|
||||
return (8 * 1024 ** 3, 24 * 1024 ** 3)
|
||||
|
||||
|
||||
class FakeSystem:
|
||||
def __init__(self):
|
||||
self.fail = False
|
||||
|
||||
def read(self, cpu, gpu):
|
||||
if self.fail:
|
||||
raise OSError("commander gone")
|
||||
return PowerReadings(cpu=cpu.power, gpu=gpu.power, other=40, parts={"fans": 2.0})
|
||||
|
||||
|
||||
class TelemetryHealthTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.cpu, self.gpu, self.system = FakeSource(), FakeSource(), FakeSystem()
|
||||
self.telemetry = Telemetry(self.cpu, self.gpu, self.system)
|
||||
|
||||
def test_before_the_first_read_there_is_nothing_to_report(self):
|
||||
self.assertEqual(self.telemetry.snapshot(), {"seq": 0, "health": {}})
|
||||
|
||||
def test_a_healthy_run_has_no_health_entries(self):
|
||||
self.telemetry.read()
|
||||
self.assertEqual(self.telemetry.health(), {})
|
||||
self.assertEqual(self.telemetry.snapshot()["health"], {})
|
||||
|
||||
def test_a_failing_source_costs_only_itself_and_is_reported(self):
|
||||
self.cpu.fail = True
|
||||
with self.assertLogs("kurgan.telemetry.service", level="WARNING"):
|
||||
sample = self.telemetry.read()
|
||||
health = self.telemetry.health()
|
||||
self.assertEqual(list(health), ["CPU sensors"])
|
||||
self.assertIn("device gone", health["CPU sensors"]["error"])
|
||||
self.assertIsNone(sample.cpu.temp) # the fallback stands in ...
|
||||
self.assertEqual(sample.gpu.temp, 50) # ... while every other source still reads
|
||||
self.assertEqual(sample.power.total, 70) # GPU 30 W + other 40 W; the CPU part is missing
|
||||
|
||||
def test_recovery_clears_the_entry(self):
|
||||
self.gpu.fail = True
|
||||
with self.assertLogs("kurgan.telemetry.service", level="WARNING"):
|
||||
self.telemetry.read()
|
||||
self.assertEqual(list(self.telemetry.health()), ["GPU sensors"])
|
||||
self.gpu.fail = False
|
||||
self.telemetry.read()
|
||||
self.assertEqual(self.telemetry.health(), {})
|
||||
|
||||
def test_the_snapshot_carries_health(self):
|
||||
self.system.fail = True
|
||||
self.gpu.memory_fails = True
|
||||
first, stop = threading.Event(), threading.Event()
|
||||
self.telemetry.subscribe(lambda sample: first.set())
|
||||
with self.assertLogs("kurgan.telemetry.service", level="WARNING") as logs:
|
||||
thread = threading.Thread(target=self.telemetry.run, args=(stop,), daemon=True)
|
||||
thread.start()
|
||||
self.assertTrue(first.wait(5)) # the listener fires after read(): both warnings are out
|
||||
stop.set()
|
||||
thread.join(5)
|
||||
self.assertEqual(len(logs.output), 2) # each failing source is reported once
|
||||
health = self.telemetry.snapshot()["health"]
|
||||
self.assertEqual(set(health), {"system power", "GPU memory"})
|
||||
snapshot = self.telemetry.snapshot()
|
||||
self.assertIsNone(snapshot["gpu"]["vram_used"])
|
||||
self.assertEqual(snapshot["gpu"]["temp"], 50)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user