Add unified shift reporting
This commit is contained in:
1 parent
1b060377aa
commit
41a5ae7a63
30 files changed
+3751
-9
No files matched your search
@@ -1,11 +1,12 @@
|
||||
"""Focused MSSQL boundary for dbo.GRAFANA_WORKPLACE_STATUS."""
|
||||
"""Focused read-only MSSQL boundaries for ERP status and confirmations."""
|
||||
|
||||
import os
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from datetime import UTC, datetime
|
||||
from decimal import Decimal
|
||||
from math import isfinite
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import pymssql
|
||||
|
||||
@@ -62,7 +63,7 @@ class ErpSettings:
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CurrentWorkplaceStatus:
|
||||
"""Latest ERP feedback; timestamp and timezone are preserved, with no freshness claim."""
|
||||
"""Latest ERP feedback with an aware timestamp; no freshness claim."""
|
||||
|
||||
workplace: str
|
||||
production_order: str
|
||||
@@ -120,7 +121,10 @@ class ErpWorkplaceStatusGateway:
|
||||
workplace=_text(row[0]), production_order=_text(row[1]),
|
||||
article_number=_text(row[2], nullable=True),
|
||||
article_description=_text(row[3], nullable=True),
|
||||
feedback_timestamp=row[4],
|
||||
feedback_timestamp=(
|
||||
row[4].replace(tzinfo=_PLANT_TIMEZONE)
|
||||
if row[4].utcoffset() is None else row[4]
|
||||
),
|
||||
order_quantity_m2=_number(row[5]), good_quantity_m2=_number(row[6]),
|
||||
remaining_quantity_m2=_number(row[7]), remaining_time_hours=_number(row[8]),
|
||||
remaining_rolls=_number(row[9]),
|
||||
@@ -146,3 +150,106 @@ def _number(value: object) -> float | None:
|
||||
if not isfinite(result):
|
||||
raise ValueError
|
||||
return result
|
||||
|
||||
|
||||
_PLANT_TIMEZONE = ZoneInfo("Europe/Berlin")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ProductionConfirmation:
|
||||
"""Historical ERP view row; quantities follow the view's production semantics."""
|
||||
|
||||
workplace: str
|
||||
production_order: str
|
||||
timestamp: datetime
|
||||
article_number: str | None
|
||||
article_description: str | None
|
||||
roll_number: str | None
|
||||
production_quantity_m2: float | None
|
||||
feedback_quantity_m2: float | None
|
||||
raw_material_number: str | None
|
||||
raw_material_description: str | None
|
||||
batch_number: str | None
|
||||
raw_material_quantity: float | None
|
||||
tenant: str | None
|
||||
|
||||
@property
|
||||
def is_finished_goods(self) -> bool:
|
||||
"""Positive feedback identifies finished production, independently of workplace."""
|
||||
return self.feedback_quantity_m2 is not None and self.feedback_quantity_m2 > 0
|
||||
|
||||
|
||||
_PRODUCTION_CONFIRMATION_SQL = """SELECT
|
||||
[Mandant], [Fertigungsauftragsnummer], [Zeitstempel], [Arbeitsplatz],
|
||||
[Artikelnummer], [Artikelbezeichnung], [Rollennummer], [Fertigungsmenge],
|
||||
[Rohstoffnummer], [Rohstoffbezeichnung], [Chargennummer], [Rückmeldemenge], [Fasermenge]
|
||||
FROM [dbo].[GRAFANA_PRODUCTION_CONFIRMATION]
|
||||
WHERE [Arbeitsplatz] = %s AND [Zeitstempel] >= %s AND [Zeitstempel] < %s
|
||||
ORDER BY [Zeitstempel], [Mandant], [Fertigungsauftragsnummer], [Rollennummer],
|
||||
[Artikelnummer], [Rohstoffnummer], [Chargennummer], [Fertigungsmenge],
|
||||
[Rückmeldemenge], [Fasermenge], [Artikelbezeichnung], [Rohstoffbezeichnung]"""
|
||||
|
||||
|
||||
class ErpProductionConfirmationGateway:
|
||||
"""Read complete view rows for an exact workplace and a plant-local [start, end).
|
||||
|
||||
SQL datetime stores no offset or fold. Ambiguous autumn timestamps are attached
|
||||
with fold=0 (the first occurrence); the source cannot identify their actual fold.
|
||||
Windows whose local end does not follow their local start cannot be expressed
|
||||
by this SQL predicate and are rejected. Repeated hours cannot be queried separately.
|
||||
"""
|
||||
|
||||
def __init__(self, settings: ErpSettings) -> None:
|
||||
self._settings = settings
|
||||
|
||||
def get_confirmations(
|
||||
self, *, workplace: str, start: datetime, end: datetime
|
||||
) -> list[ProductionConfirmation]:
|
||||
if not isinstance(workplace, str) or not workplace.strip() or "\x00" in workplace:
|
||||
raise ValueError("workplace must be a non-empty string without NUL bytes")
|
||||
for name, value in (("start", start), ("end", end)):
|
||||
if not isinstance(value, datetime) or value.tzinfo is None or value.utcoffset() is None:
|
||||
raise ValueError(f"{name} must be a timezone-aware datetime")
|
||||
if end.astimezone(UTC) <= start.astimezone(UTC):
|
||||
raise ValueError("end must follow start")
|
||||
local_start = start.astimezone(_PLANT_TIMEZONE).replace(tzinfo=None)
|
||||
local_end = end.astimezone(_PLANT_TIMEZONE).replace(tzinfo=None)
|
||||
if local_end <= local_start:
|
||||
raise ValueError("end must follow start in plant-local SQL time")
|
||||
try:
|
||||
with pymssql.connect(
|
||||
server=self._settings.host, port=self._settings.port,
|
||||
database=self._settings.database, user=self._settings.user,
|
||||
password=self._settings.password, login_timeout=10, timeout=10,
|
||||
) as connection:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
_PRODUCTION_CONFIRMATION_SQL, (workplace.strip(), local_start, local_end)
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
except Exception:
|
||||
raise ErpReadError("ERP production confirmation read failed") from None
|
||||
try:
|
||||
return [_production_confirmation(row) for row in rows]
|
||||
except Exception:
|
||||
raise ErpReadError("ERP production confirmation returned an invalid row") from None
|
||||
|
||||
|
||||
def _production_confirmation(row: tuple[object, ...]) -> ProductionConfirmation:
|
||||
if len(row) != 13 or not isinstance(row[2], datetime) or row[2].tzinfo is not None:
|
||||
raise ValueError
|
||||
return ProductionConfirmation(
|
||||
workplace=_text(row[3]),
|
||||
production_order=_text(row[1]),
|
||||
timestamp=row[2].replace(tzinfo=_PLANT_TIMEZONE, fold=0),
|
||||
article_number=_text(row[4], nullable=True),
|
||||
article_description=_text(row[5], nullable=True),
|
||||
roll_number=_text(row[6], nullable=True),
|
||||
production_quantity_m2=_number(row[7]),
|
||||
feedback_quantity_m2=_number(row[11]),
|
||||
raw_material_number=_text(row[8], nullable=True),
|
||||
raw_material_description=_text(row[9], nullable=True),
|
||||
batch_number=_text(row[10], nullable=True),
|
||||
raw_material_quantity=_number(row[12]),
|
||||
tenant=_text(row[0], nullable=True),
|
||||
)
|
||||
Reference in new issue
Block a user