121 lines
4.4 KiB
Python
121 lines
4.4 KiB
Python
"""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
|
|
|
|
|
|
@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 | None
|
|
start: datetime
|
|
end: datetime
|
|
production: ShiftProductionSummary | None
|
|
downtime: ShiftDowntimeSummary | None
|
|
materials: ShiftMaterialSummary | None
|
|
speed: ShiftSpeedSummary | None
|
|
timezone: ZoneInfo
|
|
display_name: str | None = None
|
|
site: str | None = None
|
|
|
|
@property
|
|
def downtime_event_count(self) -> int:
|
|
return len(self.downtime.events) if self.downtime is not None else 0
|
|
|
|
@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 if self.downtime is not None else ())
|
|
)
|
|
|
|
@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"], timezone: ZoneInfo,
|
|
) -> tuple[datetime, datetime]:
|
|
"""Site-local 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")
|
|
if not isinstance(timezone, ZoneInfo):
|
|
raise ValueError("timezone must be a ZoneInfo")
|
|
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=timezone),
|
|
datetime.combine(end_day, time(end_hour), tzinfo=timezone),
|
|
)
|
|
|
|
|
|
def shift_containing(
|
|
timestamp: datetime, timezone: ZoneInfo,
|
|
) -> tuple[date, Literal["early", "late", "night"], datetime, datetime]:
|
|
"""Return the site-local shift containing one confirmation instant."""
|
|
if (not isinstance(timestamp, datetime) or timestamp.tzinfo is None
|
|
or timestamp.utcoffset() is None):
|
|
raise ValueError("timestamp must be timezone-aware")
|
|
if not isinstance(timezone, ZoneInfo):
|
|
raise ValueError("timezone must be a ZoneInfo")
|
|
local = timestamp.astimezone(timezone)
|
|
if local.hour < 6:
|
|
day, shift = local.date() - timedelta(days=1), "night"
|
|
elif local.hour < 14:
|
|
day, shift = local.date(), "early"
|
|
elif local.hour < 22:
|
|
day, shift = local.date(), "late"
|
|
else:
|
|
day, shift = local.date(), "night"
|
|
start, end = shift_report_window(day, shift, timezone)
|
|
return day, shift, start, end
|
|
|
|
|
|
def most_recent_completed_shift(
|
|
now: datetime, timezone: ZoneInfo,
|
|
) -> tuple[date, Literal["early", "late", "night"]]:
|
|
"""Return start date and name of the last completed site-local shift."""
|
|
if now.tzinfo is None or now.utcoffset() is None:
|
|
raise ValueError("now must include a timezone")
|
|
if not isinstance(timezone, ZoneInfo):
|
|
raise ValueError("timezone must be a ZoneInfo")
|
|
local = now.astimezone(timezone)
|
|
day = local.date()
|
|
if local.hour < 6:
|
|
return day - timedelta(days=1), "late"
|
|
if local.hour < 14:
|
|
return day - timedelta(days=1), "night"
|
|
if local.hour < 22:
|
|
return day, "early"
|
|
return day, "late"
|