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

@@ -3,8 +3,10 @@
from collections.abc import Mapping
from datetime import datetime
from types import MappingProxyType
from typing import Protocol
from production_analytics.calculations.shift_report import SHIFT_TIMEZONE, ShiftReport
from production_analytics.calculations.shift_material import ShiftMaterialSummary
from production_analytics.calculations.shift_report import ShiftReport
from production_analytics.calculations.shift_speed import validate_speed_window
from production_analytics.service.shift_downtime import ShiftDowntimeService
from production_analytics.service.shift_material import (
@@ -16,12 +18,21 @@ from production_analytics.service.shift_report_config import ShiftReportMachineC
from production_analytics.service.shift_speed import ShiftSpeedService
class ShiftMaterialAllocator(Protocol):
def allocate(self, measured: ShiftMaterialSummary) -> ShiftMaterialSummary: ...
class ShiftReportService:
def __init__(
self, *, machines: Mapping[str, ShiftReportMachineConfig],
production: ShiftProductionService, downtime: ShiftDowntimeService,
materials: ShiftMaterialService, channel_materials: ShiftChannelMaterialService,
self,
*,
machines: Mapping[str, ShiftReportMachineConfig],
production: ShiftProductionService,
downtime: ShiftDowntimeService,
materials: ShiftMaterialService,
channel_materials: ShiftChannelMaterialService,
speed: ShiftSpeedService,
material_allocators: Mapping[str, ShiftMaterialAllocator] | None = None,
) -> None:
self.machines = MappingProxyType(dict(machines))
self.production = production
@@ -29,26 +40,47 @@ class ShiftReportService:
self.materials = materials
self.channel_materials = channel_materials
self.speed = speed
self.material_allocators = MappingProxyType(dict(material_allocators or {}))
def build(self, *, machine_name: str, start: datetime, end: datetime) -> ShiftReport:
validate_speed_window(start, end)
if not isinstance(machine_name, str) or machine_name not in self.machines:
raise ValueError("Unknown shift-report machine")
config = self.machines[machine_name]
start, end = start.astimezone(SHIFT_TIMEZONE), end.astimezone(SHIFT_TIMEZONE)
start, end = start.astimezone(config.timezone), end.astimezone(config.timezone)
production = self.production.summarize(
workplace=config.erp_workplace, start=start, end=end,
)
downtime = self.downtime.summarize(machine_id=config.machine_id, start=start, end=end)
material_service = (
self.channel_materials if config.material.mode == "channel" else self.materials
)
materials = material_service.summarize(
machine_id=config.machine_id, calculation_ids=config.material.calculation_ids,
start=start, end=end,
)
speed = self.speed.summarize(config=config.speed, start=start, end=end)
workplace=config.erp_workplace,
start=start,
end=end,
timezone=config.timezone,
) if config.capabilities.production else None
downtime = (self.downtime.summarize(machine_id=config.machine_id, start=start, end=end)
if config.capabilities.downtime else None)
materials = None
if config.material is not None:
material_service = (
self.channel_materials if config.material.mode == "channel" else self.materials
)
materials = material_service.summarize(
machine_id=config.machine_id,
calculation_ids=config.material.calculation_ids,
start=start,
end=end,
)
if machine_name in self.material_allocators:
materials = self.material_allocators[machine_name].allocate(materials)
speed = (self.speed.summarize(config=config.speed, start=start, end=end)
if config.speed is not None else None)
return ShiftReport(
machine_name=machine_name, machine_id=config.machine_id, start=start, end=end,
production=production, downtime=downtime, materials=materials, speed=speed,
machine_name=machine_name,
machine_id=config.machine_id,
display_name=config.display_name,
site=config.site,
timezone=config.timezone,
start=start,
end=end,
production=production,
downtime=downtime,
materials=materials,
speed=speed,
)