fix(unifi): the Overview's history buckets on Postgres; devices name their state (#5207)
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m24s
CI / unit (push) Successful in 1m35s
CI / publish (push) Successful in 1m29s

#5207 "UniFi Overview tab: func.strftime is SQLite-only": the WAN and
client sparklines bucketed with strftime('%s'), which Postgres lacks, so
/plugins/unifi/rows failed. Traefik's date_bin bucket moves to
core/time_range.time_bucket and both plugins use it. Devices list offline
and failing first, and a state other than online is named (Upgrading,
Adopting, Heartbeat missed...) instead of drawn as Down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 17:28:19 -04:00
co-authored by Claude Opus 5.5
parent 46a301652b
commit ea31deabf9
6 changed files with 145 additions and 49 deletions
+52
View File
@@ -0,0 +1,52 @@
"""Integration: the UniFi overview's sparkline history buckets on Postgres
(#5207: it used SQLite's strftime, so the overview rows failed). Requires
STEWARD_DATABASE_URL."""
from __future__ import annotations
import asyncio
import os
from datetime import datetime, timedelta, timezone
import pytest
pytestmark = pytest.mark.integration
@pytest.fixture
def app():
if not os.environ.get("STEWARD_DATABASE_URL"):
pytest.skip("needs Postgres")
from steward.app import create_app
return create_app(testing=False)
def test_wan_and_client_history_average_per_window(app):
from sqlalchemy import delete
from plugins.unifi.models import UnifiClientSnapshot, UnifiWanStat
from plugins.unifi.routes import history
# Two scrapes in one 10-minute window, one in the next.
start = datetime(2031, 1, 1, 12, 0, tzinfo=timezone.utc)
at = [start + timedelta(minutes=1), start + timedelta(minutes=4), start + timedelta(minutes=12)]
async def go():
async with app.db_sessionmaker() as s:
async with s.begin():
await s.execute(delete(UnifiWanStat).where(UnifiWanStat.scraped_at >= start))
await s.execute(delete(UnifiClientSnapshot).where(UnifiClientSnapshot.scraped_at >= start))
for when, ms, n in zip(at, (10.0, 30.0, 50.0), (4, 6, 9)):
s.add(UnifiWanStat(scraped_at=when, is_up=True, latency_ms=ms,
rx_bytes_rate=1.0, tx_bytes_rate=2.0))
s.add(UnifiClientSnapshot(scraped_at=when, total_clients=n))
try:
async with s.begin():
return await history(s, start, 600)
finally:
async with s.begin():
await s.execute(delete(UnifiWanStat).where(UnifiWanStat.scraped_at >= start))
await s.execute(delete(UnifiClientSnapshot).where(UnifiClientSnapshot.scraped_at >= start))
wan, clients = asyncio.run(go())
assert [(r.latency_ms, r.scraped_at, r.bucket) for r in wan] == [
(20.0, at[0], start), (50.0, at[2], start + timedelta(minutes=10))]
assert [float(r.total_clients) for r in clients] == [5.0, 9.0]
+13
View File
@@ -3,6 +3,7 @@ by Settings → Thresholds, devices with status dots, categories as badges (#512
from datetime import datetime, timezone
from types import SimpleNamespace as NS
from plugins.unifi.routes import device_state, devices_in_order
from tests._templates import render_plugin_template
from tests.test_style_tokens import hex_colours
@@ -13,14 +14,19 @@ async def test_wan_health_and_devices_use_the_shared_shapes(app):
devices = [NS(name="switch", mac="aa", model="USW", device_type="usw", ip="10.0.0.2", state=1,
uptime_seconds=3600),
NS(name="ap-attic", mac="bb", model="U6", device_type="uap", ip="10.0.0.3", state=0,
uptime_seconds=None),
NS(name="ap-porch", mac="cc", model="U6", device_type="uap", ip="10.0.0.4", state=4,
uptime_seconds=None)]
async with app.test_request_context("/plugins/unifi/rows"):
page = await render_plugin_template(app, "unifi", "unifi/rows.html", latest_wan=wan, devices=devices,
device_state=device_state,
sparkline_latency="", sparkline_rx="", sparkline_tx="")
assert '<span class="stat-tile-label">WAN</span>' in page and "stat-crit" in page
assert '<span class="stat-tile-value">250 ms</span>' in page
assert 'class="dot dot-ok"' in page and 'class="dot dot-crit"' in page
# #5207: a device mid-upgrade is named, not drawn as down.
assert 'class="dot dot-warn" aria-hidden="true" title="Upgrading"' in page and "Offline" in page
assert "font-weight:600" not in page and "font-weight:700" not in page
assert not hex_colours(page), hex_colours(page)
@@ -32,3 +38,10 @@ async def test_event_categories_are_badges(app):
page = await render_plugin_template(app, "unifi", "unifi/events.html", events=events)
assert '<span class="badge badge-crit">ids</span>' in page
assert "rgba(" not in page
def test_offline_devices_come_first_then_transitions_then_working():
devices = [NS(name=n, mac=n, state=st) for n, st in
[("b-ok", 1), ("a-ok", 1), ("upgrading", 4), ("gone", 0), ("odd", 99)]]
assert [d.name for d in devices_in_order(devices)] == ["gone", "odd", "upgrading", "a-ok", "b-ok"]
assert device_state(99) == ("unknown", "State 99")