Files
production-analytics/docs/historical-reconciliation.md

78 lines
4.6 KiB
Markdown

# Historical attribution reconciliation
Run from the repository with the existing ENLYZE, ERP, and PostgreSQL secret
files (or use the CLI secret-file flags):
```sh
production-analytics historical-reconcile --lookback-days 3 --dry-run
production-analytics historical-reconcile --lookback-days 3
```
The default window is the previous three **complete Europe/Berlin calendar days**,
from local midnight inclusive to today's midnight exclusive. DST days can have
23 or 25 hours. Today's live polling is excluded. Machine IDs and ERP workplaces
come from `config/shift-report-machines.yaml`; ENLYZE order name templates are
in `config/historical-order-formats.yaml` using the existing order formatter.
Apply (or reapply) `db/historical_assignment_audit.sql` before a write run; new database
installations can use `db/schema.sql`. No schema changes happen automatically.
The job reads material consumption, channel consumption, and process application
snapshots for every configured machine. It requires exactly one closed ENLYZE run
covering the sample, plus finished-goods ERP confirmations in that run whose order
set equals the run's order. It queries the full extent of overlapping closed runs,
including evidence outside the selected window. No confirmation timestamp is
interpreted as a production boundary. Missing evidence, open runs, competing runs,
and ERP/ENLYZE disagreements are unresolved and logged with record identities.
Ordinary corrections change only production_order. Measured values, timestamps,
and run IDs remain untouched. A persisted cumulative run must be entirely inside the selected data
and every affected sample must have unambiguous corroborated evidence. Different
parts of the same physical run may map to different closed ENLYZE runs/orders.
Ambiguous or partial persisted runs remain unresolved; a wider lookback can
resolve partial runs. No ERP confirmation point is used to invent a boundary.
Cumulative material intervals belong to the ending sample's production order.
Order changes never establish a new baseline: each adjacent counter delta is
counted exactly once, preserving total physical consumption. Channel material
identity gaps are bridged only when all enclosing samples share the same order.
The summary retains raw row counts and additionally reports physical runs
inspected, runs with proposed corrections, proposed/corrected order segments,
and unresolved runs/segments. Run identities are scoped by source table, machine,
and calculation; channel segment counts use separate extruder/channel streams.
Segments are contiguous samples with the same target order and correction state.
ERP KPI efficiency snapshots and downtime attribution are outside this job's
scope: they have separate derivation/reconciliation semantics. Live report
reconstruction is unchanged; no final reports are persisted or regenerated.
Updates use compare-and-set predicates and one serializable transaction per
machine, with an advisory lock for concurrent jobs. For application snapshots only,
an existing corrected-key row is retained and the stale row deleted when their
application_g_m2 values are exactly equal and the target run ID matches the
corroborated ENLYZE run. A different value or run ID fails and rolls back the
machine. Concurrent modifications also fail. The same transaction inserts audit
rows recording the action, machine, table, full record key, old/new order, source
run ID, retained target run ID for deduplication, evidence reason, and database
timestamp. Already-correct rows
produce neither updates nor audit entries. Dry-run runs the same evidence and
whole-run checks but writes nothing, including audit rows.
Any required source/database failure is reported per machine and results in a
nonzero exit status; other machines can complete. Invalid configuration fails
before processing. Inspect unresolved counts as well as the exit status: lack of
unambiguous evidence is distinct from a source outage.
Deployment examples (not installed or enabled automatically):
- `deploy/systemd/production-analytics-historical-reconcile.service`
- `deploy/systemd/production-analytics-historical-reconcile.timer`
Review deployment paths, credentials and service user for the host; apply the audit
DDL and review a dry-run before installing/enabling the timer. The timer uses
`OnCalendar=*-*-* 04:30:00 Europe/Berlin` after ERP at approximately 00:00 and DB1 at
approximately 02:30. `Persistent=true` catches a missed run after reboot. The
lookback is relative to actual execution time, so long outages require an explicit
larger lookback. No automatic retention or backfill beyond that window is implied.