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:
@@ -259,6 +259,43 @@ def window_averages(conn: sqlite3.Connection, seconds: int) -> dict[str, dict]:
|
||||
return {r["inverter_id"]: dict(r) for r in rows}
|
||||
|
||||
|
||||
def recent_readings(conn: sqlite3.Connection, n: int, seconds: int) -> dict[str, list[dict]]:
|
||||
"""{inverter_id: [{ts, w}, ...]} — up to the last n readings of the past
|
||||
`seconds`, newest first."""
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT inverter_id, ts, w FROM (
|
||||
SELECT inverter_id, ts, ac_power_total_w AS w,
|
||||
row_number() OVER (PARTITION BY inverter_id ORDER BY ts DESC) AS rn
|
||||
FROM readings WHERE ts >= ?
|
||||
) WHERE rn <= ? ORDER BY inverter_id, ts DESC
|
||||
""",
|
||||
(now_ts() - seconds, n),
|
||||
).fetchall()
|
||||
out: dict[str, list[dict]] = {}
|
||||
for r in rows:
|
||||
out.setdefault(r["inverter_id"], []).append({"ts": r["ts"], "w": r["w"]})
|
||||
return out
|
||||
|
||||
|
||||
def last_production_run(conn: sqlite3.Connection, gap_seconds: int = 5400) -> tuple[int, int] | None:
|
||||
"""(start, end) of the most recent stretch in which some inverter produced
|
||||
power, split at silences longer than `gap_seconds` (the night). A unit
|
||||
that keeps reporting 0 W overnight (cloud-polled) doesn't join the runs."""
|
||||
rows = conn.execute(
|
||||
"SELECT DISTINCT ts FROM readings WHERE ac_power_total_w > 0 AND ts >= ? ORDER BY ts DESC",
|
||||
(now_ts() - 4 * 86400,),
|
||||
).fetchall()
|
||||
if not rows:
|
||||
return None
|
||||
end = start = rows[0]["ts"]
|
||||
for r in rows[1:]:
|
||||
if start - r["ts"] > gap_seconds:
|
||||
break
|
||||
start = r["ts"]
|
||||
return start, end
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- history
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user