Add unified shift reporting

This commit is contained in:
admin committed 2026-10-07 21:58:18 +02:00
1 parent 1b060377aa
commit 41a5ae7a63
30 files changed
+3751 -9

No files matched your search

@@ -12,6 +12,7 @@ from production_analytics.calculations.material_consumption import (
area_application_rate_kg_per_hour,
rotational_discharge_rate_kg_per_hour,
)
from production_analytics.calculations.shift_speed import ShiftSpeedSample, validate_speed_window
from production_analytics.enlyze.exploration import ExplorationClient
if TYPE_CHECKING:
@@ -153,6 +154,63 @@ class EnlyzeApiGateway:
except (KeyError, TypeError, ValueError) as exc:
raise ValueError(f"Invalid downtime response on page {page}: {exc}") from exc
def get_speed_samples(
self, *, machine_id: str, speed_signal_ref: str, start: datetime, end: datetime,
) -> list[ShiftSpeedSample]:
"""Retrieve one bounded signal using the existing timeseries cursor contract.
Invalid/missing sample values remain unavailable for domain aggregation.
Malformed timestamps/rows become invalid samples, while structural response
and pagination errors still fail the read instead of returning partial KPIs.
"""
validate_speed_window(start, end)
for name, value in (("machine_id", machine_id), ("speed_signal_ref", speed_signal_ref)):
if not isinstance(value, str) or not value.strip() or "\x00" in value:
raise ValueError(f"{name} must be a non-empty string without NUL bytes")
body = {
"machine": machine_id,
"start": start.astimezone(UTC).isoformat(),
"end": end.astimezone(UTC).isoformat(),
"variables": [{"uuid": speed_signal_ref}],
}
samples = []
cursors: set[str] = set()
page = 1
while True:
response = self._client.post_json("/v2/timeseries", body)
try:
data = response.body["data"]
columns = data["columns"]
if not isinstance(columns, list) or any(
columns.count(column) != 1 for column in ("time", speed_signal_ref)
):
raise ValueError("required columns must occur exactly once")
records = data["records"]
if not isinstance(records, list):
raise ValueError("records must be a list")
time_index, speed_index = columns.index("time"), columns.index(speed_signal_ref)
for record in records:
if not isinstance(record, list) or len(record) != len(columns):
samples.append(ShiftSpeedSample(None, None))
continue
try:
timestamp = _parse_timestamp(record[time_index])
except (TypeError, ValueError):
timestamp = None
samples.append(ShiftSpeedSample(timestamp, record[speed_index]))
cursor = _next_cursor(response.body)
if cursor is None:
return samples
if cursor in cursors:
raise ValueError("next_cursor has already been followed")
cursors.add(cursor)
body = {**body, "cursor": cursor}
page += 1
except (KeyError, TypeError, ValueError) as exc:
raise ValueError(
f"Invalid speed timeseries response on page {page}: {exc}"
) from exc
def get_material_samples(
self,
*,