Extend shift reporting and downtime analytics
This commit is contained in:
1 parent
41a5ae7a63
commit
6e8eb6f50f
74 files changed
+5397
-238
No files matched your search
+281
-14
@@ -202,9 +202,8 @@ def test_channel_b_identity_attribution_and_no_null_carry_forward():
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert [(item.material_name, item.consumption_kg) for item in summary.items] == [
|
||||
("Material A", 30),
|
||||
("Material A", 45),
|
||||
(None, 10),
|
||||
("Material A", 15),
|
||||
("Material B", 10),
|
||||
]
|
||||
assert summary.items[1].material_mapping_status == "UNAVAILABLE"
|
||||
@@ -243,6 +242,7 @@ def test_e1_historical_null_channel_fixtures():
|
||||
for minutes, value in ((-1, 5000), (479, 5000 + delta))
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert summary.total_consumption_kg == pytest.approx(24_886.805)
|
||||
assert len(summary.items) == 14
|
||||
assert {(item.extruder, item.doser_channel): item.consumption_kg for item in summary.items} == (
|
||||
pytest.approx(
|
||||
@@ -321,14 +321,54 @@ def test_service_composes_once_and_propagates_errors(channel):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("channel", [False, True])
|
||||
def test_ambiguous_stream_timestamps_and_reappearing_runs_rejected(channel):
|
||||
def test_ambiguous_stream_timestamps_and_reappearing_runs_allowed(channel):
|
||||
sample = channel_sample if channel else machine_sample
|
||||
with pytest.raises(ValueError, match="same stream timestamp"):
|
||||
aggregate([sample(0, 100), sample(0, 100)], channel=channel)
|
||||
with pytest.raises(ValueError, match="reappeared"):
|
||||
aggregate([sample(0, 100), sample(10, 10, run="r2"), sample(20, 200)], channel=channel)
|
||||
with pytest.raises(ValueError, match="Production order"):
|
||||
aggregate([sample(0, 100), sample(10, 110, order="different")], channel=channel)
|
||||
summary = aggregate([
|
||||
sample(0, 100), sample(10, 110), sample(20, 500, run="r2"),
|
||||
sample(30, 520, run="r2"), sample(40, 200), sample(50, 230),
|
||||
], channel=channel)
|
||||
assert summary.total_consumption_kg == 60
|
||||
assert [(item.run_id, item.consumption_kg) for item in summary.items] == [
|
||||
("r1", 10), ("r2", 20), ("r1", 30),
|
||||
]
|
||||
assert summary.items[2].baseline_missing
|
||||
summary = aggregate([sample(0, 100), sample(10, 110, order="different")], channel=channel)
|
||||
assert [(i.production_order, i.consumption_kg) for i in summary.items] == [("different", 10)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("channel", [False, True])
|
||||
def test_reappearing_run_keeps_order_attribution_within_each_segment(channel):
|
||||
sample = channel_sample if channel else machine_sample
|
||||
rows = [
|
||||
sample(0, 100, run="A", order="first"),
|
||||
sample(1, 110, run="A", order="second"),
|
||||
sample(2, 500, run="B", order="second"),
|
||||
sample(3, 520, run="B", order="third"),
|
||||
sample(4, 200, run="A", order="third"),
|
||||
sample(5, 230, run="A", order="fourth"),
|
||||
]
|
||||
summary = aggregate(rows, channel=channel)
|
||||
assert [(i.run_id, i.production_order, i.consumption_kg) for i in summary.items] == [
|
||||
("A", "second", 10), ("B", "third", 20), ("A", "fourth", 30),
|
||||
]
|
||||
|
||||
|
||||
def test_channel_identity_gap_does_not_bridge_across_reappearing_run_segment():
|
||||
rows = [
|
||||
channel_sample(0, 100, run="A", name="Known"),
|
||||
channel_sample(1, 110, run="A", name=None),
|
||||
channel_sample(2, 500, run="B", name="Other"),
|
||||
channel_sample(3, 520, run="B", name="Other"),
|
||||
channel_sample(4, 200, run="A", name=None),
|
||||
channel_sample(5, 230, run="A", name="Known"),
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert summary.total_consumption_kg == 60
|
||||
assert [(item.run_id, item.material_name, item.consumption_kg) for item in summary.items] == [
|
||||
("A", None, 10), ("B", "Other", 20), ("A", "Known", 30),
|
||||
]
|
||||
|
||||
|
||||
def test_machine_and_calculation_scoping():
|
||||
@@ -375,17 +415,22 @@ def test_new_run_exactly_at_shift_start_is_its_own_baseline(channel):
|
||||
def test_invalid_counters_do_not_hide_run_boundaries(channel):
|
||||
sample = channel_sample if channel else machine_sample
|
||||
summary = aggregate(
|
||||
[sample(-2, 1000), sample(-1, None, run="r2"),
|
||||
sample(10, 50, run="r2"), sample(20, 60, run="r2")],
|
||||
[
|
||||
sample(-2, 1000),
|
||||
sample(-1, None, run="r2"),
|
||||
sample(10, 50, run="r2"),
|
||||
sample(20, 60, run="r2"),
|
||||
],
|
||||
channel=channel,
|
||||
)
|
||||
assert summary.total_consumption_kg == 10
|
||||
assert len(summary.items) == 1
|
||||
assert summary.items[0].run_id == "r2"
|
||||
assert summary.items[0].baseline_missing
|
||||
with pytest.raises(ValueError, match="reappeared"):
|
||||
aggregate([sample(0, 100), sample(10, None, run="r2"), sample(20, 120)],
|
||||
channel=channel)
|
||||
summary = aggregate(
|
||||
[sample(0, 100), sample(10, None, run="r2"), sample(20, 120)], channel=channel
|
||||
)
|
||||
assert summary.total_consumption_kg == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("channel", [False, True])
|
||||
@@ -401,7 +446,229 @@ def test_spring_dst_window_uses_elapsed_instants(channel):
|
||||
start = datetime(2026, 3, 29, 1, 30, tzinfo=berlin)
|
||||
end = datetime(2026, 3, 29, 3, 30, tzinfo=berlin)
|
||||
sample = channel_sample if channel else machine_sample
|
||||
rows = [replace(sample(value=value), timestamp=timestamp) for timestamp, value in
|
||||
[(start, 100), (datetime(2026, 3, 29, 1, 20, tzinfo=UTC), 110), (end, 999)]]
|
||||
rows = [
|
||||
replace(sample(value=value), timestamp=timestamp)
|
||||
for timestamp, value in [
|
||||
(start, 100),
|
||||
(datetime(2026, 3, 29, 1, 20, tzinfo=UTC), 110),
|
||||
(end, 999),
|
||||
]
|
||||
]
|
||||
assert (end.astimezone(UTC) - start.astimezone(UTC)).total_seconds() == 3600
|
||||
assert aggregate(rows, channel=channel, start=start, end=end).total_consumption_kg == 10
|
||||
|
||||
|
||||
@pytest.mark.parametrize("middle", [None, "Material B"])
|
||||
@pytest.mark.parametrize("missing", [False, True])
|
||||
def test_repeated_channel_identity_consolidates_after_interval_attribution(middle, missing):
|
||||
offset = 1 if missing else 0
|
||||
rows = [
|
||||
channel_sample(offset, 100, name="Material A"),
|
||||
channel_sample(10, 110, name="Material A"),
|
||||
channel_sample(20, 115, name=middle),
|
||||
channel_sample(30, 135, name="Material A", quality=False),
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert len(summary.items) == 2
|
||||
a, other = summary.items
|
||||
assert (a.material_name, a.consumption_kg) == ("Material A", 30)
|
||||
assert (other.material_name, other.consumption_kg) == (middle, 5)
|
||||
assert a.baseline_timestamp == rows[0].timestamp
|
||||
assert a.end_timestamp == rows[-1].timestamp
|
||||
assert not a.percentage_sum_valid
|
||||
assert other.percentage_sum_valid
|
||||
assert all(item.baseline_missing == missing for item in summary.items)
|
||||
if middle is None:
|
||||
assert other.material_mapping_status == "UNAVAILABLE"
|
||||
assert summary.total_consumption_kg == 35
|
||||
assert summary == aggregate(reversed(rows), channel=True)
|
||||
|
||||
|
||||
def test_hundreds_of_alternating_channel_identities_consolidate():
|
||||
rows = [channel_sample(0, 100)] + [
|
||||
channel_sample(index, 100 + index, name="Material A" if index % 2 else None)
|
||||
for index in range(1, 401)
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert [(item.material_name, item.consumption_kg) for item in summary.items] == [
|
||||
("Material A", 399),
|
||||
(None, 1),
|
||||
]
|
||||
assert summary.total_consumption_kg == 400
|
||||
assert summary == aggregate(reversed(rows), channel=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("context", ["channel", "extruder", "run", "order", "calculation"])
|
||||
def test_channel_consolidation_keeps_unrelated_streams_and_runs_separate(context):
|
||||
rows = [channel_sample(0, 100), channel_sample(10, 110)]
|
||||
changes = {
|
||||
"channel": {"doser_channel": "C2"},
|
||||
"extruder": {"extruder": "Ex2"},
|
||||
"run": {"run_id": "r2"},
|
||||
"order": {"run_id": "r2", "production_order": "other"},
|
||||
"calculation": {"calculation_id": "calc2"},
|
||||
}[context]
|
||||
rows += [
|
||||
replace(channel_sample(20, 500), **changes),
|
||||
replace(channel_sample(30, 520), **changes),
|
||||
]
|
||||
summary = aggregate(rows, channel=True, calculation_ids=("calc", "calc2"))
|
||||
assert len(summary.items) == 2
|
||||
assert sorted(item.consumption_kg for item in summary.items) == [10, 20]
|
||||
assert summary.total_consumption_kg == 30
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"field,value", [("material_number", "123"), ("material_mapping_status", "OTHER")]
|
||||
)
|
||||
def test_channel_consolidation_preserves_distinct_identity_metadata(field, value):
|
||||
rows = [
|
||||
channel_sample(0, 100),
|
||||
channel_sample(10, 110),
|
||||
replace(channel_sample(20, 120), **{field: value}),
|
||||
channel_sample(30, 130),
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert len(summary.items) == 2
|
||||
assert sorted(item.consumption_kg for item in summary.items) == [10, 20]
|
||||
assert summary.total_consumption_kg == 30
|
||||
|
||||
|
||||
@pytest.mark.parametrize("include_zero", [False, True])
|
||||
def test_consolidated_channel_zero_handling(include_zero):
|
||||
rows = [
|
||||
channel_sample(0, 100),
|
||||
channel_sample(10, 100),
|
||||
channel_sample(20, 105, name=None),
|
||||
channel_sample(30, 105),
|
||||
]
|
||||
summary = aggregate(rows, channel=True, include_zero=include_zero)
|
||||
assert summary.total_consumption_kg == 5
|
||||
assert len(summary.items) == (2 if include_zero else 1)
|
||||
if include_zero:
|
||||
assert summary.items[0].consumption_kg == 0
|
||||
assert summary.items[0].baseline_timestamp == rows[0].timestamp
|
||||
assert summary.items[0].end_timestamp == rows[-1].timestamp
|
||||
|
||||
|
||||
@pytest.mark.parametrize("minutes", [(0, 1, 2), (0, 0.5, 1, 1.5), (0, 5, 10)])
|
||||
def test_short_bounded_unknown_gap_bridges_all_deltas(minutes):
|
||||
rows = [
|
||||
channel_sample(
|
||||
minute,
|
||||
100 + index * 10,
|
||||
name=" Material A " if index in (0, len(minutes) - 1) else None,
|
||||
quality=index != 1,
|
||||
)
|
||||
for index, minute in enumerate(minutes)
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert len(summary.items) == 1
|
||||
item = summary.items[0]
|
||||
assert item.material_name == "Material A"
|
||||
assert item.consumption_kg == (len(minutes) - 1) * 10
|
||||
assert summary.total_consumption_kg == rows[-1].cumulative_consumption_kg - 100
|
||||
assert item.baseline_timestamp == rows[0].timestamp
|
||||
assert item.end_timestamp == rows[-1].timestamp
|
||||
assert not item.percentage_sum_valid
|
||||
assert not item.baseline_missing
|
||||
assert summary == aggregate(reversed(rows), channel=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"names,minutes",
|
||||
[
|
||||
(("Material A", None, "Material B"), (0, 1, 2)),
|
||||
(("Material A", None, None), (0, 1, 2)),
|
||||
((None, None, "Material A"), (0, 1, 2)),
|
||||
(("Material A", None, "Material A"), (0, 1, 25)),
|
||||
(("Material A", None, "Material A"), (0, 5, 10.01)),
|
||||
],
|
||||
)
|
||||
def test_ambiguous_unbounded_or_long_identity_gaps_stay_unknown(names, minutes):
|
||||
rows = [
|
||||
channel_sample(minute, 100 + index * 10, name=name)
|
||||
for index, (minute, name) in enumerate(zip(minutes, names))
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
unknown = next(item for item in summary.items if item.material_name is None)
|
||||
assert unknown.consumption_kg == (20 if names[-1] is None else 10)
|
||||
assert unknown.material_mapping_status == "UNAVAILABLE"
|
||||
assert summary.total_consumption_kg == 20
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"field,value", [("material_number", "M2"), ("material_mapping_status", "OTHER")]
|
||||
)
|
||||
def test_bridging_requires_exact_normalized_identity(field, value):
|
||||
rows = [
|
||||
channel_sample(0, 100, number="M1"),
|
||||
channel_sample(0.5, 105, number="M1"),
|
||||
channel_sample(1, 115, name=None),
|
||||
replace(channel_sample(2, 120, number="M1"), **{field: value}),
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert len(summary.items) == 3
|
||||
assert summary.items[1].material_name is None
|
||||
assert summary.items[1].consumption_kg == 10
|
||||
assert summary.total_consumption_kg == 20
|
||||
|
||||
|
||||
@pytest.mark.parametrize("boundary", ["run", "order", "channel", "extruder"])
|
||||
def test_bridging_never_crosses_context_boundaries(boundary):
|
||||
changes = {
|
||||
"run": {"run": "r2"},
|
||||
"order": {"run": "r2", "order": "new-order"},
|
||||
"channel": {"channel": "C2"},
|
||||
"extruder": {"extruder": "Ex2"},
|
||||
}[boundary]
|
||||
rows = [
|
||||
channel_sample(0, 100),
|
||||
channel_sample(1, 110, name=None),
|
||||
channel_sample(2, 500, **changes),
|
||||
channel_sample(3, 520, **changes),
|
||||
]
|
||||
summary = aggregate(rows, channel=True)
|
||||
unknown = next(item for item in summary.items if item.material_name is None)
|
||||
assert unknown.consumption_kg == 10
|
||||
assert summary.total_consumption_kg == 30
|
||||
|
||||
|
||||
def test_short_bridge_preserves_missing_baseline_and_zero_filter():
|
||||
rows = [channel_sample(1, 100), channel_sample(2, 100, name=None), channel_sample(3, 100)]
|
||||
summary = aggregate(rows, channel=True)
|
||||
assert len(summary.items) == 1
|
||||
assert summary.items[0].baseline_missing
|
||||
assert summary.total_consumption_kg == 0
|
||||
assert aggregate(rows, channel=True, include_zero=False).items == ()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("channel", [False, True])
|
||||
def test_orders_within_physical_run_conserve_counter_and_use_end_order(channel):
|
||||
sample = channel_sample if channel else machine_sample
|
||||
rows = [sample(i, value, order=order) for i, value, order in
|
||||
[(0, 1000, "1032"), (1, 1010, "1032"),
|
||||
(2, 1025, "1037"), (3, 1040, "1037")]]
|
||||
summary = aggregate(rows, channel=channel)
|
||||
assert [(i.production_order, i.consumption_kg) for i in summary.items] == [
|
||||
("1032", 10), ("1037", 30)]
|
||||
assert {i.run_id for i in summary.items} == {"r1"}
|
||||
assert summary.total_consumption_kg == 40
|
||||
assert aggregate([replace(r, production_order="OLD") for r in rows],
|
||||
channel=channel).total_consumption_kg == 40
|
||||
rows[2] = sample(2, 1005, order="1037")
|
||||
with pytest.raises(ValueError, match="decreased/reset"):
|
||||
aggregate(rows, channel=channel)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("orders", [("X", "Y", "Y"), ("X", "X", "Y"), ("X", "Y", "X")])
|
||||
def test_channel_unknown_gap_cannot_bridge_order_boundary(orders):
|
||||
rows = [channel_sample(0, 100, order=orders[0]),
|
||||
channel_sample(1, 110, name=None, order=orders[1]),
|
||||
channel_sample(2, 120, order=orders[2])]
|
||||
summary = aggregate(rows, channel=True)
|
||||
unknown = [i for i in summary.items if i.material_name is None]
|
||||
assert len(unknown) == 1
|
||||
assert unknown[0].production_order == orders[1]
|
||||
assert unknown[0].consumption_kg == 10
|
||||
assert summary.total_consumption_kg == 20
|
||||
Reference in new issue
Block a user