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
@@ -1,3 +1,4 @@
|
||||
from copy import deepcopy
|
||||
from dataclasses import FrozenInstanceError, replace
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
@@ -5,8 +6,10 @@ from unittest.mock import Mock
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from production_analytics.calculations.shift_speed import (
|
||||
NumericDwellGate,
|
||||
ShiftSpeedConfig,
|
||||
ShiftSpeedSample,
|
||||
aggregate_shift_speed,
|
||||
@@ -126,6 +129,101 @@ def test_zero_gate_excludes_zero_and_negative():
|
||||
assert summary.average_line_speed_m_min == 1
|
||||
|
||||
|
||||
def test_boolean_gate_uses_status_instead_of_numeric_threshold():
|
||||
config = replace(CONFIG, production_gate_m_min=None,
|
||||
running_signal_ref="status", running_when=False)
|
||||
samples = [ShiftSpeedSample(START + timedelta(seconds=i), speed, status)
|
||||
for i, (speed, status) in enumerate([
|
||||
(0, False), (0.2, False), (8, True), (4, False),
|
||||
(6, None), (7, 0), (9, "false"), (None, False),
|
||||
])]
|
||||
summary = aggregate_shift_speed(samples, config=config, start=START, end=END)
|
||||
assert (summary.sample_count, summary.active_sample_count,
|
||||
summary.inactive_sample_count, summary.invalid_sample_count) == (4, 3, 1, 4)
|
||||
assert summary.average_line_speed_m_min == pytest.approx(4.2 / 3)
|
||||
assert summary.minimum_active_speed_m_min == 0
|
||||
assert summary.maximum_active_speed_m_min == 4
|
||||
|
||||
|
||||
def test_boolean_gate_defaults_to_true_and_unweighted_mean():
|
||||
config = replace(CONFIG, production_gate_m_min=None, running_signal_ref="status")
|
||||
samples = [ShiftSpeedSample(START, 1, True),
|
||||
ShiftSpeedSample(END - timedelta(seconds=1), 9, True),
|
||||
ShiftSpeedSample(START, 100, False)]
|
||||
summary = aggregate_shift_speed(samples, config=config, start=START, end=END)
|
||||
assert summary.average_line_speed_m_min == 5
|
||||
assert summary.inactive_sample_count == 1
|
||||
|
||||
|
||||
DWELL = NumericDwellGate("gate", 50, "below", 150)
|
||||
|
||||
|
||||
def dwell_summary(rows, gate=DWELL):
|
||||
config = replace(CONFIG, production_gate_m_min=None, numeric_dwell_gate=gate)
|
||||
samples = [ShiftSpeedSample(START + timedelta(seconds=second), speed,
|
||||
gate_value=value)
|
||||
for second, speed, value in rows]
|
||||
return aggregate_shift_speed(samples, config=config, start=START, end=END)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("duration,expected_inactive", [(149, 0), (150, 2), (230, 2)])
|
||||
def test_dwell_duration_includes_first_crossing(duration, expected_inactive):
|
||||
summary = dwell_summary([(-1, 1, 50), (0, 2, 49), (duration - 1, 4, 49),
|
||||
(duration, 8, 50)])
|
||||
assert summary.inactive_sample_count == expected_inactive
|
||||
assert summary.active_sample_count == 3 - expected_inactive
|
||||
assert summary.average_line_speed_m_min == (8 if expected_inactive else 14 / 3)
|
||||
|
||||
|
||||
def test_separate_short_excursions_do_not_merge_and_equality_is_active():
|
||||
summary = dwell_summary([(-10, 1, 50), (0, 2, 49), (80, 3, 50),
|
||||
(100, 4, 49), (200, 5, 50)])
|
||||
assert summary.active_sample_count == 4
|
||||
assert summary.inactive_sample_count == 0
|
||||
assert summary.average_line_speed_m_min == 3.5
|
||||
|
||||
|
||||
def test_irregular_timestamps_use_elapsed_time_and_arithmetic_mean():
|
||||
summary = dwell_summary([(-5, 1, 50), (0, 2, 49), (7, 4, 49),
|
||||
(149, 6, 49), (150, 8, 50), (200, 10, 50)])
|
||||
assert summary.inactive_sample_count == 3
|
||||
assert summary.average_line_speed_m_min == 9
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad", [None, float("nan"), float("inf"), "49", True])
|
||||
def test_invalid_dwell_gate_breaks_interval_and_excludes_speed(bad):
|
||||
summary = dwell_summary([(-1, 1, 50), (0, 2, 49), (50, 3, bad),
|
||||
(100, 4, 49), (200, 5, 50)])
|
||||
assert summary.active_sample_count == 1
|
||||
assert summary.invalid_sample_count == 3
|
||||
assert summary.average_line_speed_m_min == 5
|
||||
|
||||
|
||||
def test_open_short_dwell_interval_is_unknown_at_window_boundary():
|
||||
summary = dwell_summary([(0, 2, 49), (10, 4, 49)])
|
||||
assert summary.sample_count == 0
|
||||
assert summary.invalid_sample_count == 2
|
||||
|
||||
|
||||
def test_dwell_uses_both_sides_of_window_for_classification():
|
||||
summary = dwell_summary([(-150, 1, 49), (0, 2, 49), (30, 4, 50),
|
||||
(END.timestamp() - START.timestamp() - 1, 6, 49),
|
||||
(END.timestamp() - START.timestamp() + 149, 8, 50)])
|
||||
assert summary.inactive_sample_count == 2
|
||||
assert summary.active_sample_count == 1
|
||||
assert summary.average_line_speed_m_min == 4
|
||||
|
||||
|
||||
@pytest.mark.parametrize("changes", [
|
||||
{"running_signal_ref": ""}, {"running_signal_ref": "speed"},
|
||||
{"running_signal_ref": 123}, {"running_when": 0},
|
||||
{"production_gate_m_min": None},
|
||||
])
|
||||
def test_invalid_boolean_configuration_rejected(changes):
|
||||
with pytest.raises(ValueError):
|
||||
replace(CONFIG, **changes)
|
||||
|
||||
|
||||
def test_large_finite_speeds_do_not_overflow_mean():
|
||||
assert summarize([1e308, 1e308]).average_line_speed_m_min == 1e308
|
||||
|
||||
@@ -167,6 +265,53 @@ def test_gateway_pagination_column_order_and_unavailable_samples():
|
||||
assert client.post_json.call_args_list[1].args == ("/v2/timeseries", {**body, "cursor": "next"})
|
||||
|
||||
|
||||
def test_gateway_two_variable_response_and_single_request():
|
||||
client = Mock()
|
||||
client.post_json.return_value = page([
|
||||
[False, 0.2, START.isoformat()], [True, 10, START.isoformat()],
|
||||
[None, 20, START.isoformat()], [START.isoformat()],
|
||||
], columns=["status", "speed", "time"])
|
||||
config = replace(CONFIG, production_gate_m_min=None,
|
||||
running_signal_ref="status", running_when=False)
|
||||
summary = ShiftSpeedService(EnlyzeApiGateway(client)).summarize(
|
||||
config=config, start=START, end=END)
|
||||
assert summary.average_line_speed_m_min == 0.2
|
||||
assert (summary.active_sample_count, summary.inactive_sample_count,
|
||||
summary.invalid_sample_count) == (1, 1, 2)
|
||||
assert client.post_json.call_count == 1
|
||||
assert client.post_json.call_args.args[1]["variables"] == [
|
||||
{"uuid": "speed"}, {"uuid": "status"}]
|
||||
|
||||
|
||||
def test_gateway_requires_status_column_when_configured():
|
||||
client = Mock()
|
||||
client.post_json.return_value = page([], columns=["time", "speed"])
|
||||
with pytest.raises(ValueError, match="required columns"):
|
||||
EnlyzeApiGateway(client).get_speed_samples(
|
||||
machine_id="machine", speed_signal_ref="speed", running_signal_ref="status",
|
||||
start=START, end=END)
|
||||
|
||||
|
||||
def test_dwell_gateway_requests_both_signals_with_expanded_window():
|
||||
client = Mock()
|
||||
client.post_json.return_value = page([
|
||||
[50, START.isoformat(), 1],
|
||||
[49, (START + timedelta(seconds=10)).isoformat(), 2],
|
||||
[49, (START + timedelta(seconds=160)).isoformat(), 4],
|
||||
[50, (START + timedelta(seconds=170)).isoformat(), 8],
|
||||
], columns=["gate", "time", "speed"])
|
||||
config = replace(CONFIG, production_gate_m_min=None, numeric_dwell_gate=DWELL)
|
||||
summary = ShiftSpeedService(EnlyzeApiGateway(client)).summarize(
|
||||
config=config, start=START, end=END)
|
||||
assert summary.average_line_speed_m_min == 4.5
|
||||
assert summary.inactive_sample_count == 2
|
||||
assert client.post_json.call_count == 1
|
||||
body = client.post_json.call_args.args[1]
|
||||
assert body["variables"] == [{"uuid": "speed"}, {"uuid": "gate"}]
|
||||
assert body["start"] == (START - timedelta(seconds=150)).astimezone(UTC).isoformat()
|
||||
assert body["end"] == (END + timedelta(seconds=150)).astimezone(UTC).isoformat()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("body", [
|
||||
{"data": {"columns": ["time"], "records": []}},
|
||||
{"data": {"columns": ["time", "speed", "speed"], "records": []}},
|
||||
@@ -206,6 +351,62 @@ def test_validated_machine_configuration(name, machine, signal, gate):
|
||||
assert config.signal
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name,speed,gate,threshold", [
|
||||
("Gitter 1", "33abccf8-8dee-4b40-b0ce-63c3c0fd2cd9",
|
||||
"620511e3-a46b-47c4-8514-f2d4bde29e2b", 50),
|
||||
("Gitter 2", "d3acefd8-1013-4b59-a235-fa265fd85fae",
|
||||
"1ae8fb5d-1d25-414a-9181-d35b6cdde507", 27000),
|
||||
("Gitter 3", "88641764-6fd1-4733-a054-80fa43a62470",
|
||||
"f78bd11f-b2a8-4454-86a0-c8de576799c2", 31000),
|
||||
("Gitter 4", "1d7660d4-10f4-4466-b1fd-076f9fd504fe",
|
||||
"5685fb13-861d-4e95-8a98-e39d4fc3e059", 2),
|
||||
])
|
||||
def test_dwell_machine_configuration(name, speed, gate, threshold):
|
||||
machine = load_shift_report_machines(CONFIG_PATH)[name]
|
||||
assert machine.capabilities.speed and not machine.capabilities.material
|
||||
assert machine.speed.speed_signal_ref == speed
|
||||
assert machine.speed.numeric_dwell_gate.signal_ref == gate
|
||||
assert machine.speed.numeric_dwell_gate.threshold == threshold
|
||||
assert machine.speed.numeric_dwell_gate.comparison == "below"
|
||||
assert machine.speed.numeric_dwell_gate.minimum_duration_seconds == 150
|
||||
assert machine.speed.running_signal_ref is None
|
||||
assert machine.speed.production_gate_m_min is None
|
||||
|
||||
|
||||
def test_k5_boolean_machine_configuration():
|
||||
config = load_shift_report_machines(CONFIG_PATH)["K5"].speed
|
||||
assert config.speed_signal_ref == "c3ae3002-b75c-4d7c-bb33-f10265fcf428"
|
||||
assert config.running_signal_ref == "507b1225-2992-4dd1-8412-34969466722f"
|
||||
assert config.running_when is False
|
||||
assert config.numeric_dwell_gate is None
|
||||
summary = aggregate_shift_speed([
|
||||
ShiftSpeedSample(START, 1, False),
|
||||
ShiftSpeedSample(START + timedelta(seconds=1), 9, True),
|
||||
ShiftSpeedSample(START + timedelta(seconds=2), 10, None),
|
||||
], config=config, start=START, end=END)
|
||||
assert summary.average_line_speed_m_min == 1
|
||||
assert (summary.active_sample_count, summary.inactive_sample_count,
|
||||
summary.invalid_sample_count) == (1, 1, 1)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", [
|
||||
{"running_signal_ref": "status"}, {"running_when": False},
|
||||
{"production_gate_m_min": 0.5}, {"unexpected": True},
|
||||
{"numeric_dwell_gate": {"signal_ref": "gate"}},
|
||||
{"numeric_dwell_gate": {"signal_ref": "gate", "threshold": 50,
|
||||
"comparison": "below", "minimum_duration_seconds": 0}},
|
||||
{"numeric_dwell_gate": {"signal_ref": "gate", "threshold": float("nan"),
|
||||
"comparison": "below", "minimum_duration_seconds": 150}},
|
||||
])
|
||||
def test_dwell_yaml_validation(tmp_path, change):
|
||||
document = deepcopy(yaml.safe_load(CONFIG_PATH.read_text()))
|
||||
document["machines"]["Gitter 2"]["speed"].update(change)
|
||||
path = tmp_path / "machines.yaml"
|
||||
path.write_text(yaml.safe_dump(document))
|
||||
with pytest.raises(ValueError):
|
||||
load_shift_report_machines(path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("document", ["machines: {}", "other: {}", "machines: []",
|
||||
"machines:\n B1: {}", "machines:\n B1: null"])
|
||||
def test_invalid_configuration(tmp_path, document):
|
||||
|
||||
Reference in new issue
Block a user