Extend shift reporting and downtime analytics
This commit is contained in:
1 parent
41a5ae7a63
commit
6e8eb6f50f
74 files changed
+5397
-238
No files matched your search
@@ -13,8 +13,6 @@ from production_analytics.calculations.shift_material import ShiftMaterialSummar
|
||||
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:
|
||||
@@ -25,17 +23,20 @@ class ShiftReportDowntimeReason:
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ShiftReport:
|
||||
machine_name: str
|
||||
machine_id: str
|
||||
machine_id: str | None
|
||||
start: datetime
|
||||
end: datetime
|
||||
production: ShiftProductionSummary
|
||||
downtime: ShiftDowntimeSummary
|
||||
materials: ShiftMaterialSummary
|
||||
speed: ShiftSpeedSummary
|
||||
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)
|
||||
return len(self.downtime.events) if self.downtime is not None else 0
|
||||
|
||||
@property
|
||||
def downtime_reasons(self) -> tuple[ShiftReportDowntimeReason, ...]:
|
||||
@@ -46,7 +47,7 @@ class ShiftReport:
|
||||
if reason.reason_name and reason.reason_name.strip()
|
||||
else "Ohne Stillstandsgrund",
|
||||
)
|
||||
for reason in self.downtime.reasons
|
||||
for reason in (self.downtime.reasons if self.downtime is not None else ())
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -56,21 +57,42 @@ class ShiftReport:
|
||||
|
||||
|
||||
def shift_report_window(
|
||||
day: date, shift: Literal["early", "late", "night"],
|
||||
day: date, shift: Literal["early", "late", "night"], timezone: ZoneInfo,
|
||||
) -> tuple[datetime, datetime]:
|
||||
"""Berlin wall-clock shifts; day denotes the shift's start date.
|
||||
"""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=SHIFT_TIMEZONE),
|
||||
datetime.combine(end_day, time(end_hour), tzinfo=SHIFT_TIMEZONE),
|
||||
datetime.combine(day, time(start_hour), tzinfo=timezone),
|
||||
datetime.combine(end_day, time(end_hour), tzinfo=timezone),
|
||||
)
|
||||
|
||||
|
||||
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"
|
||||
Reference in new issue
Block a user