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:
+32
-1
@@ -14,6 +14,15 @@ from .config import get_settings
|
||||
from .db import get_meta
|
||||
|
||||
|
||||
# Recent readings per inverter: their spacing tells each unit's reporting
|
||||
# cadence; the newest few let clients average out a passing cloud.
|
||||
RECENT_SAMPLES = 8
|
||||
RECENT_LOOKBACK_SECONDS = 2 * 3600
|
||||
RECENT_POWER_COUNT = 5
|
||||
RECENT_POWER_SECONDS = 1800
|
||||
MAX_ONLINE_WINDOW_SECONDS = 1800
|
||||
|
||||
|
||||
def _minutes(seconds: int) -> str:
|
||||
if seconds < 3600:
|
||||
return f"{seconds // 60} min"
|
||||
@@ -39,10 +48,21 @@ def summary(conn: sqlite3.Connection) -> dict:
|
||||
latest = store.latest_all(conn)
|
||||
averages = store.window_averages(conn, s.underperform_window_seconds)
|
||||
energy_today = store.today_energy(conn)
|
||||
recent = store.recent_readings(conn, RECENT_SAMPLES, RECENT_LOOKBACK_SECONDS)
|
||||
|
||||
def window(inv_id: str) -> int:
|
||||
"""Silence allowed before offline: a couple of missed cycles at the
|
||||
unit's own cadence (60 s local, ~5 min cloud), never below the
|
||||
configured window."""
|
||||
ts = [x["ts"] for x in recent.get(inv_id, [])]
|
||||
gaps = [a - b for a, b in zip(ts, ts[1:]) if a > b]
|
||||
if not gaps:
|
||||
return s.online_window_seconds
|
||||
return int(min(MAX_ONLINE_WINDOW_SECONDS, max(s.online_window_seconds, 2.5 * statistics.median(gaps))))
|
||||
|
||||
def is_online(inv_id: str) -> bool:
|
||||
r = latest.get(inv_id)
|
||||
return r is not None and r["age_seconds"] <= s.online_window_seconds
|
||||
return r is not None and r["age_seconds"] <= window(inv_id)
|
||||
|
||||
# Fleet comparison: per kWp when every producing inverter has a capacity
|
||||
# set, raw watts otherwise (assumes similar arrays).
|
||||
@@ -74,6 +94,7 @@ def summary(conn: sqlite3.Connection) -> dict:
|
||||
|
||||
if not online:
|
||||
since = f"for {_minutes(r['age_seconds'])}" if r else "yet"
|
||||
silent_s = r["age_seconds"] if r else None
|
||||
if not datalogger_online:
|
||||
state = "unknown"
|
||||
issues.append({
|
||||
@@ -89,6 +110,7 @@ def summary(conn: sqlite3.Connection) -> dict:
|
||||
"code": "not_responding",
|
||||
"severity": "critical",
|
||||
"message": f"Not responding {since} while the datalogger is online",
|
||||
"params": {"seconds": silent_s},
|
||||
})
|
||||
else:
|
||||
if r.get("error_code"):
|
||||
@@ -98,12 +120,14 @@ def summary(conn: sqlite3.Connection) -> dict:
|
||||
"code": "error_code",
|
||||
"severity": "critical",
|
||||
"message": f"Inverter reports error code {r['error_code']}{text}",
|
||||
"params": {"error_code": r["error_code"], "status_text": r.get("status_text")},
|
||||
})
|
||||
if compare and relative is not None and relative < s.underperform_ratio:
|
||||
issues.append({
|
||||
"code": "underperforming",
|
||||
"severity": "warning",
|
||||
"message": f"Producing {relative:.0%} of the fleet median — check strings, fuses and breakers",
|
||||
"params": {"relative": relative},
|
||||
})
|
||||
avg = averages.get(inv["id"], {})
|
||||
m1, m2 = avg.get("mppt1_w"), avg.get("mppt2_w")
|
||||
@@ -115,6 +139,7 @@ def summary(conn: sqlite3.Connection) -> dict:
|
||||
"code": "mppt_imbalance",
|
||||
"severity": "warning",
|
||||
"message": f"{weak} at {lo:.0f} W vs {hi:.0f} W on the other input — check that string",
|
||||
"params": {"input": weak, "weak_w": round(lo), "strong_w": round(hi)},
|
||||
})
|
||||
if state == "ok" and issues:
|
||||
state = "warning"
|
||||
@@ -131,9 +156,15 @@ def summary(conn: sqlite3.Connection) -> dict:
|
||||
"issues": issues,
|
||||
"avg_power_w": round(averages[inv["id"]]["ac_power_w"]) if online and averages.get(inv["id"], {}).get("ac_power_w") is not None else None,
|
||||
"relative_output": relative,
|
||||
"online_window_seconds": window(inv["id"]),
|
||||
},
|
||||
"energy_today": energy_today.get(inv["id"], {"wh": None, "estimated": False}),
|
||||
"latest": r,
|
||||
"recent_power": [
|
||||
{"timestamp": store.to_dt(x["ts"]), "ac_power_total_w": x["w"]}
|
||||
for x in recent.get(inv["id"], [])
|
||||
if x["ts"] >= now - RECENT_POWER_SECONDS
|
||||
][:RECENT_POWER_COUNT],
|
||||
})
|
||||
|
||||
today_values = [e["wh"] for e in energy_today.values() if e["wh"] is not None]
|
||||
|
||||
Reference in New Issue
Block a user