Add unified shift reporting
This commit is contained in:
1 parent
1b060377aa
commit
41a5ae7a63
30 files changed
+3751
-9
No files matched your search
@@ -0,0 +1,124 @@
|
||||
"""Pure finished-production aggregation for an explicit workplace and shift window."""
|
||||
|
||||
from collections import defaultdict
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from math import fsum, isfinite
|
||||
|
||||
from production_analytics.erp import ProductionConfirmation
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ShiftProductionItem:
|
||||
production_order: str
|
||||
article_number: str | None
|
||||
article_description: str | None
|
||||
produced_m2: float
|
||||
roll_count: int
|
||||
first_confirmation_at: datetime
|
||||
last_confirmation_at: datetime
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ShiftProductionSummary:
|
||||
start: datetime
|
||||
end: datetime
|
||||
workplace: str
|
||||
total_produced_m2: float
|
||||
total_roll_count: int
|
||||
items: tuple[ShiftProductionItem, ...]
|
||||
|
||||
|
||||
def validate_shift_window(*, workplace: str, start: datetime, end: datetime) -> str:
|
||||
"""Validate inputs and return the exact workplace with surrounding padding removed."""
|
||||
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")
|
||||
_aware(start, "start")
|
||||
_aware(end, "end")
|
||||
# Compare instants, including distinct folds of the same local DST hour.
|
||||
if end.timestamp() <= start.timestamp():
|
||||
raise ValueError("end must follow start")
|
||||
return workplace.strip()
|
||||
|
||||
|
||||
def aggregate_shift_production(
|
||||
confirmations: Iterable[ProductionConfirmation],
|
||||
*,
|
||||
workplace: str,
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
) -> ShiftProductionSummary:
|
||||
"""Sum positive ERP feedback in [start, end), preserving supplied timezones.
|
||||
|
||||
Only rows for the exact workplace and window contribute. Every finished-goods
|
||||
row contributes its feedback m²; only roll counts are deduplicated, within each
|
||||
order/article/description group. Missing or blank roll numbers count as no roll.
|
||||
"""
|
||||
workplace = validate_shift_window(workplace=workplace, start=start, end=end)
|
||||
groups: dict[tuple[str, str | None, str | None], list[ProductionConfirmation]] = defaultdict(
|
||||
list
|
||||
)
|
||||
for confirmation in confirmations:
|
||||
_aware(confirmation.timestamp, "confirmation timestamp")
|
||||
if confirmation.workplace != workplace or not (
|
||||
start.timestamp() <= confirmation.timestamp.timestamp() < end.timestamp()
|
||||
):
|
||||
continue
|
||||
quantity = confirmation.feedback_quantity_m2
|
||||
if quantity is not None and (isinstance(quantity, bool) or not isfinite(quantity)):
|
||||
raise ValueError("feedback quantity must be finite")
|
||||
if not confirmation.is_finished_goods:
|
||||
continue
|
||||
groups[
|
||||
(
|
||||
confirmation.production_order,
|
||||
confirmation.article_number,
|
||||
confirmation.article_description,
|
||||
)
|
||||
].append(confirmation)
|
||||
items = []
|
||||
for (order, article, description), rows in groups.items():
|
||||
produced = fsum(row.feedback_quantity_m2 for row in rows)
|
||||
if not isfinite(produced):
|
||||
raise ValueError("produced quantity must be finite")
|
||||
items.append(
|
||||
ShiftProductionItem(
|
||||
production_order=order,
|
||||
article_number=article,
|
||||
article_description=description,
|
||||
produced_m2=produced,
|
||||
roll_count=len(
|
||||
{
|
||||
row.roll_number.strip()
|
||||
for row in rows
|
||||
if row.roll_number is not None and row.roll_number.strip()
|
||||
}
|
||||
),
|
||||
first_confirmation_at=min(
|
||||
rows, key=lambda row: row.timestamp.timestamp()
|
||||
).timestamp,
|
||||
last_confirmation_at=max(rows, key=lambda row: row.timestamp.timestamp()).timestamp,
|
||||
)
|
||||
)
|
||||
items.sort(
|
||||
key=lambda item: (
|
||||
item.first_confirmation_at.timestamp(),
|
||||
item.production_order,
|
||||
(item.article_number is not None, item.article_number or ""),
|
||||
(item.article_description is not None, item.article_description or ""),
|
||||
)
|
||||
)
|
||||
return ShiftProductionSummary(
|
||||
start=start,
|
||||
end=end,
|
||||
workplace=workplace,
|
||||
total_produced_m2=fsum(item.produced_m2 for item in items),
|
||||
total_roll_count=sum(item.roll_count for item in items),
|
||||
items=tuple(items),
|
||||
)
|
||||
|
||||
|
||||
def _aware(value: datetime, name: str) -> None:
|
||||
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")
|
||||
Reference in new issue
Block a user