42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
"""Compose bounded ENLYZE speed reads with the pure running-speed KPI."""
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Protocol
|
|
|
|
from production_analytics.calculations.shift_speed import (
|
|
ShiftSpeedConfig,
|
|
ShiftSpeedSample,
|
|
ShiftSpeedSummary,
|
|
aggregate_shift_speed,
|
|
validate_speed_window,
|
|
)
|
|
|
|
|
|
class ShiftSpeedReader(Protocol):
|
|
def get_speed_samples(
|
|
self, *, machine_id: str, speed_signal_ref: str, start: datetime, end: datetime,
|
|
running_signal_ref: str | None = None,
|
|
numeric_gate_signal_ref: str | None = None,
|
|
) -> list[ShiftSpeedSample]: ...
|
|
|
|
|
|
class ShiftSpeedService:
|
|
def __init__(self, gateway: ShiftSpeedReader) -> None:
|
|
self.gateway = gateway
|
|
|
|
def summarize(
|
|
self, *, config: ShiftSpeedConfig, start: datetime, end: datetime,
|
|
) -> ShiftSpeedSummary:
|
|
validate_speed_window(start, end)
|
|
kwargs = {"machine_id": config.machine_id, "speed_signal_ref": config.speed_signal_ref,
|
|
"start": start, "end": end}
|
|
if config.running_signal_ref is not None:
|
|
kwargs["running_signal_ref"] = config.running_signal_ref
|
|
if config.numeric_dwell_gate is not None:
|
|
kwargs["numeric_gate_signal_ref"] = config.numeric_dwell_gate.signal_ref
|
|
dwell = timedelta(seconds=config.numeric_dwell_gate.minimum_duration_seconds)
|
|
kwargs["start"] = start.astimezone(UTC) - dwell
|
|
kwargs["end"] = end.astimezone(UTC) + dwell
|
|
samples = self.gateway.get_speed_samples(**kwargs)
|
|
return aggregate_shift_speed(samples, config=config, start=start, end=end)
|