feat(api): shared view settings, recent readings, last production run

Groundwork for the dashboard settings page and the five-inverter contract:

- GET/PUT /api/settings stores display options (comparison averaging,
  scale and reference, power chart range and smoothing, voltage display,
  default language) in the meta table. Shared by every viewer since there
  are no accounts; PUT needs the API key so a public visitor can't change
  everyone's view. Unknown keys are ignored, missing ones take defaults.
- /api/summary returns each inverter's last few readings (recent_power) so
  the comparison can average out one reading taken under a passing cloud.
- Offline is now judged per inverter: a couple of missed cycles at the
  unit's own cadence (INV5 is cloud-polled every ~5 min), never below
  ONLINE_WINDOW_SECONDS. Before, INV5 flapped offline between reports.
- /api/power?last_run=true covers the latest production run, split at
  night silences; units reporting 0 W overnight don't join runs.
- Issues carry params so clients can render them in Greek.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 17:59:33 +03:00
co-authored by Claude Opus 5.5
parent c725b748c7
commit 8029468fb1
5 changed files with 230 additions and 5 deletions
+54
View File
@@ -189,3 +189,57 @@ def test_energy_survives_out_of_order_readings(client):
e = client.get("/api/summary").json()["inverters"][0]["energy_today"]
if e["wh"] is not None:
assert 2900 <= e["wh"] <= 3000
def test_view_settings_shared_defaults_and_auth(client):
s = client.get("/api/settings").json()
assert s["comparison_readings"] == 1 and s["power_range"] == "24h"
body = {**s, "comparison_readings": 3, "comparison_scale": "custom", "comparison_custom_kw": 25,
"power_range": "run", "power_smoothing": "light", "future_option": True}
assert client.put("/api/settings", json=body, headers={"X-API-Key": "nope"}).status_code == 401
saved = client.put("/api/settings", json=body).json()
assert saved["comparison_custom_kw"] == 25 and "future_option" not in saved
assert client.get("/api/settings").json()["power_range"] == "run"
assert client.put("/api/settings", json={"comparison_readings": 4}).status_code == 422
def test_recent_power_and_slow_unit_stays_online(client):
now = int(time.time())
batch = [onsite_payload("INV1", now - i * 60, 6000 + i) for i in range(7)]
# Cloud-polled unit: every 5 min, last one 8 min ago — beyond the 5 min
# default window but only ~1.5 of its own cycles
batch += [onsite_payload("INV5", now - 480 - i * 300, 5000) for i in range(6)]
client.post("/api/readings", json=batch)
by_id = {x["inverter"]["id"]: x for x in client.get("/api/summary").json()["inverters"]}
assert [p["ac_power_total_w"] for p in by_id["INV1"]["recent_power"]] == [6000, 6001, 6002, 6003, 6004]
assert by_id["INV1"]["health"]["online_window_seconds"] == 300
assert by_id["INV5"]["health"]["online"] is True
assert by_id["INV5"]["health"]["online_window_seconds"] == 750
assert len(by_id["INV5"]["recent_power"]) == 5
def test_issue_params_for_translation(client):
now = int(time.time())
client.post("/api/readings", json=[
onsite_payload("INV1", now, 7000), onsite_payload("INV2", now, 7000), onsite_payload("INV4", now, 2000),
])
inv4 = client.get("/api/summary").json()["inverters"][2]["health"]
assert inv4["issues"][0]["params"]["relative"] < 0.75
def test_power_last_production_run(client):
now = int(time.time())
# Yesterday's run, a night of silence, then this morning's run; INV5
# reports 0 W all night and must not glue the runs together
batch = [onsite_payload("INV1", now - 20 * 3600 + i * 600, 5000) for i in range(6)]
batch += [onsite_payload("INV5", now - 18 * 3600 + i * 1800, 0, status=1) for i in range(30)]
batch += [onsite_payload("INV1", now - 3 * 3600 + i * 600, 5000) for i in range(18)]
client.post("/api/readings", json=batch)
p = client.get("/api/power", params={"last_run": True}).json()
start = datetime.fromisoformat(p["from"]).timestamp()
assert abs(start - (now - 3 * 3600 - 900)) < 5
assert abs(datetime.fromisoformat(p["to"]).timestamp() - now) < 5