Extend shift reporting and downtime analytics

This commit is contained in:
admin committed 2026-10-20 14:16:58 +02:00
1 parent 41a5ae7a63
commit 6e8eb6f50f
74 files changed
+5397 -238

No files matched your search

@@ -1,4 +1,4 @@
"""Machine-independent shift downtime from raw source intervals."""
"""Machine-independent shift downtime from effective attribution intervals."""
from collections import defaultdict
from collections.abc import Iterable
@@ -18,6 +18,8 @@ class ShiftDowntimeEvent:
reason_group: str | None
category: str | None
comment: str | None
attributed_start: datetime | None = None
attributed_end: datetime | None = None
def __post_init__(self) -> None:
for name in ("external_id", "machine_id"):
@@ -25,6 +27,10 @@ class ShiftDowntimeEvent:
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")
_aware(self.source_start, "source_start")
for name in ("attributed_start", "attributed_end"):
value = getattr(self, name)
if value is not None:
_aware(value, name)
if self.source_end is not None:
_aware(self.source_end, "source_end")
if self.source_end.astimezone(UTC) < self.source_start.astimezone(UTC):
@@ -85,12 +91,12 @@ def validate_downtime_window(*, machine_id: str, start: datetime, end: datetime)
def aggregate_shift_downtime(
events: Iterable[ShiftDowntimeEvent], *, machine_id: str, start: datetime, end: datetime
) -> ShiftDowntimeSummary:
"""Clip source intervals to [start, end); open events end at the shift end.
"""Clip effective intervals to [start, end); open events end at the shift end.
Total downtime is the union of intervals. Each reason also uses a union within
its full metadata group. Different reasons may overlap, so their durations need
not sum to total downtime. Non-downtime is a time complement, not measured output.
No production-order attribution fields participate in this calculation.
Attribution timestamps take precedence, falling back to raw source timestamps.
"""
validate_downtime_window(machine_id=machine_id, start=start, end=end)
lower, upper = start.astimezone(UTC), end.astimezone(UTC)
@@ -98,12 +104,16 @@ def aggregate_shift_downtime(
for event in events:
if event.machine_id != machine_id:
continue
event_start = event.source_start.astimezone(UTC)
event_end = upper if event.source_end is None else event.source_end.astimezone(UTC)
effective_start = event.attributed_start or event.source_start
effective_end = (
event.attributed_end if event.attributed_end is not None else event.source_end
)
event_start = effective_start.astimezone(UTC)
event_end = upper if effective_end is None else effective_end.astimezone(UTC)
if event_start >= upper or event_end <= lower:
continue
clipped_start = start if event_start < lower else event.source_start
clipped_end = end if event.source_end is None or event_end > upper else event.source_end
clipped_start = start if event_start < lower else effective_start
clipped_end = end if effective_end is None or event_end > upper else effective_end
duration = (clipped_end.astimezone(UTC) - clipped_start.astimezone(UTC)).total_seconds()
if duration > 0:
clipped.append(ClippedShiftDowntimeEvent(event, clipped_start, clipped_end, duration))