Add channel material and downtime analytics
This commit is contained in:
1 parent
5452dc035b
commit
640ec53330
24 files changed
+2362
-76
No files matched your search
@@ -0,0 +1,127 @@
|
||||
"""Configuration for reusable channel-level extruder consumption."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from production_analytics.calculations.config import CalculationConfigError, finite_number
|
||||
from production_analytics.service.channel_material import ChannelDefinition
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChannelMaterialCalculationConfig:
|
||||
id: str
|
||||
machine_ref: str
|
||||
max_sample_gap_seconds: float
|
||||
percentage_tolerance: float
|
||||
channels: tuple[ChannelDefinition, ...]
|
||||
material_names: dict[str, str]
|
||||
|
||||
|
||||
def load_channel_material_calculation(path: str | Path) -> ChannelMaterialCalculationConfig:
|
||||
try:
|
||||
document = yaml.safe_load(Path(path).read_text(encoding="utf-8"))
|
||||
except (OSError, UnicodeError, yaml.YAMLError) as exc:
|
||||
raise CalculationConfigError("Cannot read channel material configuration") from exc
|
||||
if (
|
||||
not isinstance(document, dict)
|
||||
or set(document) != {"calculations"}
|
||||
or not isinstance(document["calculations"], list)
|
||||
):
|
||||
raise CalculationConfigError("Configuration must contain only calculations")
|
||||
if len(document["calculations"]) != 1:
|
||||
raise CalculationConfigError("Channel configuration requires one calculation")
|
||||
entry = document["calculations"][0]
|
||||
required = {
|
||||
"id",
|
||||
"type",
|
||||
"version",
|
||||
"machine_ref",
|
||||
"max_sample_gap_seconds",
|
||||
"percentage_tolerance",
|
||||
"extruders",
|
||||
}
|
||||
if (
|
||||
not isinstance(entry, dict)
|
||||
or set(entry) - (required | {"material_names"})
|
||||
or required - set(entry)
|
||||
):
|
||||
raise CalculationConfigError("Invalid channel calculation fields")
|
||||
if entry["type"] != "channel_material_consumption" or entry["version"] != "1":
|
||||
raise CalculationConfigError("Unsupported channel calculation type or version")
|
||||
if not all(isinstance(entry[x], str) and entry[x].strip() for x in ("id", "machine_ref")):
|
||||
raise CalculationConfigError("id and machine_ref must be non-empty strings")
|
||||
channels = []
|
||||
extruders = entry["extruders"]
|
||||
if not isinstance(extruders, list) or not extruders:
|
||||
raise CalculationConfigError("extruders must be a non-empty list")
|
||||
for extruder in extruders:
|
||||
if not isinstance(extruder, dict) or set(extruder) != {
|
||||
"id",
|
||||
"total_rate_signal_ref",
|
||||
"channels",
|
||||
}:
|
||||
raise CalculationConfigError("Invalid extruder fields")
|
||||
if not all(
|
||||
isinstance(extruder[x], str) and extruder[x].strip()
|
||||
for x in ("id", "total_rate_signal_ref")
|
||||
):
|
||||
raise CalculationConfigError("Extruder id and total signal must be strings")
|
||||
rows = extruder["channels"]
|
||||
if not isinstance(rows, list) or len(rows) != 7:
|
||||
raise CalculationConfigError("Each extruder requires exactly seven channels")
|
||||
for row in rows:
|
||||
allowed = {
|
||||
"id",
|
||||
"status_signal_ref",
|
||||
"percentage_signal_ref",
|
||||
"screw_speed_signal_ref",
|
||||
"material_number_signal_ref",
|
||||
}
|
||||
if (
|
||||
not isinstance(row, dict)
|
||||
or set(row) - allowed
|
||||
or {"id", "status_signal_ref", "percentage_signal_ref", "screw_speed_signal_ref"}
|
||||
- set(row)
|
||||
):
|
||||
raise CalculationConfigError("Invalid dosing channel fields")
|
||||
if not all(
|
||||
isinstance(row[x], str) and row[x].strip()
|
||||
for x in (
|
||||
"id",
|
||||
"status_signal_ref",
|
||||
"percentage_signal_ref",
|
||||
"screw_speed_signal_ref",
|
||||
)
|
||||
):
|
||||
raise CalculationConfigError("Dosing signal references must be non-empty strings")
|
||||
material = row.get("material_number_signal_ref")
|
||||
if material is not None and (not isinstance(material, str) or not material.strip()):
|
||||
raise CalculationConfigError("material signal must be a string or null")
|
||||
channels.append(
|
||||
ChannelDefinition(
|
||||
extruder["id"],
|
||||
row["id"],
|
||||
extruder["total_rate_signal_ref"],
|
||||
row["status_signal_ref"],
|
||||
row["percentage_signal_ref"],
|
||||
row["screw_speed_signal_ref"],
|
||||
material,
|
||||
)
|
||||
)
|
||||
if len({(c.extruder, c.channel) for c in channels}) != len(channels):
|
||||
raise CalculationConfigError("Extruder/channel identities must be unique")
|
||||
names = entry.get("material_names", {})
|
||||
if not isinstance(names, dict) or any(
|
||||
not isinstance(k, str) or not isinstance(v, str) for k, v in names.items()
|
||||
):
|
||||
raise CalculationConfigError("material_names must map strings to strings")
|
||||
return ChannelMaterialCalculationConfig(
|
||||
entry["id"],
|
||||
entry["machine_ref"],
|
||||
finite_number(entry["max_sample_gap_seconds"], "max_sample_gap_seconds", positive=True),
|
||||
finite_number(entry["percentage_tolerance"], "percentage_tolerance"),
|
||||
tuple(channels),
|
||||
names,
|
||||
)
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Pure channel-level dosing calculations, shared by extruder implementations."""
|
||||
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from math import isfinite
|
||||
|
||||
from production_analytics.calculations.material_consumption import MaterialSample
|
||||
|
||||
MATERIAL_ASSIGNED = "ASSIGNED"
|
||||
MATERIAL_UNAVAILABLE = "UNAVAILABLE"
|
||||
MATERIAL_AMBIGUOUS = "AMBIGUOUS"
|
||||
MATERIAL_UNMAPPED = "UNMAPPED"
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class DosingChannelSample:
|
||||
timestamp: datetime
|
||||
total_rate_kg_per_hour: float
|
||||
channel_status: bool
|
||||
dosing_percentage: float
|
||||
screw_speed: float
|
||||
material_number: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChannelRate:
|
||||
sample: MaterialSample
|
||||
material_number: str | None
|
||||
material_name: str | None
|
||||
material_mapping_status: str
|
||||
|
||||
|
||||
def validate_active_percentage_sum(
|
||||
samples: Iterable[DosingChannelSample], *, tolerance: float
|
||||
) -> bool:
|
||||
"""Return whether active channel percentages close to 100; never normalize them."""
|
||||
values = tuple(samples)
|
||||
if not isfinite(tolerance) or tolerance < 0:
|
||||
raise ValueError("percentage tolerance must be finite and non-negative")
|
||||
total = 0.0
|
||||
for value in values:
|
||||
if not isfinite(value.dosing_percentage):
|
||||
raise ValueError("dosing percentage must be finite")
|
||||
if value.channel_status:
|
||||
total += value.dosing_percentage
|
||||
return abs(total - 100.0) <= tolerance
|
||||
|
||||
|
||||
def channel_rates(
|
||||
samples: Iterable[DosingChannelSample],
|
||||
*,
|
||||
material_names: dict[str, str] | None = None,
|
||||
percentage_tolerance: float = 1.0,
|
||||
) -> tuple[ChannelRate, ...]:
|
||||
"""Derive rates and conservative material identity for one aligned timestamp.
|
||||
|
||||
A material value which occurs on more than one channel is deliberately not
|
||||
attributed to either channel: PLC assignments are not reliable enough to
|
||||
resolve that ambiguity from a previous sample.
|
||||
"""
|
||||
values = tuple(samples)
|
||||
validate_active_percentage_sum(values, tolerance=percentage_tolerance)
|
||||
counts = {
|
||||
material_number: sum(sample.material_number == material_number for sample in values)
|
||||
for material_number in (sample.material_number for sample in values)
|
||||
if material_number is not None
|
||||
}
|
||||
names = material_names or {}
|
||||
result = []
|
||||
for value in values:
|
||||
if not isfinite(value.total_rate_kg_per_hour) or not isfinite(value.screw_speed):
|
||||
raise ValueError("total rate and screw speed must be finite")
|
||||
if value.material_number is None:
|
||||
number, name, status = None, None, MATERIAL_UNAVAILABLE
|
||||
elif counts[value.material_number] > 1:
|
||||
number, name, status = None, None, MATERIAL_AMBIGUOUS
|
||||
elif value.material_number not in names and names:
|
||||
number, name, status = value.material_number, None, MATERIAL_UNMAPPED
|
||||
else:
|
||||
number, name, status = (
|
||||
value.material_number,
|
||||
names.get(value.material_number),
|
||||
MATERIAL_ASSIGNED,
|
||||
)
|
||||
rate = (
|
||||
value.total_rate_kg_per_hour * value.dosing_percentage / 100
|
||||
if value.channel_status
|
||||
else 0.0
|
||||
)
|
||||
result.append(
|
||||
ChannelRate(
|
||||
sample=MaterialSample(value.timestamp, rate, 1.0 if value.channel_status else 0.0),
|
||||
material_number=number,
|
||||
material_name=name,
|
||||
material_mapping_status=status,
|
||||
)
|
||||
)
|
||||
return tuple(result)
|
||||
@@ -3,7 +3,9 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
from math import isfinite
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from production_analytics.calculations.channel_material import DosingChannelSample
|
||||
from production_analytics.calculations.material_consumption import (
|
||||
MaterialSample,
|
||||
area_application_rate_kg_per_hour,
|
||||
@@ -11,6 +13,9 @@ from production_analytics.calculations.material_consumption import (
|
||||
)
|
||||
from production_analytics.enlyze.exploration import ExplorationClient
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from production_analytics.service.channel_material import ChannelDefinition
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class EnlyzeProductionRun:
|
||||
@@ -22,6 +27,24 @@ class EnlyzeProductionRun:
|
||||
end: datetime | None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class EnlyzeDowntime:
|
||||
"""ENLYZE downtime source record. ``end`` and ``reason`` are deliberately nullable."""
|
||||
|
||||
uuid: str
|
||||
machine_id: str
|
||||
source_type: str
|
||||
start: datetime
|
||||
end: datetime | None
|
||||
comment: str | None
|
||||
reason_id: str | None
|
||||
reason_name: str | None
|
||||
reason_description: str | None
|
||||
reason_group: str | None
|
||||
reason_category: str | None
|
||||
source_updated_at: datetime | None
|
||||
|
||||
|
||||
class EnlyzeApiGateway:
|
||||
def __init__(self, client: ExplorationClient) -> None:
|
||||
self._client = client
|
||||
@@ -60,11 +83,16 @@ class EnlyzeApiGateway:
|
||||
end = _parse_timestamp(item["end"]) if item["end"] is not None else None
|
||||
if end is not None and end < start:
|
||||
raise ValueError("run end must not precede start")
|
||||
runs.append(EnlyzeProductionRun(
|
||||
uuid=item["uuid"], machine_id=item["machine"],
|
||||
product_id=item.get("product"), production_order=item["production_order"],
|
||||
start=start, end=end,
|
||||
))
|
||||
runs.append(
|
||||
EnlyzeProductionRun(
|
||||
uuid=item["uuid"],
|
||||
machine_id=item["machine"],
|
||||
product_id=item.get("product"),
|
||||
production_order=item["production_order"],
|
||||
start=start,
|
||||
end=end,
|
||||
)
|
||||
)
|
||||
next_cursor = None
|
||||
if "metadata" in response.body:
|
||||
metadata = response.body["metadata"]
|
||||
@@ -85,6 +113,45 @@ class EnlyzeApiGateway:
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
raise ValueError(f"Invalid production-run response on page {page}: {exc}") from exc
|
||||
|
||||
def get_downtimes(
|
||||
self,
|
||||
machine_id: str,
|
||||
*,
|
||||
start: datetime | None = None,
|
||||
) -> list[EnlyzeDowntime]:
|
||||
"""Retrieve machine downtimes and follow ENLYZE cursor pagination.
|
||||
|
||||
``start`` is an inclusive source-time lookback; callers use it for reconciliation,
|
||||
not as a claim that records before it cannot subsequently be edited.
|
||||
"""
|
||||
if start is not None and (start.tzinfo is None or start.utcoffset() is None):
|
||||
raise ValueError("start must be timezone-aware")
|
||||
params = {"machine": machine_id}
|
||||
if start is not None:
|
||||
params["start"] = start.astimezone(UTC).isoformat()
|
||||
result: list[EnlyzeDowntime] = []
|
||||
cursors: set[str] = set()
|
||||
page = 1
|
||||
while True:
|
||||
response = self._client.get("/v2/downtimes", params)
|
||||
try:
|
||||
if not isinstance(response.body, dict) or not isinstance(
|
||||
response.body["data"], list
|
||||
):
|
||||
raise ValueError("body data must be a list")
|
||||
for item in response.body["data"]:
|
||||
result.append(_parse_downtime(item, machine_id))
|
||||
cursor = _next_cursor(response.body)
|
||||
if cursor is None:
|
||||
return result
|
||||
if cursor in cursors:
|
||||
raise ValueError("next_cursor has already been followed")
|
||||
cursors.add(cursor)
|
||||
params = {**params, "cursor": cursor}
|
||||
page += 1
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
raise ValueError(f"Invalid downtime response on page {page}: {exc}") from exc
|
||||
|
||||
def get_material_samples(
|
||||
self,
|
||||
*,
|
||||
@@ -152,7 +219,9 @@ class EnlyzeApiGateway:
|
||||
if nominal_width_m is None or specific_discharge_kg_per_rev_m is None:
|
||||
raise ValueError("rotational discharge requires width and factor")
|
||||
rate = rotational_discharge_rate_kg_per_hour(
|
||||
sources, nominal_width_m, specific_discharge_kg_per_rev_m,
|
||||
sources,
|
||||
nominal_width_m,
|
||||
specific_discharge_kg_per_rev_m,
|
||||
)
|
||||
elif application_variable_ids:
|
||||
if nominal_width_m is None:
|
||||
@@ -167,15 +236,21 @@ class EnlyzeApiGateway:
|
||||
from production_analytics.calculations.material_application import (
|
||||
rotational_application_g_m2,
|
||||
)
|
||||
|
||||
application = rotational_application_g_m2(
|
||||
sources, specific_discharge_kg_per_rev_m, gate,
|
||||
sources,
|
||||
specific_discharge_kg_per_rev_m,
|
||||
gate,
|
||||
process_application_gate_threshold,
|
||||
)
|
||||
samples.append(MaterialSample(
|
||||
timestamp=_parse_timestamp(record[time_index]),
|
||||
material_rate_kg_per_hour=float(rate), gate_value=float(gate),
|
||||
application_g_m2=application,
|
||||
))
|
||||
samples.append(
|
||||
MaterialSample(
|
||||
timestamp=_parse_timestamp(record[time_index]),
|
||||
material_rate_kg_per_hour=float(rate),
|
||||
gate_value=float(gate),
|
||||
application_g_m2=application,
|
||||
)
|
||||
)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValueError(f"malformed record {index}: {exc}") from exc
|
||||
next_cursor = None
|
||||
@@ -198,6 +273,199 @@ class EnlyzeApiGateway:
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
raise ValueError(f"Invalid timeseries response on page {page}: {exc}") from exc
|
||||
|
||||
def get_dosing_channel_samples(
|
||||
self,
|
||||
*,
|
||||
machine_id: str,
|
||||
channels: tuple["ChannelDefinition", ...],
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
) -> dict[str, list[DosingChannelSample]]:
|
||||
"""Read configured dosing channels through the gateway only.
|
||||
|
||||
Configurations name stable PLC origins; ENLYZE UUIDs are resolved at poll
|
||||
time and must be unique, preventing a silently stale variable mapping.
|
||||
"""
|
||||
if (
|
||||
start.tzinfo is None
|
||||
or start.utcoffset() is None
|
||||
or end.tzinfo is None
|
||||
or end.utcoffset() is None
|
||||
):
|
||||
raise ValueError("dosing channel window must be timezone-aware")
|
||||
if end < start:
|
||||
raise ValueError("end must not precede start")
|
||||
resolved = self.resolve_dosing_channel_signal_refs(
|
||||
machine_id=machine_id,
|
||||
channels=channels,
|
||||
)
|
||||
variable_ids = list(
|
||||
dict.fromkeys(
|
||||
variable_id
|
||||
for references in resolved.values()
|
||||
for variable_id in references
|
||||
if variable_id is not None
|
||||
)
|
||||
)
|
||||
body = {
|
||||
"machine": machine_id,
|
||||
"start": start.astimezone(UTC).isoformat(),
|
||||
"end": end.astimezone(UTC).isoformat(),
|
||||
"variables": [{"uuid": x} for x in variable_ids],
|
||||
}
|
||||
result = {key: [] for key in resolved}
|
||||
cursors: set[str] = set()
|
||||
while True:
|
||||
response = self._client.post_json("/v2/timeseries", body)
|
||||
try:
|
||||
data = response.body["data"]
|
||||
columns = data["columns"]
|
||||
if not isinstance(columns, list) or any(
|
||||
columns.count(x) != 1 for x in ("time", *variable_ids)
|
||||
):
|
||||
raise ValueError("required columns must occur exactly once")
|
||||
if not isinstance(data["records"], list):
|
||||
raise ValueError("records must be a list")
|
||||
for row in data["records"]:
|
||||
if not isinstance(row, list) or len(row) != len(columns):
|
||||
raise ValueError("record must match columns")
|
||||
timestamp = _parse_timestamp(row[columns.index("time")])
|
||||
for key, (total, status, percentage, screw, material) in resolved.items():
|
||||
sample = self._dosing_sample(
|
||||
row, columns, timestamp, total, status, percentage, screw, material
|
||||
)
|
||||
if sample is not None:
|
||||
result[key].append(sample)
|
||||
cursor = _next_cursor(response.body)
|
||||
if cursor is None:
|
||||
return result
|
||||
if cursor in cursors:
|
||||
raise ValueError("next_cursor has already been followed")
|
||||
cursors.add(cursor)
|
||||
body = {**body, "cursor": cursor}
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
raise ValueError(f"Invalid dosing timeseries response: {exc}") from exc
|
||||
|
||||
def resolve_dosing_channel_signal_refs(
|
||||
self,
|
||||
*,
|
||||
machine_id: str,
|
||||
channels: tuple["ChannelDefinition", ...],
|
||||
) -> dict[str, tuple[str, str, str, str, str | None]]:
|
||||
"""Resolve configured PLC origins without reading samples or writing state."""
|
||||
origins = self._variables_by_origin(machine_id)
|
||||
resolved: dict[str, tuple[str, str, str, str, str | None]] = {}
|
||||
for channel in channels:
|
||||
refs = (
|
||||
channel.total_rate_signal_ref,
|
||||
channel.status_signal_ref,
|
||||
channel.percentage_signal_ref,
|
||||
channel.screw_speed_signal_ref,
|
||||
)
|
||||
ids = tuple(self._unique_origin(origins, ref) for ref in refs)
|
||||
material = (
|
||||
self._unique_origin(origins, channel.material_number_signal_ref)
|
||||
if channel.material_number_signal_ref
|
||||
else None
|
||||
)
|
||||
key = channel.channel + "\0" + channel.extruder
|
||||
resolved[key] = (*ids, material)
|
||||
return resolved
|
||||
|
||||
@staticmethod
|
||||
def _dosing_sample(
|
||||
row: list[object],
|
||||
columns: list[object],
|
||||
timestamp: datetime,
|
||||
total: str,
|
||||
status: str,
|
||||
percentage: str,
|
||||
screw: str,
|
||||
material: str | None,
|
||||
) -> DosingChannelSample | None:
|
||||
def numeric(variable: str) -> float:
|
||||
value = row[columns.index(variable)]
|
||||
if (
|
||||
isinstance(value, bool)
|
||||
or not isinstance(value, (int, float))
|
||||
or not isfinite(value)
|
||||
):
|
||||
raise ValueError("dosing values must be finite numeric")
|
||||
return float(value)
|
||||
|
||||
for variable in (total, percentage, screw):
|
||||
if row[columns.index(variable)] is None:
|
||||
return None
|
||||
|
||||
status_value = row[columns.index(status)]
|
||||
if status_value is None:
|
||||
enabled = False
|
||||
elif isinstance(status_value, bool):
|
||||
enabled = status_value
|
||||
elif (
|
||||
isinstance(status_value, (int, float))
|
||||
and not isinstance(status_value, bool)
|
||||
and isfinite(status_value)
|
||||
):
|
||||
enabled = status_value != 0
|
||||
else:
|
||||
raise ValueError("dosing status must be boolean or numeric")
|
||||
material_value = None if material is None else row[columns.index(material)]
|
||||
if material_value is not None and not isinstance(material_value, (str, int, float)):
|
||||
raise ValueError("material assignment must be scalar or null")
|
||||
return DosingChannelSample(
|
||||
timestamp,
|
||||
numeric(total),
|
||||
enabled,
|
||||
numeric(percentage),
|
||||
numeric(screw),
|
||||
None if material_value is None else str(material_value),
|
||||
)
|
||||
|
||||
def _variables_by_origin(self, machine_id: str) -> dict[str, list[str]]:
|
||||
params = {"machine": machine_id}
|
||||
result: dict[str, list[str]] = {}
|
||||
cursors: set[str] = set()
|
||||
while True:
|
||||
response = self._client.get("/v2/variables", params)
|
||||
try:
|
||||
items = response.body["data"]
|
||||
if not isinstance(items, list):
|
||||
raise ValueError("data must be a list")
|
||||
for item in items:
|
||||
uuid = item["uuid"]
|
||||
if not isinstance(uuid, str):
|
||||
raise ValueError("variable uuid must be a string")
|
||||
details = item.get("details")
|
||||
if not isinstance(details, dict):
|
||||
raise ValueError("variable details must be an object")
|
||||
origin_identifier = details.get("origin_identifier")
|
||||
if origin_identifier is None:
|
||||
# Derived ENLYZE variables do not have a PLC origin.
|
||||
continue
|
||||
if not isinstance(origin_identifier, dict):
|
||||
raise ValueError("variable origin_identifier must be an object")
|
||||
code = origin_identifier.get("code")
|
||||
if not isinstance(code, str):
|
||||
raise ValueError("variable origin must be a string")
|
||||
result.setdefault(code, []).append(uuid)
|
||||
cursor = _next_cursor(response.body)
|
||||
if cursor is None:
|
||||
return result
|
||||
if cursor in cursors:
|
||||
raise ValueError("next_cursor has already been followed")
|
||||
cursors.add(cursor)
|
||||
params = {**params, "cursor": cursor}
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
raise ValueError(f"Invalid variable response: {exc}") from exc
|
||||
|
||||
@staticmethod
|
||||
def _unique_origin(origins: dict[str, list[str]], origin: str) -> str:
|
||||
matches = origins.get(origin, [])
|
||||
if len(matches) != 1:
|
||||
raise ValueError(f"Configured PLC origin {origin!r} is unavailable or ambiguous")
|
||||
return matches[0]
|
||||
|
||||
|
||||
def _parse_timestamp(value: str) -> datetime:
|
||||
if not isinstance(value, str):
|
||||
@@ -206,3 +474,67 @@ def _parse_timestamp(value: str) -> datetime:
|
||||
if timestamp.tzinfo is None or timestamp.utcoffset() is None:
|
||||
raise ValueError("timestamp must be timezone-aware")
|
||||
return timestamp.astimezone(UTC)
|
||||
|
||||
|
||||
def _next_cursor(body: dict[object, object]) -> str | None:
|
||||
if "metadata" not in body:
|
||||
return None
|
||||
metadata = body["metadata"]
|
||||
if not isinstance(metadata, dict) or "next_cursor" not in metadata:
|
||||
raise ValueError("metadata must be an object with a next_cursor field")
|
||||
cursor = metadata["next_cursor"]
|
||||
if cursor is not None and (not isinstance(cursor, str) or not cursor):
|
||||
raise ValueError("next_cursor must be null or a non-empty string")
|
||||
return cursor
|
||||
|
||||
|
||||
def _nullable_text(value: object, name: str) -> str | None:
|
||||
if value is not None and not isinstance(value, str):
|
||||
raise ValueError(f"{name} must be a string or null")
|
||||
return value
|
||||
|
||||
|
||||
def _parse_downtime(item: object, machine_id: str) -> EnlyzeDowntime:
|
||||
if not isinstance(item, dict):
|
||||
raise ValueError("downtime must be an object")
|
||||
for key in ("uuid", "machine", "type", "start"):
|
||||
if not isinstance(item.get(key), str) or not item[key]:
|
||||
raise ValueError(f"{key} must be a non-empty string")
|
||||
if item["machine"] != machine_id:
|
||||
raise ValueError("downtime machine does not match requested machine")
|
||||
start = _parse_timestamp(item["start"])
|
||||
end_value = item.get("end")
|
||||
end = _parse_timestamp(end_value) if end_value is not None else None
|
||||
if end is not None and end < start:
|
||||
raise ValueError("downtime end must not precede start")
|
||||
reason = item.get("reason")
|
||||
if reason is not None and not isinstance(reason, dict):
|
||||
raise ValueError("reason must be an object or null")
|
||||
if reason is not None:
|
||||
for key in ("uuid", "name", "category"):
|
||||
if not isinstance(reason.get(key), str) or not reason[key]:
|
||||
raise ValueError(f"reason {key} must be a non-empty string")
|
||||
updated = item.get("updated")
|
||||
if updated is not None and not isinstance(updated, dict):
|
||||
raise ValueError("updated must be an object or null")
|
||||
updated_at = None
|
||||
if updated is not None:
|
||||
updated_at = _parse_timestamp(updated.get("timestamp"))
|
||||
return EnlyzeDowntime(
|
||||
uuid=item["uuid"],
|
||||
machine_id=machine_id,
|
||||
source_type=item["type"],
|
||||
start=start,
|
||||
end=end,
|
||||
comment=_nullable_text(item.get("comment"), "comment"),
|
||||
reason_id=None if reason is None else reason["uuid"],
|
||||
reason_name=None if reason is None else reason["name"],
|
||||
reason_description=None
|
||||
if reason is None
|
||||
else _nullable_text(reason.get("description"), "reason description"),
|
||||
reason_group=None
|
||||
if reason is None
|
||||
else _nullable_text(reason.get("group"), "reason group"),
|
||||
reason_category=None if reason is None else reason["category"],
|
||||
source_updated_at=updated_at,
|
||||
)
|
||||
@@ -0,0 +1,246 @@
|
||||
"""Stateful polling for configured extruder dosing channels."""
|
||||
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Protocol
|
||||
|
||||
from production_analytics.calculations.channel_material import (
|
||||
ChannelRate,
|
||||
DosingChannelSample,
|
||||
channel_rates,
|
||||
validate_active_percentage_sum,
|
||||
)
|
||||
from production_analytics.calculations.material_consumption import (
|
||||
MaterialConsumptionIntegrator,
|
||||
MaterialIntegrationState,
|
||||
MaterialIntegratorConfig,
|
||||
)
|
||||
from production_analytics.enlyze.gateway import EnlyzeProductionRun
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChannelDefinition:
|
||||
extruder: str
|
||||
channel: str
|
||||
total_rate_signal_ref: str
|
||||
status_signal_ref: str
|
||||
percentage_signal_ref: str
|
||||
screw_speed_signal_ref: str
|
||||
material_number_signal_ref: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChannelPollingState:
|
||||
run_id: str
|
||||
integration_state: MaterialIntegrationState
|
||||
|
||||
|
||||
class ChannelStateStore(Protocol):
|
||||
def load(
|
||||
self, machine_id: str, extruder: str, channel: str, production_order: str
|
||||
) -> ChannelPollingState | None: ...
|
||||
def save(
|
||||
self,
|
||||
machine_id: str,
|
||||
extruder: str,
|
||||
channel: str,
|
||||
production_order: str,
|
||||
state: ChannelPollingState,
|
||||
) -> None: ...
|
||||
|
||||
|
||||
class ChannelGateway(Protocol):
|
||||
def get_open_production_run(self, machine_id: str) -> EnlyzeProductionRun | None: ...
|
||||
def get_production_runs(self, machine_id: str) -> list[EnlyzeProductionRun]: ...
|
||||
def get_dosing_channel_samples(
|
||||
self,
|
||||
*,
|
||||
machine_id: str,
|
||||
channels: tuple[ChannelDefinition, ...],
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
) -> dict[str, list[DosingChannelSample]]: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ChannelPollResult:
|
||||
run: EnlyzeProductionRun
|
||||
extruder: str
|
||||
channel: str
|
||||
state: MaterialIntegrationState
|
||||
material_number: str | None
|
||||
material_name: str | None
|
||||
material_mapping_status: str
|
||||
percentage_sum_valid: bool
|
||||
|
||||
|
||||
class ChannelMaterialPollingService:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
gateway: ChannelGateway,
|
||||
state_store: ChannelStateStore,
|
||||
machine_id: str,
|
||||
channels: tuple[ChannelDefinition, ...],
|
||||
max_sample_gap_seconds: float,
|
||||
percentage_tolerance: float = 1.0,
|
||||
material_names: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
if not channels or len({(c.extruder, c.channel) for c in channels}) != len(channels):
|
||||
raise ValueError("channels must be non-empty and uniquely identified")
|
||||
self.gateway, self.state_store, self.machine_id, self.channels = (
|
||||
gateway,
|
||||
state_store,
|
||||
machine_id,
|
||||
channels,
|
||||
)
|
||||
self.config = MaterialIntegratorConfig(0.5, max_sample_gap_seconds)
|
||||
self.percentage_tolerance, self.material_names = percentage_tolerance, material_names or {}
|
||||
|
||||
def poll_once(self, *, now: datetime) -> tuple[ChannelPollResult, ...] | None:
|
||||
if now.tzinfo is None or now.utcoffset() is None:
|
||||
raise ValueError("now must be timezone-aware")
|
||||
run = self.gateway.get_open_production_run(self.machine_id)
|
||||
if run is None or now < run.start:
|
||||
return None
|
||||
# Each channel is separately checkpointed so an E4-style extra extruder
|
||||
# can be introduced without changing state identity.
|
||||
states: dict[str, ChannelPollingState | None] = {
|
||||
c.channel + "\0" + c.extruder: self.state_store.load(
|
||||
self.machine_id, c.extruder, c.channel, run.production_order
|
||||
)
|
||||
for c in self.channels
|
||||
}
|
||||
# A missing checkpoint is bootstrapped over each closed run separately.
|
||||
# Resetting the temporal baseline at every run boundary is essential:
|
||||
# a short wall-clock gap between ENLYZE runs must not become material.
|
||||
if all(state is None for state in states.values()):
|
||||
totals = {key: MaterialIntegrationState() for key in states}
|
||||
previous_runs = sorted(
|
||||
(
|
||||
candidate
|
||||
for candidate in self.gateway.get_production_runs(self.machine_id)
|
||||
if candidate.production_order == run.production_order
|
||||
and candidate.uuid != run.uuid
|
||||
and candidate.end is not None
|
||||
and candidate.end <= run.start
|
||||
),
|
||||
key=lambda candidate: candidate.start,
|
||||
)
|
||||
for previous in previous_runs:
|
||||
historical = self._derive(
|
||||
self.gateway.get_dosing_channel_samples(
|
||||
machine_id=self.machine_id,
|
||||
channels=self.channels,
|
||||
start=previous.start,
|
||||
end=previous.end,
|
||||
)
|
||||
)
|
||||
for key, samples in historical.items():
|
||||
integrator = MaterialConsumptionIntegrator(self.config, totals[key])
|
||||
integrator.process_many(rate.sample for rate, _ in samples)
|
||||
state = integrator.state
|
||||
totals[key] = MaterialIntegrationState(
|
||||
cumulative_consumption_kg=state.cumulative_consumption_kg,
|
||||
integrated_running_seconds=state.integrated_running_seconds,
|
||||
)
|
||||
states = {key: ChannelPollingState("bootstrap", state) for key, state in totals.items()}
|
||||
start = min(
|
||||
(
|
||||
s.integration_state.last_processed_timestamp
|
||||
for s in states.values()
|
||||
if s is not None
|
||||
and s.run_id == run.uuid
|
||||
and s.integration_state.last_processed_timestamp is not None
|
||||
),
|
||||
default=run.start,
|
||||
)
|
||||
if now < start:
|
||||
return None
|
||||
raw = self.gateway.get_dosing_channel_samples(
|
||||
machine_id=self.machine_id, channels=self.channels, start=start, end=now
|
||||
)
|
||||
derived = self._derive(raw)
|
||||
results = []
|
||||
for definition in self.channels:
|
||||
key = definition.channel + "\0" + definition.extruder
|
||||
saved = states[key]
|
||||
initial = (
|
||||
saved.integration_state
|
||||
if saved is not None and saved.run_id == run.uuid
|
||||
else MaterialIntegrationState(
|
||||
cumulative_consumption_kg=0.0
|
||||
if saved is None
|
||||
else saved.integration_state.cumulative_consumption_kg,
|
||||
integrated_running_seconds=0.0
|
||||
if saved is None
|
||||
else saved.integration_state.integrated_running_seconds,
|
||||
)
|
||||
)
|
||||
integrator = MaterialConsumptionIntegrator(self.config, initial)
|
||||
samples = derived[key]
|
||||
latest: ChannelRate | None = None
|
||||
quality = True
|
||||
for rate, sample_quality in samples:
|
||||
if (
|
||||
initial.last_processed_timestamp is not None
|
||||
and rate.sample.timestamp < initial.last_processed_timestamp
|
||||
):
|
||||
continue
|
||||
integrator.process(rate.sample)
|
||||
latest = rate
|
||||
quality = quality and sample_quality
|
||||
state = integrator.state
|
||||
self.state_store.save(
|
||||
self.machine_id,
|
||||
definition.extruder,
|
||||
definition.channel,
|
||||
run.production_order,
|
||||
ChannelPollingState(run.uuid, state),
|
||||
)
|
||||
results.append(
|
||||
ChannelPollResult(
|
||||
run,
|
||||
definition.extruder,
|
||||
definition.channel,
|
||||
state,
|
||||
None if latest is None else latest.material_number,
|
||||
None if latest is None else latest.material_name,
|
||||
"UNAVAILABLE" if latest is None else latest.material_mapping_status,
|
||||
quality,
|
||||
)
|
||||
)
|
||||
return tuple(results)
|
||||
|
||||
def _derive(
|
||||
self, raw: dict[str, list[DosingChannelSample]]
|
||||
) -> dict[str, list[tuple[ChannelRate, bool]]]:
|
||||
"""Evaluate aligned channel records with extruder-scoped quality rules."""
|
||||
by_timestamp: dict[datetime, dict[str, DosingChannelSample]] = defaultdict(dict)
|
||||
for definition in self.channels:
|
||||
key = definition.channel + "\0" + definition.extruder
|
||||
for sample in raw[key]:
|
||||
by_timestamp[sample.timestamp][key] = sample
|
||||
derived: dict[str, list[tuple[ChannelRate, bool]]] = defaultdict(list)
|
||||
for timestamp, values in sorted(by_timestamp.items()):
|
||||
if set(values) != set(raw):
|
||||
raise ValueError(f"unaligned channel samples at {timestamp.isoformat()}")
|
||||
grouped: dict[str, list[tuple[str, DosingChannelSample]]] = defaultdict(list)
|
||||
for key, sample in values.items():
|
||||
grouped[key.split("\0", 1)[1]].append((key, sample))
|
||||
for siblings in grouped.values():
|
||||
quality = validate_active_percentage_sum(
|
||||
(sample for _, sample in siblings),
|
||||
tolerance=self.percentage_tolerance,
|
||||
)
|
||||
for (key, _), rate in zip(
|
||||
siblings,
|
||||
channel_rates(
|
||||
(sample for _, sample in siblings),
|
||||
material_names=self.material_names,
|
||||
percentage_tolerance=self.percentage_tolerance,
|
||||
),
|
||||
):
|
||||
derived[key].append((rate, quality))
|
||||
return derived
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Sequential foreground runner for channel-level material consumption."""
|
||||
|
||||
import sys
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from datetime import UTC, datetime
|
||||
from typing import TextIO
|
||||
|
||||
from production_analytics.calculations.config import finite_number
|
||||
from production_analytics.enlyze.exploration import ConfigurationError, ExplorationError
|
||||
from production_analytics.service.channel_material import ChannelMaterialPollingService
|
||||
from production_analytics.service.postgres_channel_material import (
|
||||
PostgresChannelMaterialSnapshotWriter,
|
||||
)
|
||||
|
||||
|
||||
def utc_now() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
class ChannelMaterialPollingRunner:
|
||||
def __init__(
|
||||
self,
|
||||
service: ChannelMaterialPollingService,
|
||||
*,
|
||||
machine_id: str,
|
||||
calculation_id: str,
|
||||
snapshot_writer: PostgresChannelMaterialSnapshotWriter,
|
||||
poll_interval_seconds: float,
|
||||
clock: Callable[[], datetime] = utc_now,
|
||||
sleep: Callable[[float], None] = time.sleep,
|
||||
stdout: TextIO | None = None,
|
||||
stderr: TextIO | None = None,
|
||||
) -> None:
|
||||
self.service = service
|
||||
self.machine_id = machine_id
|
||||
self.calculation_id = calculation_id
|
||||
self.snapshot_writer = snapshot_writer
|
||||
self.interval = finite_number(poll_interval_seconds, "poll interval", positive=True)
|
||||
self.clock = clock
|
||||
self.sleep = sleep
|
||||
self.stdout = stdout if stdout is not None else sys.stdout
|
||||
self.stderr = stderr if stderr is not None else sys.stderr
|
||||
|
||||
def run_once(self) -> bool:
|
||||
"""Run and report one cycle; return whether channel snapshots were written."""
|
||||
now = self.clock()
|
||||
if now.tzinfo is None or now.utcoffset() is None:
|
||||
raise ConfigurationError("Runner clock must return timezone-aware timestamps")
|
||||
now = now.astimezone(UTC)
|
||||
prefix = f"{now.isoformat()} machine={self.machine_id!r}"
|
||||
results = self.service.poll_once(now=now)
|
||||
if results is None:
|
||||
print(
|
||||
f"{prefix} no open Production Run / no eligible polling window; snapshots=0",
|
||||
file=self.stdout,
|
||||
flush=True,
|
||||
)
|
||||
return False
|
||||
for result in results:
|
||||
self.snapshot_writer.write(
|
||||
timestamp=now,
|
||||
calculation_id=self.calculation_id,
|
||||
machine_id=self.machine_id,
|
||||
extruder=result.extruder,
|
||||
channel=result.channel,
|
||||
production_order=result.run.production_order,
|
||||
run_id=result.run.uuid,
|
||||
cumulative_consumption_kg=result.state.cumulative_consumption_kg,
|
||||
material_number=result.material_number,
|
||||
material_name=result.material_name,
|
||||
material_mapping_status=result.material_mapping_status,
|
||||
percentage_sum_valid=result.percentage_sum_valid,
|
||||
)
|
||||
print(
|
||||
f"{prefix} production_order={results[0].run.production_order!r} "
|
||||
f"channel snapshots={len(results)} written",
|
||||
file=self.stdout,
|
||||
flush=True,
|
||||
)
|
||||
return True
|
||||
|
||||
def run(self) -> None:
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
self.run_once()
|
||||
except ConfigurationError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
if isinstance(exc, ExplorationError):
|
||||
detail = f"ENLYZE/gateway: {type(exc).__name__}"
|
||||
else:
|
||||
detail = f"poll cycle: {type(exc).__name__}"
|
||||
print(f"channel material error {detail}", file=self.stderr, flush=True)
|
||||
self.sleep(self.interval)
|
||||
except KeyboardInterrupt:
|
||||
print("Channel material polling stopped", file=self.stdout, flush=True)
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Compose the live channel-level material polling application."""
|
||||
|
||||
import math
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from production_analytics.calculations.channel_config import ChannelMaterialCalculationConfig
|
||||
from production_analytics.calculations.config import finite_number
|
||||
from production_analytics.enlyze.exploration import (
|
||||
ConfigurationError,
|
||||
ExplorationClient,
|
||||
ExplorationSettings,
|
||||
load_secret_file,
|
||||
)
|
||||
from production_analytics.enlyze.gateway import EnlyzeApiGateway
|
||||
from production_analytics.service.channel_material import ChannelMaterialPollingService
|
||||
from production_analytics.service.channel_material_runner import ChannelMaterialPollingRunner
|
||||
from production_analytics.service.channel_material_state_store import JsonChannelMaterialStateStore
|
||||
from production_analytics.service.postgres_channel_material import (
|
||||
PostgresChannelMaterialSnapshotWriter,
|
||||
)
|
||||
from production_analytics.service.postgres_material import PostgresSettings
|
||||
|
||||
|
||||
def build_channel_material_runner(
|
||||
calculation: ChannelMaterialCalculationConfig,
|
||||
*,
|
||||
poll_interval_seconds: float,
|
||||
state_directory: Path,
|
||||
secrets_file: Path,
|
||||
) -> ChannelMaterialPollingRunner:
|
||||
finite_number(poll_interval_seconds, "poll interval", positive=True)
|
||||
environment = dict(os.environ)
|
||||
environment.update(load_secret_file(secrets_file))
|
||||
settings = ExplorationSettings.from_environment(environment)
|
||||
parsed = urlsplit(settings.base_url)
|
||||
if parsed.scheme not in {"http", "https"} or not parsed.hostname or parsed.username:
|
||||
raise ConfigurationError(
|
||||
"ENLYZE_BASE_URL must be an HTTP(S) server URL without credentials"
|
||||
)
|
||||
if not math.isfinite(settings.timeout_seconds):
|
||||
raise ConfigurationError("ENLYZE_HTTP_TIMEOUT_SECONDS must be finite")
|
||||
try:
|
||||
state_directory.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.TemporaryFile(dir=state_directory):
|
||||
pass
|
||||
except OSError as exc:
|
||||
raise ConfigurationError(
|
||||
f"State directory startup check failed: {type(exc).__name__}"
|
||||
) from exc
|
||||
postgres_settings = PostgresSettings.from_environment(environment)
|
||||
service = ChannelMaterialPollingService(
|
||||
gateway=EnlyzeApiGateway(ExplorationClient(settings)),
|
||||
state_store=JsonChannelMaterialStateStore(state_directory),
|
||||
machine_id=calculation.machine_ref,
|
||||
channels=calculation.channels,
|
||||
max_sample_gap_seconds=calculation.max_sample_gap_seconds,
|
||||
percentage_tolerance=calculation.percentage_tolerance,
|
||||
material_names=calculation.material_names,
|
||||
)
|
||||
return ChannelMaterialPollingRunner(
|
||||
service,
|
||||
machine_id=calculation.machine_ref,
|
||||
calculation_id=calculation.id,
|
||||
snapshot_writer=PostgresChannelMaterialSnapshotWriter(postgres_settings),
|
||||
poll_interval_seconds=poll_interval_seconds,
|
||||
)
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Restart-safe JSON state storage keyed by channel identity."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from production_analytics.calculations.material_state import (
|
||||
material_state_from_dict,
|
||||
material_state_to_dict,
|
||||
)
|
||||
from production_analytics.service.channel_material import ChannelPollingState
|
||||
|
||||
|
||||
class JsonChannelMaterialStateStore:
|
||||
def __init__(self, directory: str | Path) -> None:
|
||||
self.directory = Path(directory)
|
||||
|
||||
def _path(self, *identity: str) -> Path:
|
||||
digest = hashlib.sha256(json.dumps(identity).encode()).hexdigest()
|
||||
return self.directory / f"channel-material-{digest}.json"
|
||||
|
||||
def load(
|
||||
self, machine_id: str, extruder: str, channel: str, production_order: str
|
||||
) -> ChannelPollingState | None:
|
||||
path = self._path(machine_id, extruder, channel, production_order)
|
||||
if not path.exists():
|
||||
return None
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(data, dict) or not isinstance(data.get("run_id"), str):
|
||||
raise ValueError("Invalid channel material state")
|
||||
return ChannelPollingState(
|
||||
data["run_id"], material_state_from_dict(data["integration_state"])
|
||||
)
|
||||
|
||||
def save(
|
||||
self,
|
||||
machine_id: str,
|
||||
extruder: str,
|
||||
channel: str,
|
||||
production_order: str,
|
||||
state: ChannelPollingState,
|
||||
) -> None:
|
||||
self.directory.mkdir(parents=True, exist_ok=True)
|
||||
payload = {
|
||||
"run_id": state.run_id,
|
||||
"integration_state": material_state_to_dict(state.integration_state),
|
||||
}
|
||||
material_state_from_dict(payload["integration_state"])
|
||||
destination = self._path(machine_id, extruder, channel, production_order)
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w", encoding="utf-8", dir=self.directory, delete=False
|
||||
) as handle:
|
||||
temporary = Path(handle.name)
|
||||
json.dump(payload, handle, allow_nan=False)
|
||||
handle.flush()
|
||||
os.fsync(handle.fileno())
|
||||
os.replace(temporary, destination)
|
||||
@@ -0,0 +1,214 @@
|
||||
"""Generic ENLYZE downtime reconciliation and ERP order attribution."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from enum import StrEnum
|
||||
from math import isfinite
|
||||
from typing import Protocol
|
||||
|
||||
from production_analytics.context import build_enlyze_production_order
|
||||
from production_analytics.enlyze.gateway import EnlyzeDowntime
|
||||
from production_analytics.erp import CurrentWorkplaceStatus
|
||||
|
||||
|
||||
class DowntimeCategory(StrEnum):
|
||||
PLANNED = "PLANNED"
|
||||
UNPLANNED = "UNPLANNED"
|
||||
UNKNOWN = "UNKNOWN"
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ProductionOrderBoundary:
|
||||
machine_id: str
|
||||
production_order: str
|
||||
started_at: datetime
|
||||
ended_at: datetime | None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ProductionDowntimeEvent:
|
||||
external_id: str
|
||||
machine_id: str
|
||||
production_order: str | None
|
||||
source_type: str
|
||||
source_start: datetime
|
||||
source_end: datetime | None
|
||||
attributed_start: datetime | None
|
||||
attributed_end: datetime | None
|
||||
attributed_duration_seconds: float | None
|
||||
reason_id: str | None
|
||||
reason_name: str | None
|
||||
reason_description: str | None
|
||||
reason_group: str | None
|
||||
category: DowntimeCategory
|
||||
comment: str | None
|
||||
source_updated_at: datetime | None
|
||||
last_reconciled_at: datetime
|
||||
|
||||
|
||||
class DowntimeGateway(Protocol):
|
||||
def get_downtimes(
|
||||
self, machine_id: str, *, start: datetime | None = None
|
||||
) -> list[EnlyzeDowntime]: ...
|
||||
|
||||
|
||||
class WorkplaceStatusReader(Protocol):
|
||||
def get_current_workplace_status(self, workplace: str) -> CurrentWorkplaceStatus | None: ...
|
||||
|
||||
|
||||
class DowntimeRepository(Protocol):
|
||||
def current_boundary(self, machine_id: str) -> ProductionOrderBoundary | None: ...
|
||||
def save_boundary(self, boundary: ProductionOrderBoundary) -> None: ...
|
||||
def close_order_attribution(
|
||||
self, machine_id: str, production_order: str, ended_at: datetime
|
||||
) -> None: ...
|
||||
def upsert(self, event: ProductionDowntimeEvent) -> None: ...
|
||||
|
||||
|
||||
class DowntimeReconciliationService:
|
||||
"""Reconciles mutable source events without deriving a shift schedule."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
gateway: DowntimeGateway,
|
||||
erp: WorkplaceStatusReader,
|
||||
repository: DowntimeRepository,
|
||||
*,
|
||||
machine_id: str,
|
||||
workplace: str,
|
||||
production_order_format: str,
|
||||
lookback_hours: float = 48,
|
||||
full_reconciliation_hours: float = 24,
|
||||
completion_tolerance_m2: float = 0.001,
|
||||
) -> None:
|
||||
if not isfinite(lookback_hours) or lookback_hours <= 0:
|
||||
raise ValueError("lookback_hours must be positive")
|
||||
if not isfinite(full_reconciliation_hours) or full_reconciliation_hours <= 0:
|
||||
raise ValueError("full_reconciliation_hours must be positive")
|
||||
if not isfinite(completion_tolerance_m2) or completion_tolerance_m2 < 0:
|
||||
raise ValueError("completion_tolerance_m2 must be non-negative")
|
||||
self.gateway, self.erp, self.repository = gateway, erp, repository
|
||||
self.machine_id, self.workplace = machine_id, workplace
|
||||
self.production_order_format = production_order_format
|
||||
self.lookback = timedelta(hours=lookback_hours)
|
||||
self.full_reconciliation_interval = timedelta(hours=full_reconciliation_hours)
|
||||
self._last_full_reconciliation: datetime | None = None
|
||||
self.tolerance = completion_tolerance_m2
|
||||
|
||||
def reconcile_once(self, now: datetime) -> int:
|
||||
if now.tzinfo is None or now.utcoffset() is None:
|
||||
raise ValueError("now must be timezone-aware")
|
||||
now = now.astimezone(UTC)
|
||||
boundary = self._refresh_boundary(now)
|
||||
events = self.gateway.get_downtimes(self.machine_id, start=now - self.lookback)
|
||||
if (
|
||||
self._last_full_reconciliation is None
|
||||
or now - self._last_full_reconciliation >= self.full_reconciliation_interval
|
||||
):
|
||||
# ENLYZE does not guarantee that a delayed reason edit remains in a source-start
|
||||
# lookback window. A periodic full scan makes old open/UNKNOWN UUIDs mutable too.
|
||||
events = list(
|
||||
{
|
||||
event.uuid: event
|
||||
for event in [
|
||||
*events,
|
||||
*self.gateway.get_downtimes(self.machine_id),
|
||||
]
|
||||
}.values()
|
||||
)
|
||||
self._last_full_reconciliation = now
|
||||
for source in events:
|
||||
self.repository.upsert(self._event(source, boundary, now))
|
||||
return len(events)
|
||||
|
||||
def _refresh_boundary(self, now: datetime) -> ProductionOrderBoundary | None:
|
||||
status = self.erp.get_current_workplace_status(self.workplace)
|
||||
old = self.repository.current_boundary(self.machine_id)
|
||||
if status is None:
|
||||
return old
|
||||
order = build_enlyze_production_order(status.production_order, self.production_order_format)
|
||||
feedback_at = _aware_feedback(status.feedback_timestamp)
|
||||
if old is not None and old.production_order != order and old.ended_at is None:
|
||||
self.repository.close_order_attribution(
|
||||
self.machine_id, old.production_order, feedback_at
|
||||
)
|
||||
if old is None or old.production_order != order:
|
||||
current = ProductionOrderBoundary(self.machine_id, order, feedback_at, None)
|
||||
else:
|
||||
current = old
|
||||
if _is_complete(status, self.tolerance) and current.ended_at is None:
|
||||
current = ProductionOrderBoundary(
|
||||
current.machine_id,
|
||||
current.production_order,
|
||||
current.started_at,
|
||||
feedback_at,
|
||||
)
|
||||
self.repository.close_order_attribution(
|
||||
self.machine_id, current.production_order, feedback_at
|
||||
)
|
||||
self.repository.save_boundary(current)
|
||||
return current
|
||||
|
||||
def _event(
|
||||
self,
|
||||
source: EnlyzeDowntime,
|
||||
boundary: ProductionOrderBoundary | None,
|
||||
now: datetime,
|
||||
) -> ProductionDowntimeEvent:
|
||||
category = (
|
||||
DowntimeCategory(source.reason_category)
|
||||
if source.reason_category
|
||||
in {
|
||||
"PLANNED",
|
||||
"UNPLANNED",
|
||||
}
|
||||
else DowntimeCategory.UNKNOWN
|
||||
)
|
||||
attributed_start = attributed_end = None
|
||||
order = None
|
||||
if boundary is not None:
|
||||
lower = max(source.start, boundary.started_at)
|
||||
upper = source.end
|
||||
if boundary.ended_at is not None:
|
||||
upper = boundary.ended_at if upper is None else min(upper, boundary.ended_at)
|
||||
if upper is None or lower < upper:
|
||||
order, attributed_start, attributed_end = boundary.production_order, lower, upper
|
||||
duration = (
|
||||
None
|
||||
if attributed_start is None or attributed_end is None
|
||||
else (attributed_end - attributed_start).total_seconds()
|
||||
)
|
||||
return ProductionDowntimeEvent(
|
||||
source.uuid,
|
||||
source.machine_id,
|
||||
order,
|
||||
source.source_type,
|
||||
source.start,
|
||||
source.end,
|
||||
attributed_start,
|
||||
attributed_end,
|
||||
duration,
|
||||
source.reason_id,
|
||||
source.reason_name,
|
||||
source.reason_description,
|
||||
source.reason_group,
|
||||
category,
|
||||
source.comment,
|
||||
source.source_updated_at,
|
||||
now,
|
||||
)
|
||||
|
||||
|
||||
def _aware_feedback(value: datetime) -> datetime:
|
||||
if value.tzinfo is None or value.utcoffset() is None:
|
||||
raise ValueError("ERP feedback timestamp must be timezone-aware for downtime attribution")
|
||||
return value.astimezone(UTC)
|
||||
|
||||
|
||||
def _is_complete(status: CurrentWorkplaceStatus, tolerance: float) -> bool:
|
||||
remaining = status.remaining_quantity_m2
|
||||
if remaining is not None and remaining <= tolerance:
|
||||
return True
|
||||
if status.order_quantity_m2 is None or status.good_quantity_m2 is None:
|
||||
return False
|
||||
return status.good_quantity_m2 + tolerance >= status.order_quantity_m2
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Foreground reconciliation runner suitable for a systemd service."""
|
||||
|
||||
import sys
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from datetime import UTC, datetime
|
||||
from typing import TextIO
|
||||
|
||||
from production_analytics.calculations.config import finite_number
|
||||
from production_analytics.service.downtime import DowntimeReconciliationService
|
||||
|
||||
|
||||
class DowntimeReconciliationRunner:
|
||||
def __init__(
|
||||
self,
|
||||
service: DowntimeReconciliationService,
|
||||
*,
|
||||
machine_id: str,
|
||||
poll_interval_seconds: float = 300,
|
||||
clock: Callable[[], datetime] | None = None,
|
||||
sleep: Callable[[float], None] = time.sleep,
|
||||
stdout: TextIO | None = None,
|
||||
stderr: TextIO | None = None,
|
||||
) -> None:
|
||||
self.service, self.machine_id = service, machine_id
|
||||
self.interval = finite_number(poll_interval_seconds, "poll interval", positive=True)
|
||||
self.clock, self.sleep = clock or (lambda: datetime.now(UTC)), sleep
|
||||
self.stdout, self.stderr = stdout or sys.stdout, stderr or sys.stderr
|
||||
|
||||
def run(self) -> None:
|
||||
try:
|
||||
while True:
|
||||
now = self.clock()
|
||||
try:
|
||||
count = self.service.reconcile_once(now)
|
||||
print(
|
||||
f"{now.isoformat()} machine={self.machine_id!r} reconciled={count}",
|
||||
file=self.stdout,
|
||||
flush=True,
|
||||
)
|
||||
except Exception as exc:
|
||||
print(
|
||||
f"{now.isoformat()} machine={self.machine_id!r} reconciliation failed: "
|
||||
f"{type(exc).__name__}",
|
||||
file=self.stderr,
|
||||
flush=True,
|
||||
)
|
||||
self.sleep(self.interval)
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Build the generic downtime runner from a small, explicit YAML configuration."""
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from production_analytics.enlyze.exploration import (
|
||||
ExplorationClient,
|
||||
ExplorationSettings,
|
||||
load_secret_file,
|
||||
)
|
||||
from production_analytics.enlyze.gateway import EnlyzeApiGateway
|
||||
from production_analytics.erp import ErpSettings, ErpWorkplaceStatusGateway
|
||||
from production_analytics.service.downtime import DowntimeReconciliationService
|
||||
from production_analytics.service.downtime_runner import DowntimeReconciliationRunner
|
||||
from production_analytics.service.postgres_downtime import PostgresDowntimeRepository
|
||||
from production_analytics.service.postgres_material import PostgresSettings
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class DowntimeConfig:
|
||||
machine_id: str
|
||||
erp_workplace: str
|
||||
production_order_format: str
|
||||
poll_interval_seconds: float = 300
|
||||
lookback_hours: float = 48
|
||||
full_reconciliation_hours: float = 24
|
||||
completion_tolerance_m2: float = 0.001
|
||||
|
||||
|
||||
def load_downtime_config(path: Path) -> DowntimeConfig:
|
||||
raw = yaml.safe_load(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError("Downtime config must be a mapping")
|
||||
allowed = {field.name for field in DowntimeConfig.__dataclass_fields__.values()}
|
||||
if set(raw) - allowed or not {"machine_id", "erp_workplace", "production_order_format"} <= set(
|
||||
raw
|
||||
):
|
||||
raise ValueError("Downtime config has unknown or missing fields")
|
||||
for name in ("machine_id", "erp_workplace", "production_order_format"):
|
||||
if not isinstance(raw[name], str) or not raw[name].strip():
|
||||
raise ValueError(f"{name} must be a non-empty string")
|
||||
return DowntimeConfig(**raw)
|
||||
|
||||
|
||||
def build_downtime_runner(
|
||||
config_path: Path, *, secrets_file: Path, erp_secrets_file: Path
|
||||
) -> DowntimeReconciliationRunner:
|
||||
config = load_downtime_config(config_path)
|
||||
environment = {**os.environ, **load_secret_file(secrets_file)}
|
||||
erp_environment = {**os.environ, **load_secret_file(erp_secrets_file)}
|
||||
service = DowntimeReconciliationService(
|
||||
EnlyzeApiGateway(ExplorationClient(ExplorationSettings.from_environment(environment))),
|
||||
ErpWorkplaceStatusGateway(ErpSettings.from_environment(erp_environment)),
|
||||
PostgresDowntimeRepository(PostgresSettings.from_environment(os.environ)),
|
||||
machine_id=config.machine_id,
|
||||
workplace=config.erp_workplace,
|
||||
production_order_format=config.production_order_format,
|
||||
lookback_hours=config.lookback_hours,
|
||||
full_reconciliation_hours=config.full_reconciliation_hours,
|
||||
completion_tolerance_m2=config.completion_tolerance_m2,
|
||||
)
|
||||
return DowntimeReconciliationRunner(
|
||||
service, machine_id=config.machine_id, poll_interval_seconds=config.poll_interval_seconds
|
||||
)
|
||||
@@ -0,0 +1,63 @@
|
||||
"""PostgreSQL writer for channel-level cumulative consumption snapshots."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from production_analytics.service.postgres_material import PostgresSettings
|
||||
|
||||
|
||||
class PostgresChannelMaterialSnapshotWriter:
|
||||
def __init__(self, settings: PostgresSettings) -> None:
|
||||
self.settings = settings
|
||||
|
||||
def write(
|
||||
self,
|
||||
*,
|
||||
timestamp: datetime,
|
||||
calculation_id: str,
|
||||
machine_id: str,
|
||||
extruder: str,
|
||||
channel: str,
|
||||
production_order: str,
|
||||
run_id: str,
|
||||
cumulative_consumption_kg: float,
|
||||
material_number: str | None,
|
||||
material_name: str | None,
|
||||
material_mapping_status: str,
|
||||
percentage_sum_valid: bool,
|
||||
) -> None:
|
||||
import psycopg
|
||||
|
||||
with psycopg.connect(
|
||||
host=self.settings.host,
|
||||
port=self.settings.port,
|
||||
dbname=self.settings.dbname,
|
||||
user=self.settings.user,
|
||||
password=self.settings.password,
|
||||
connect_timeout=10,
|
||||
options="-c statement_timeout=10000",
|
||||
) as connection:
|
||||
connection.execute(
|
||||
"""INSERT INTO channel_material_consumption_snapshots
|
||||
(timestamp, calculation_id, machine_id, extruder, doser_channel,
|
||||
production_order, run_id, cumulative_consumption_kg,
|
||||
material_number, material_name, material_mapping_status,
|
||||
percentage_sum_valid)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (calculation_id, machine_id, extruder, doser_channel,
|
||||
production_order, timestamp)
|
||||
DO NOTHING""",
|
||||
(
|
||||
timestamp,
|
||||
calculation_id,
|
||||
machine_id,
|
||||
extruder,
|
||||
channel,
|
||||
production_order,
|
||||
run_id,
|
||||
cumulative_consumption_kg,
|
||||
material_number,
|
||||
material_name,
|
||||
material_mapping_status,
|
||||
percentage_sum_valid,
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,112 @@
|
||||
"""PostgreSQL persistence for reconciled ENLYZE downtime events."""
|
||||
|
||||
from production_analytics.service.downtime import ProductionDowntimeEvent, ProductionOrderBoundary
|
||||
from production_analytics.service.postgres_material import PostgresSettings
|
||||
|
||||
|
||||
class PostgresDowntimeRepository:
|
||||
def __init__(self, settings: PostgresSettings) -> None:
|
||||
self.settings = settings
|
||||
|
||||
def _connect(self):
|
||||
import psycopg
|
||||
|
||||
return psycopg.connect(
|
||||
host=self.settings.host,
|
||||
port=self.settings.port,
|
||||
dbname=self.settings.dbname,
|
||||
user=self.settings.user,
|
||||
password=self.settings.password,
|
||||
connect_timeout=10,
|
||||
options="-c statement_timeout=10000",
|
||||
)
|
||||
|
||||
def current_boundary(self, machine_id: str) -> ProductionOrderBoundary | None:
|
||||
with self._connect() as connection:
|
||||
row = connection.execute(
|
||||
"""SELECT machine_id, production_order, started_at, ended_at
|
||||
FROM production_order_attribution_state WHERE machine_id = %s""",
|
||||
(machine_id,),
|
||||
).fetchone()
|
||||
return None if row is None else ProductionOrderBoundary(*row)
|
||||
|
||||
def save_boundary(self, boundary: ProductionOrderBoundary) -> None:
|
||||
with self._connect() as connection:
|
||||
connection.execute(
|
||||
"""INSERT INTO production_order_attribution_state
|
||||
(machine_id, production_order, started_at, ended_at)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
ON CONFLICT (machine_id) DO UPDATE SET
|
||||
production_order = EXCLUDED.production_order,
|
||||
started_at = EXCLUDED.started_at, ended_at = EXCLUDED.ended_at""",
|
||||
(
|
||||
boundary.machine_id,
|
||||
boundary.production_order,
|
||||
boundary.started_at,
|
||||
boundary.ended_at,
|
||||
),
|
||||
)
|
||||
|
||||
def close_order_attribution(self, machine_id: str, production_order: str, ended_at) -> None:
|
||||
with self._connect() as connection:
|
||||
connection.execute(
|
||||
"""UPDATE production_downtime_events
|
||||
SET attributed_end = CASE WHEN source_end IS NULL OR source_end > %s THEN %s
|
||||
ELSE source_end END,
|
||||
attributed_duration_seconds = EXTRACT(EPOCH FROM
|
||||
(CASE WHEN source_end IS NULL OR source_end > %s
|
||||
THEN %s ELSE source_end END
|
||||
- attributed_start))
|
||||
WHERE machine_id = %s AND production_order = %s
|
||||
AND attributed_start IS NOT NULL AND attributed_end IS NULL""",
|
||||
(ended_at, ended_at, ended_at, ended_at, machine_id, production_order),
|
||||
)
|
||||
|
||||
def upsert(self, event: ProductionDowntimeEvent) -> None:
|
||||
with self._connect() as connection:
|
||||
connection.execute(
|
||||
"""INSERT INTO production_downtime_events
|
||||
(external_id, machine_id, production_order, source_type, source_start,
|
||||
source_end, attributed_start, attributed_end, attributed_duration_seconds,
|
||||
reason_id, reason_name, reason_description, reason_group, category, comment,
|
||||
source_updated_at, last_reconciled_at)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (external_id) DO UPDATE SET
|
||||
machine_id = EXCLUDED.machine_id,
|
||||
production_order = COALESCE(
|
||||
EXCLUDED.production_order, production_downtime_events.production_order),
|
||||
source_type = EXCLUDED.source_type, source_start = EXCLUDED.source_start,
|
||||
source_end = EXCLUDED.source_end,
|
||||
attributed_start = COALESCE(
|
||||
EXCLUDED.attributed_start, production_downtime_events.attributed_start),
|
||||
attributed_end = COALESCE(
|
||||
EXCLUDED.attributed_end, production_downtime_events.attributed_end),
|
||||
attributed_duration_seconds = COALESCE(
|
||||
EXCLUDED.attributed_duration_seconds,
|
||||
production_downtime_events.attributed_duration_seconds),
|
||||
reason_id = EXCLUDED.reason_id, reason_name = EXCLUDED.reason_name,
|
||||
reason_description = EXCLUDED.reason_description,
|
||||
reason_group = EXCLUDED.reason_group,
|
||||
category = EXCLUDED.category, comment = EXCLUDED.comment,
|
||||
source_updated_at = EXCLUDED.source_updated_at,
|
||||
last_reconciled_at = EXCLUDED.last_reconciled_at""",
|
||||
(
|
||||
event.external_id,
|
||||
event.machine_id,
|
||||
event.production_order,
|
||||
event.source_type,
|
||||
event.source_start,
|
||||
event.source_end,
|
||||
event.attributed_start,
|
||||
event.attributed_end,
|
||||
event.attributed_duration_seconds,
|
||||
event.reason_id,
|
||||
event.reason_name,
|
||||
event.reason_description,
|
||||
event.reason_group,
|
||||
event.category.value,
|
||||
event.comment,
|
||||
event.source_updated_at,
|
||||
event.last_reconciled_at,
|
||||
),
|
||||
)
|
||||
Reference in new issue
Block a user