diff --git a/reporter.py b/reporter.py index ef9acfb..1e48704 100644 --- a/reporter.py +++ b/reporter.py @@ -46,6 +46,7 @@ class LocalStateReporter: metadata_drift_count=0, last_drift_fingerprint="", last_heal_result=None, + last_heal_timestamp=None, checks_increment=0, heal_result=None, ): @@ -58,8 +59,15 @@ class LocalStateReporter: if last_heal_result is not None else previous.get("last_heal_result") ) + effective_last_heal_timestamp = ( + last_heal_timestamp + if last_heal_timestamp is not None + else previous.get("last_heal_timestamp") + ) if heal_result is not None: effective_heal_result = heal_result + if effective_last_heal_timestamp is None: + effective_last_heal_timestamp = datetime.now().isoformat() checks_total = max(0, self._get_int(previous, "checks_total", 0)) checks_total += max(0, int(checks_increment)) @@ -84,6 +92,7 @@ class LocalStateReporter: "metadata_drift_count": metadata_drift_count, "last_drift_fingerprint": last_drift_fingerprint, "last_heal_result": effective_heal_result, + "last_heal_timestamp": effective_last_heal_timestamp, "uptime_seconds": uptime_seconds, "checks_total": checks_total, "heals_total": heals_total, diff --git a/tests/test_reporter.py b/tests/test_reporter.py index 385cd09..b7a7662 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -2,6 +2,7 @@ import json import os import tempfile import unittest +from datetime import datetime from reporter import LocalStateReporter @@ -78,8 +79,26 @@ class ReporterStateTests(unittest.TestCase): self.assertEqual(2, payload["heals_total"]) self.assertEqual(1, payload["heal_failures_total"]) self.assertEqual("success", payload["last_heal_result"]) + self.assertIn("last_heal_timestamp", payload) + datetime.fromisoformat(payload["last_heal_timestamp"]) self.assertGreaterEqual(payload["uptime_seconds"], 0) + def test_update_status_preserves_last_heal_timestamp_when_no_new_heal(self): + 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.update_status(False, 1, heal_result="failed") + with open(state_file, "r") as f: + first_payload = json.load(f) + first_ts = first_payload["last_heal_timestamp"] + + reporter.update_status(True, 1, checks_increment=1) + with open(state_file, "r") as f: + second_payload = json.load(f) + + self.assertEqual(first_ts, second_payload["last_heal_timestamp"]) + if __name__ == "__main__": unittest.main()