Add unified shift reporting
This commit is contained in:
1 parent
1b060377aa
commit
41a5ae7a63
30 files changed
+3751
-9
No files matched your search
@@ -0,0 +1,76 @@
|
||||
"""Immutable shift-report composition models, independent of output format."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, datetime, time, timedelta
|
||||
from typing import Literal
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from production_analytics.calculations.shift_downtime import (
|
||||
ShiftDowntimeReason,
|
||||
ShiftDowntimeSummary,
|
||||
)
|
||||
from production_analytics.calculations.shift_material import ShiftMaterialSummary
|
||||
from production_analytics.calculations.shift_production import ShiftProductionSummary
|
||||
from production_analytics.calculations.shift_speed import ShiftSpeedSummary
|
||||
|
||||
SHIFT_TIMEZONE = ZoneInfo("Europe/Berlin")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ShiftReportDowntimeReason:
|
||||
reason: ShiftDowntimeReason
|
||||
display_name: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ShiftReport:
|
||||
machine_name: str
|
||||
machine_id: str
|
||||
start: datetime
|
||||
end: datetime
|
||||
production: ShiftProductionSummary
|
||||
downtime: ShiftDowntimeSummary
|
||||
materials: ShiftMaterialSummary
|
||||
speed: ShiftSpeedSummary
|
||||
|
||||
@property
|
||||
def downtime_event_count(self) -> int:
|
||||
return len(self.downtime.events)
|
||||
|
||||
@property
|
||||
def downtime_reasons(self) -> tuple[ShiftReportDowntimeReason, ...]:
|
||||
return tuple(
|
||||
ShiftReportDowntimeReason(
|
||||
reason=reason,
|
||||
display_name=reason.reason_name
|
||||
if reason.reason_name and reason.reason_name.strip()
|
||||
else "Ohne Stillstandsgrund",
|
||||
)
|
||||
for reason in self.downtime.reasons
|
||||
)
|
||||
|
||||
@property
|
||||
def top_downtime_reasons(self) -> tuple[ShiftReportDowntimeReason, ...]:
|
||||
# Preserve the source summary's validated duration ranking and tie order.
|
||||
return self.downtime_reasons[:3]
|
||||
|
||||
|
||||
def shift_report_window(
|
||||
day: date, shift: Literal["early", "late", "night"],
|
||||
) -> tuple[datetime, datetime]:
|
||||
"""Berlin wall-clock shifts; day denotes the shift's start date.
|
||||
|
||||
A night shift ends on the following calendar day, so elapsed duration may
|
||||
be seven or nine hours across DST. No fixed-duration arithmetic is used.
|
||||
"""
|
||||
if not isinstance(day, date) or isinstance(day, datetime):
|
||||
raise ValueError("day must be a calendar date")
|
||||
hours = {"early": (6, 14), "late": (14, 22), "night": (22, 6)}
|
||||
if shift not in hours:
|
||||
raise ValueError("shift must be early, late, or night")
|
||||
start_hour, end_hour = hours[shift]
|
||||
end_day = day + timedelta(days=1) if shift == "night" else day
|
||||
return (
|
||||
datetime.combine(day, time(start_hour), tzinfo=SHIFT_TIMEZONE),
|
||||
datetime.combine(end_day, time(end_hour), tzinfo=SHIFT_TIMEZONE),
|
||||
)
|
||||
Reference in new issue
Block a user