From f6ebd78a4a33bffa6e8536ecc6b3f9c42d1d12fa Mon Sep 17 00:00:00 2001
From: Martin Tazl
Date: Tue, 20 Oct 2026 16:07:57 +0200
Subject: [PATCH] Add per-order speed and material KPIs
---
docs/shift-report.md | 57 ++-
.../calculations/shift_production.py | 2 +
.../presentation/production_order_html.py | 9 +-
.../presentation/shift_report.py | 9 +-
.../presentation/shift_report_html.py | 16 +-
.../service/fiber_allocation.py | 11 +
.../service/production_order.py | 179 ++++++++-
.../service/shift_report.py | 18 +
.../service/shift_report_runtime.py | 20 +-
src/production_analytics/web.py | 11 +-
tests/test_production_order.py | 18 +-
tests/test_production_order_kpi.py | 380 ++++++++++++++++++
tests/test_shift_report.py | 37 ++
tests/test_shift_report_html.py | 20 +
tests/test_shift_report_pdf.py | 16 +
15 files changed, 773 insertions(+), 30 deletions(-)
create mode 100644 tests/test_production_order_kpi.py
diff --git a/docs/shift-report.md b/docs/shift-report.md
index 2ad3c0a..c5099b9 100644
--- a/docs/shift-report.md
+++ b/docs/shift-report.md
@@ -1,11 +1,11 @@
# Unified shift-report composition
`ShiftReportService` composes the existing production, downtime, material, and
-speed services sequentially. It performs no database or HTTP reads itself and
-adds no quantity or interval aggregation. Its frozen `ShiftReport` reuses all
-four immutable summary models directly, preserving source metadata and items
-for PDF output and the live web UI. Presentation lives separately in
-`presentation.shift_report`; report composition remains output-independent.
+speed services sequentially. It performs no database or HTTP reads itself.
+For production rows it asks the shared production-order service for FA KPI
+attribution, preserving the original shift totals and source metadata. The
+frozen `ShiftReport` feeds both PDF and live web UI; presentation remains
+output-independent.
## Configuration and API
@@ -116,7 +116,7 @@ Speed is the arithmetic sample mean strictly above the configured production
gate: average speed while running, not average speed over the full shift.
Counts, gate, and active min/max are preserved in `report.speed`.
-Every enabled source is mandatory. Disabled sections are `None` in the report and
+Every enabled shift-wide source is mandatory. Disabled sections are `None` in the report and
render as unavailable; no source is queried for them. Exceptions propagate
immediately and later services are not called. An empty successful summary remains
an empty summary; a failed
@@ -243,8 +243,47 @@ FA, machine and shift sum their positive feedback m². Unknown or ambiguous
workplaces remain visible but cannot be assigned a configured shift.
The ERP gateway owns parameterized read-only SQL, the production-order service
-owns mapping and shift grouping, and a dedicated presenter renders HTML. The
-order/machine/shift model is the anchor for later order-specific KPI, roll/QS and
-setup-sheet views; those data are not included yet. ERP timestamps are local
+owns mapping, shift grouping, and approximate KPI windows, and a dedicated
+presenter renders HTML. The order/machine/shift model can later anchor roll/QS
+and setup-sheet views; those data are not included yet. ERP timestamps are local
datetimes without offsets; autumn repeated-hour rows are interpreted as the
first occurrence, as in the existing confirmation gateway.
+
+For each affected machine shift, positive ERP confirmations for **all** FAs are
+sorted by instant (FA breaks ties). Consecutive confirmations of one FA form a
+run. The boundary between adjacent runs is the midpoint between the preceding
+run's last confirmation and the next run's first confirmation. The first run
+starts at shift start and the last ends at shift end. Windows are half-open and
+do not overlap. If an FA returns after another FA, it has multiple windows but
+still one row for that shift. These windows are a practical estimate from ERP
+confirmation times, **not** exact machine-running intervals; idle periods at
+shift edges may be included. Exact same-time FA changes are sorted by FA; a
+zero-length window yields unavailable KPIs rather than an invented interval.
+
+Each window is passed to the existing speed service, retaining the machine's
+numeric, Boolean, or dwell activity gate. The displayed FA speed combines active
+sample means by active sample count, so it remains an arithmetic sample mean.
+For material sources without an order-aware allocator, each window is passed to
+the configured cumulative or channel material service, using its normal
+baseline and counter-delta rules. Snapshot timing around midpoint boundaries
+can leave small gaps or shifts in attribution, so those segment totals need only
+approximately reconcile with the whole-shift total. Where an order-aware
+allocator is configured, K7 being the current example, FA material instead sums
+the allocated items from the **full-shift** material result by production order.
+The compact FA value thus uses the same ENLYZE measurement and ERP fiber
+allocation as the detailed material table. The shift report reuses its already
+allocated summary; the order detail obtains and allocates a full-shift summary.
+Speed always uses the approximate FA windows.
+
+The shift report's production table uses these same FA windows and KPI results
+for each listed order. Its speed describes the FA's estimated period; material
+uses the configured segment or order-aware source rule. The shift-wide speed and
+material sections remain useful reconciliation totals. In HTML, the FA number
+links directly to `/order?production_order=...` for the order's full shift list.
+The PDF prints the FA number as text.
+
+Disabled speed or material capability produces `—` and makes no query to that
+source. No active speed samples also produce `—`; an observed material total of
+zero remains `0,00 kg`. Known ENLYZE/PostgreSQL/ERP source-read failures degrade
+that optional KPI to unavailable. Unexpected configuration or calculation errors
+still fail the order page rather than being hidden.
diff --git a/src/production_analytics/calculations/shift_production.py b/src/production_analytics/calculations/shift_production.py
index 1bf4f22..ee56b67 100644
--- a/src/production_analytics/calculations/shift_production.py
+++ b/src/production_analytics/calculations/shift_production.py
@@ -18,6 +18,8 @@ class ShiftProductionItem:
roll_count: int
first_confirmation_at: datetime
last_confirmation_at: datetime
+ average_speed_m_min: float | None = None
+ material_consumption_kg: float | None = None
@dataclass(frozen=True, slots=True)
diff --git a/src/production_analytics/presentation/production_order_html.py b/src/production_analytics/presentation/production_order_html.py
index bb7680b..ebe029d 100644
--- a/src/production_analytics/presentation/production_order_html.py
+++ b/src/production_analytics/presentation/production_order_html.py
@@ -7,6 +7,7 @@ from production_analytics.presentation.mode_navigation import (
MODE_NAV_CSS,
render_mode_navigation,
)
+from production_analytics.presentation.shift_report import _number
from production_analytics.service.production_order import (
ProductionOrderDetail,
ProductionOrderSearchResult,
@@ -83,7 +84,8 @@ def render_detail(*, detail: ProductionOrderDetail | None,
return _page("Fertigungsauftrag", "Fertigungsauftrag nicht gefunden.
")
body = f"Fertigungsauftrag {escape(detail.production_order)}
"
body += 'Zeiten sind ERP-Rückmeldezeitpunkte, keine durchgehenden '
- body += 'Maschinenlaufzeiten. Mengen sind positive Rückmeldemengen in m².
'
+ body += 'Maschinenlaufzeiten. Mengen sind positive Rückmeldemengen in m². '
+ body += 'Geschwindigkeit und Material beziehen sich auf angenäherte FA-Zeitfenster.
'
body += 'Artikel und Arbeitsplätze