Add dashboard-triggered replay jobs and detector deltas
Closes the last ROADMAP §1 item. The replay/replay-graylog CLI backend already existed and was already safe (a temporary, auto-cleaned SQLite baseline, never touching live state) — the gap was purely that a human had to SSH in and run it by hand. Extracts the core logic from cli.py into run_local_replay()/run_graylog_replay() in replay.py, reused by both the CLI (now thin wrappers) and a new dashboard panel (Diagnostics -> Replay & Detector Deltas). Runs as a background job via an in-memory job store, since the dashboard already runs ThreadingHTTPServer and this session directly observed live MCP polling take anywhere from 9 minutes to over an hour — a synchronous request/response was never viable. Deliberately serialized to one job at a time given this box's GPU/CPU constraints. Also removed a redundant "Detector Precision" panel that duplicated (with a weaker, current-poll-cycle-only computation) what the new Detector Health panel already does correctly with real historical data. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
import inspect
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -255,6 +256,85 @@ class DashboardTests(unittest.TestCase):
|
||||
self.assertIn('name="auto_apply_profile_suggestions"', HTML)
|
||||
self.assertIn("field_confidence_threshold: config.field_confidence_threshold ?? 60", HTML)
|
||||
|
||||
def test_dashboard_exposes_replay_controls(self):
|
||||
self.assertIn("/api/replay/start", HTML)
|
||||
self.assertIn("/api/replay/status", HTML)
|
||||
self.assertIn("Replay", HTML)
|
||||
|
||||
|
||||
class ReplayJobTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
dashboard._replay_jobs.clear()
|
||||
|
||||
def tearDown(self):
|
||||
dashboard._replay_jobs.clear()
|
||||
|
||||
def test_run_replay_job_records_success(self):
|
||||
with patch("fgai.dashboard.run_local_replay", return_value={"events": 5}):
|
||||
dashboard._replay_jobs["job-1"] = {"status": "running", "started_at": 100}
|
||||
dashboard._run_replay_job("job-1", {"mode": "local"})
|
||||
|
||||
job = dashboard._replay_jobs["job-1"]
|
||||
self.assertEqual(job["status"], "done")
|
||||
self.assertEqual(job["result"], {"events": 5})
|
||||
self.assertIn("finished_at", job)
|
||||
|
||||
def test_run_replay_job_records_failure_without_crashing(self):
|
||||
with patch("fgai.dashboard.run_local_replay", side_effect=RuntimeError("boom")):
|
||||
dashboard._replay_jobs["job-1"] = {"status": "running", "started_at": 100}
|
||||
dashboard._run_replay_job("job-1", {"mode": "local"})
|
||||
|
||||
job = dashboard._replay_jobs["job-1"]
|
||||
self.assertEqual(job["status"], "error")
|
||||
self.assertEqual(job["error"], "boom")
|
||||
|
||||
def test_run_replay_job_dispatches_graylog_mode(self):
|
||||
with patch("fgai.dashboard.run_graylog_replay", return_value={"events": 9}) as mocked:
|
||||
dashboard._replay_jobs["job-1"] = {"status": "running", "started_at": 100}
|
||||
dashboard._run_replay_job("job-1", {"mode": "graylog", "stream_ids": ["a", "b"], "range_seconds": 600})
|
||||
|
||||
mocked.assert_called_once()
|
||||
self.assertEqual(dashboard._replay_jobs["job-1"]["result"], {"events": 9})
|
||||
|
||||
def test_start_replay_job_rejects_a_second_concurrent_job(self):
|
||||
dashboard._replay_jobs["existing"] = {"status": "running", "started_at": 100}
|
||||
|
||||
result = dashboard._start_replay_job({"mode": "local"})
|
||||
|
||||
self.assertIn("error", result)
|
||||
self.assertEqual(len(dashboard._replay_jobs), 1)
|
||||
|
||||
def test_start_replay_job_starts_a_thread_when_idle(self):
|
||||
with patch("fgai.dashboard.run_local_replay", return_value={"events": 1}):
|
||||
result = dashboard._start_replay_job({"mode": "local"})
|
||||
self.assertIn("job_id", result)
|
||||
job_id = result["job_id"]
|
||||
for _ in range(50):
|
||||
if dashboard._replay_jobs.get(job_id, {}).get("status") != "running":
|
||||
break
|
||||
time.sleep(0.02)
|
||||
|
||||
self.assertEqual(dashboard._replay_jobs[job_id]["status"], "done")
|
||||
|
||||
def test_replay_job_status_returns_job_by_id(self):
|
||||
dashboard._replay_jobs["job-1"] = {"status": "done", "started_at": 100, "result": {"events": 1}}
|
||||
|
||||
status = dashboard._replay_job_status("job-1")
|
||||
|
||||
self.assertEqual(status["status"], "done")
|
||||
|
||||
def test_replay_job_status_returns_latest_job_when_no_id_given(self):
|
||||
dashboard._replay_jobs["older"] = {"status": "done", "started_at": 100}
|
||||
dashboard._replay_jobs["newer"] = {"status": "running", "started_at": 200}
|
||||
|
||||
status = dashboard._replay_job_status()
|
||||
|
||||
self.assertEqual(status["job_id"], "newer")
|
||||
|
||||
def test_replay_job_status_returns_none_when_no_jobs_exist(self):
|
||||
self.assertIsNone(dashboard._replay_job_status())
|
||||
self.assertIsNone(dashboard._replay_job_status("missing"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user