feat(reporter): add rolling 24h drift/heal counters to state snapshot

This commit is contained in:
patrick committed 2026-03-06 17:38:42 -05:00
1 parent 8c7b0768b0
commit 469a30df6a
3 files changed
+121 -1

No files matched your search

+30
View File
@@ -3,6 +3,7 @@ import os
import tempfile
import unittest
from datetime import datetime
from unittest.mock import patch
from reporter import LocalStateReporter
@@ -79,6 +80,10 @@ class ReporterStateTests(unittest.TestCase):
self.assertEqual(2, payload["checks_total"])
self.assertEqual(1, payload["drift_events_total"])
self.assertEqual(2, payload["drift_heal_attempts_total"])
self.assertEqual(1, payload["drift_events_24h"])
self.assertEqual(2, payload["drift_heal_attempts_24h"])
self.assertEqual(2, payload["heals_24h"])
self.assertEqual(1, payload["heal_failures_24h"])
self.assertEqual(2, payload["heals_total"])
self.assertEqual(1, payload["heal_failures_total"])
self.assertEqual("success", payload["last_heal_result"])
@@ -100,6 +105,31 @@ class ReporterStateTests(unittest.TestCase):
self.assertEqual(2, payload["drift_events_total"])
self.assertEqual(1, payload["drift_heal_attempts_total"])
self.assertEqual(2, payload["drift_events_24h"])
self.assertEqual(1, payload["drift_heal_attempts_24h"])
@patch("reporter.time.time")
def test_update_status_rolls_24h_window(self, time_mock):
with tempfile.TemporaryDirectory() as tmp_dir:
state_file = os.path.join(tmp_dir, "state.json")
reporter = LocalStateReporter(state_file=state_file, node_name="n1")
reporter.started_at_epoch = 0
# Initial write at hour 1000.
time_mock.return_value = 1000 * 3600
reporter.update_status(False, 1, drift_events_increment=1, heal_result="failed")
# New write 25 hours later; previous hour should be pruned from 24h view.
time_mock.return_value = (1000 + 25) * 3600
reporter.update_status(True, 1, checks_increment=1)
with open(state_file, "r") as f:
payload = json.load(f)
self.assertEqual(0, payload["drift_events_24h"])
self.assertEqual(0, payload["drift_heal_attempts_24h"])
self.assertEqual(0, payload["heals_24h"])
self.assertEqual(0, payload["heal_failures_24h"])
def test_update_status_preserves_last_heal_timestamp_when_no_new_heal(self):
with tempfile.TemporaryDirectory() as tmp_dir: