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
+53
View File
@@ -82,6 +82,10 @@ class Issue(BaseModel):
code: str
severity: Severity
message: str
params: dict[str, Any] = Field(
default_factory=dict,
description="Values inside the message, so clients can render it in their own language",
)
class Health(BaseModel):
@@ -95,6 +99,10 @@ class Health(BaseModel):
relative_output: float | None = Field(
description="Output vs fleet median (1.0 = median), per kWp when capacities are set"
)
online_window_seconds: int = Field(
description="Silence after which this inverter counts as offline: ONLINE_WINDOW_SECONDS, "
"stretched for units that report slowly (e.g. cloud-polled every 5 min)"
)
class EnergyToday(BaseModel):
@@ -102,11 +110,20 @@ class EnergyToday(BaseModel):
estimated: bool = Field(description="Integrated from AC power, not the inverter's own counter")
class RecentPower(BaseModel):
timestamp: datetime
ac_power_total_w: int | None
class InverterStatus(BaseModel):
inverter: Inverter
health: Health
energy_today: EnergyToday
latest: dict[str, Any] | None
recent_power: list[RecentPower] = Field(
description="Up to the last 5 readings of the past 30 min, newest first — lets clients "
"average out a single reading taken while a cloud shaded one array"
)
class Totals(BaseModel):
@@ -130,6 +147,42 @@ class Sun(BaseModel):
daylight: bool | None
PowerRange = Literal["30m", "1h", "3h", "6h", "24h", "run", "3d", "7d", "30d"]
class ViewSettings(BaseModel):
"""Dashboard display options. Shared by every viewer (there are no user
accounts); unknown keys are ignored and missing ones take the default,
so older and newer clients can coexist."""
model_config = ConfigDict(extra="ignore")
# Output comparison
comparison_readings: Literal[1, 3, 5] = Field(
default=1, description="Average the last N readings per inverter (1 = latest only)"
)
comparison_scale: Literal["auto", "max", "custom"] = Field(
default="auto",
description="Bar length: auto = rounded-up ceiling above the highest, "
"max = highest inverter is a full bar, custom = comparison_custom_kw is a full bar",
)
comparison_custom_kw: float | None = Field(default=None, gt=0, le=1000)
comparison_reference: Literal["median", "best", "custom"] = Field(
default="median", description="What the percentage is relative to"
)
# Fleet power chart
power_range: PowerRange = "24h"
power_smoothing: Literal["off", "light", "strong"] = "off"
# Display
voltage_display: Literal["phase", "line"] = Field(
default="phase",
description="phase = line-to-neutral as sent (~240 V); line = x√3, matches the inverter screen (~415 V)",
)
default_language: Literal["en", "el"] = "el"
class Summary(BaseModel):
generated_at: datetime
datalogger: Datalogger