mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
Warn in the navbar when a hard drive runs hot (#470)
* Warn in the navbar when a hard drive runs hot. The navbar already warns about high CPU temperature; add the equivalent for drive temperature, since an overheating disk risks data loss and a user running an off-grid library won't be watching for it. - controller/lib/smart.py: build_smart_stats() returns the per-drive list plus warning thresholds (55C warn / 65C critical) so the frontend stays dumb. Reading SMART wakes a spun-down drive, so drives in standby are detected with `smartctl -n standby` (which works through the USB-SATA bridges where `hdparm -C` only reports "unknown") and NOT read; their last-known reading is reused instead. - controller/lib/status_worker.py: collect SMART on a slow 60s cadence (cached on app.state) rather than the 5s status loop, so the poll does not keep USB drives awake. The result flows into the same cached payload via a new collect_all_status(smart_stats=...) kwarg. - Frontend: useDriveTemperature() reports the hottest drive; Nav.js shows a yellow/red `hdd` icon with a popup, mirroring the CPU warning. Verified on hardware: smart_stats reports real temps and SMART runs once a minute, not every 5s. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Address Greptile review on HDD temperature warning. - _drive_is_asleep also treats SLEEP mode as asleep, not just STANDBY. smartctl -n standby exits "Device is in SLEEP mode, exit(2)" for the deeper power mode; matching only "STANDBY" meant a SLEEP drive was read and woken — the exact failure the check prevents. (TDD: added a failing SLEEP test, then fixed.) - useDriveTemperature uses ?? instead of || for the temperature fallback to express "fall back only on null/undefined". Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b112f07b4a
commit
51f89593a4
8 changed files with 450 additions and 8 deletions
|
|
@ -20,11 +20,29 @@ class TestCollectAllStatus:
|
|||
|
||||
for key in (
|
||||
"cpu_stats", "memory_stats", "load_stats", "drives_stats",
|
||||
"nic_bandwidth_stats", "power_stats", "last_status",
|
||||
"nic_bandwidth_stats", "power_stats", "smart_stats", "last_status",
|
||||
):
|
||||
assert key in status
|
||||
datetime.fromisoformat(status["last_status"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_smart_stats_passed_through(self):
|
||||
"""The smart_stats kwarg should land in the cached payload verbatim."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
smart = {"drives": [{"device": "sda", "temperature": 52}],
|
||||
"high_temperature": 55, "critical_temperature": 65}
|
||||
status = await collect_all_status(smart_stats=smart)
|
||||
assert status["smart_stats"] is smart
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_smart_stats_defaults_to_none(self):
|
||||
"""Without the kwarg, smart_stats is None (worker fills it in)."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
status = await collect_all_status()
|
||||
assert status["smart_stats"] is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("collector,result_key,failure_value", [
|
||||
("get_cpu_status", "cpu_stats", None),
|
||||
|
|
@ -48,6 +66,65 @@ class TestCollectAllStatus:
|
|||
assert any(status[k] is not None for k in other_keys)
|
||||
|
||||
|
||||
class TestRefreshSmartStats:
|
||||
"""SMART must be collected on a slow cadence, not every status cycle."""
|
||||
|
||||
def _fake_app(self):
|
||||
app = mock.Mock()
|
||||
app.state = mock.Mock(spec=[]) # no smart_cache / smart_last_collected yet
|
||||
return app
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collects_on_first_call(self):
|
||||
from controller.lib.status_worker import refresh_smart_stats
|
||||
|
||||
sentinel = {"drives": [], "high_temperature": 55, "critical_temperature": 65}
|
||||
with mock.patch("controller.lib.status_worker.build_smart_stats",
|
||||
return_value=sentinel) as build:
|
||||
app = self._fake_app()
|
||||
now = datetime(2026, 6, 3, 12, 0, 0)
|
||||
result = await refresh_smart_stats(app, now)
|
||||
|
||||
build.assert_called_once()
|
||||
assert result is sentinel
|
||||
assert app.state.smart_cache is sentinel
|
||||
assert app.state.smart_last_collected == now
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reuses_cache_before_interval(self):
|
||||
"""A second call a few seconds later must NOT re-read SMART."""
|
||||
from controller.lib.status_worker import refresh_smart_stats
|
||||
|
||||
with mock.patch("controller.lib.status_worker.build_smart_stats",
|
||||
side_effect=[{"n": 1}, {"n": 2}]) as build:
|
||||
app = self._fake_app()
|
||||
t0 = datetime(2026, 6, 3, 12, 0, 0)
|
||||
first = await refresh_smart_stats(app, t0)
|
||||
# 5s later — well within the 60s window.
|
||||
second = await refresh_smart_stats(app, datetime(2026, 6, 3, 12, 0, 5))
|
||||
|
||||
assert build.call_count == 1
|
||||
assert first == {"n": 1}
|
||||
assert second == {"n": 1}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refreshes_after_interval(self):
|
||||
"""After >= 60s, SMART is re-read and the prior result is passed in."""
|
||||
from controller.lib.status_worker import refresh_smart_stats
|
||||
|
||||
with mock.patch("controller.lib.status_worker.build_smart_stats",
|
||||
side_effect=[{"n": 1}, {"n": 2}]) as build:
|
||||
app = self._fake_app()
|
||||
t0 = datetime(2026, 6, 3, 12, 0, 0)
|
||||
await refresh_smart_stats(app, t0)
|
||||
result = await refresh_smart_stats(app, datetime(2026, 6, 3, 12, 1, 0))
|
||||
|
||||
assert build.call_count == 2
|
||||
# The prior cache is handed to build_smart_stats for spin-down reuse.
|
||||
assert build.call_args_list[1].args[0] == {"n": 1}
|
||||
assert result == {"n": 2}
|
||||
|
||||
|
||||
class TestGetAdaptiveSleep:
|
||||
"""Tests for adaptive sleep calculation."""
|
||||
|
||||
|
|
@ -121,11 +198,14 @@ class TestAggregatedStatsEndpoint:
|
|||
|
||||
for key in (
|
||||
"cpu_stats", "memory_stats", "load_stats", "drives_stats",
|
||||
"nic_bandwidth_stats", "power_stats", "last_status",
|
||||
"nic_bandwidth_stats", "power_stats", "smart_stats", "last_status",
|
||||
):
|
||||
assert key in data
|
||||
# last_status is an ISO timestamp.
|
||||
datetime.fromisoformat(data["last_status"])
|
||||
# SMART carries its thresholds for the frontend warning.
|
||||
for field in ("drives", "high_temperature", "critical_temperature"):
|
||||
assert field in data["smart_stats"]
|
||||
# Sub-field shape spot-checks.
|
||||
for field in ("percent", "cores"):
|
||||
assert field in data["cpu_stats"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue