Extend shift reporting and downtime analytics

This commit is contained in:
admin committed 2026-10-20 14:16:58 +02:00
1 parent 41a5ae7a63
commit 6e8eb6f50f
74 files changed
+5397 -238

No files matched your search

+152 -4
View File
@@ -1,3 +1,4 @@
import sqlite3
import traceback
from dataclasses import FrozenInstanceError, replace
from datetime import UTC, datetime, timedelta
@@ -83,6 +84,103 @@ def test_open_event_clipped_to_shift_end():
assert summary.events[0].source.source_end is None
def test_raw_closed_event_without_attribution():
source = event(start=START + timedelta(hours=1), end=START + timedelta(hours=2))
summary = summarize([source])
assert source.attributed_start is source.attributed_end is None
assert summary.events[0].start == source.source_start
assert summary.events[0].end == source.source_end
assert summary.total_downtime_seconds == 3600
def test_attributed_end_closes_raw_open_event():
attributed_end = START + timedelta(hours=2)
source = event(end=None, attributed_end=attributed_end)
summary = summarize([source])
assert summary.events[0].start == START
assert summary.events[0].end is attributed_end
assert summary.events[0].source.source_end is None
assert summary.total_downtime_seconds == 2 * 3600
def test_attributed_start_with_no_end_remains_open():
summary = summarize([event(end=None, attributed_start=START + timedelta(hours=7))])
assert summary.total_downtime_seconds == 3600
assert summary.events[0].end is END
def test_attribution_overrides_conflicting_raw_interval():
attributed_start = START + timedelta(hours=1)
attributed_end = START + timedelta(hours=2)
source = event(
start=END,
end=END + timedelta(hours=2),
attributed_start=attributed_start,
attributed_end=attributed_end,
)
summary = summarize([source])
assert summary.events[0].start is attributed_start
assert summary.events[0].end is attributed_end
assert summary.total_downtime_seconds == 3600
assert source.source_start == END
assert source.source_end == END + timedelta(hours=2)
def test_attributed_interval_clips_both_shift_boundaries():
source = event(
start=END,
end=END,
attributed_start=START - timedelta(hours=1),
attributed_end=END + timedelta(hours=1),
)
summary = summarize([source])
assert summary.events[0].start is START
assert summary.events[0].end is END
assert summary.total_downtime_seconds == 8 * 3600
def test_production_reconciled_no_data_event_does_not_overlap_later_shift():
source_start = datetime(2026, 10, 12, 8, 56, 30, tzinfo=UTC)
source = event(
start=source_start,
end=None,
attributed_start=source_start,
attributed_end=datetime(2026, 10, 12, 12, 36, 2, tzinfo=UTC),
)
summary = summarize(
[source],
start=datetime(2026, 10, 13, 12, tzinfo=UTC),
end=datetime(2026, 10, 13, 20, tzinfo=UTC),
)
assert summary.events == summary.reasons == ()
assert summary.total_downtime_seconds == 0
assert summary.non_downtime_seconds == 8 * 3600
def test_attributed_intervals_preserve_union_and_reason_aggregation():
sources = [
event(
identifier,
end=None,
reason=reason,
attributed_start=START + timedelta(hours=lower),
attributed_end=START + timedelta(hours=upper),
)
for identifier, reason, lower, upper in [
("a", "A", 0, 2),
("b", "A", 1, 3),
("c", "B", 2, 4),
]
]
summary = summarize(sources)
assert summary.total_downtime_seconds == 4 * 3600
assert summary.non_downtime_seconds == 4 * 3600
assert [
(reason.reason_name, reason.duration_seconds, reason.event_count)
for reason in summary.reasons
] == [("A", 3 * 3600, 2), ("B", 2 * 3600, 1)]
def test_overlap_nested_duplicates_and_adjacent_intervals_are_not_double_counted():
events = [
event("a", end=START + timedelta(hours=2)),
@@ -207,6 +305,8 @@ def db(monkeypatch):
None,
"UNKNOWN",
None,
START.astimezone(UTC),
END.astimezone(UTC),
)
]
repository = PostgresDowntimeRepository(PostgresSettings("localhost", 5432, "db", "user", "pw"))
@@ -218,10 +318,14 @@ def test_repository_sql_parameters_mapping_and_lifecycle(db):
values = repository.get_events(machine_id="machine", start=START, end=END)
sql, params = connection.execute.call_args.args
assert params == ("machine", END, START)
assert "WHERE machine_id = %s AND source_start < %s" in sql
assert "source_end IS NULL OR source_end > %s" in sql
assert "ORDER BY source_start, external_id" in sql
assert "attributed_" not in sql and "production_order" not in sql
assert "WHERE machine_id = %s AND COALESCE(attributed_start, source_start) < %s" in sql
assert "COALESCE(attributed_end, source_end) IS NULL" in sql
assert "OR COALESCE(attributed_end, source_end) > %s" in sql
assert "ORDER BY COALESCE(attributed_start, source_start), external_id" in sql
assert "category, comment, attributed_start, attributed_end" in sql
assert "production_order" not in sql
assert values[0].attributed_start == START.astimezone(UTC)
assert values[0].attributed_end == END.astimezone(UTC)
assert len(values) == 1 and values[0].source_end is None
assert values[0].source_start.tzinfo is UTC
connect.assert_called_once_with(
@@ -245,6 +349,46 @@ def test_machine_id_is_parameterized(db):
assert params[0] == machine_id
def test_repository_excludes_production_reconciled_event_from_later_shift(db):
repository, _, connection = db
# Execute the repository's overlap predicate, translating only driver placeholders.
with sqlite3.connect(":memory:") as database:
database.execute(
"""CREATE TABLE production_downtime_events (
external_id, machine_id, source_start, source_end,
reason_id, reason_name, reason_description, reason_group,
category, comment, attributed_start, attributed_end)"""
)
database.execute(
"""INSERT INTO production_downtime_events
(external_id, machine_id, source_start, attributed_start, attributed_end)
VALUES (?, ?, ?, ?, ?)""",
(
"reconciled-no-data",
"machine",
"2026-10-12T08:56:30+00:00",
"2026-10-12T08:56:30+00:00",
"2026-10-12T12:36:02+00:00",
),
)
def execute(sql, params):
return database.execute(
sql.replace("%s", "?"),
tuple(
value.isoformat() if isinstance(value, datetime) else value
for value in params
),
)
connection.execute.side_effect = execute
assert repository.get_events(
machine_id="machine",
start=datetime(2026, 10, 13, 12, tzinfo=UTC),
end=datetime(2026, 10, 13, 20, tzinfo=UTC),
) == []
@pytest.mark.parametrize(
"changes",
[
@@ -291,6 +435,10 @@ def test_repository_errors_are_safe(db, stage):
[
{"source_start": START.replace(tzinfo=None)},
{"source_end": END.replace(tzinfo=None)},
{"attributed_start": START.replace(tzinfo=None)},
{"attributed_end": END.replace(tzinfo=None)},
{"attributed_start": "invalid"},
{"attributed_end": "invalid"},
{"source_end": START - timedelta(seconds=1)},
{"reason_name": 123},
],