Coverage for /Users/les/Projects/crackerjack/crackerjack/mcp/enhanced_progress_monitor.py: 0%
236 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 15:39 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 15:39 -0700
1import asyncio
2import time
3from contextlib import suppress
4from datetime import datetime
5from pathlib import Path
7from textual.app import App, ComposeResult
8from textual.containers import Container, Horizontal, Vertical
9from textual.reactive import reactive
10from textual.widget import Widget
11from textual.widgets import DataTable, Footer, Label, ProgressBar
13from .progress_components import (
14 JobDataCollector,
15 ServiceManager,
16 TerminalRestorer,
17)
20class MetricCard(Widget):
21 value = reactive(" --")
22 label = reactive("Metric")
23 trend = reactive("")
24 color = reactive("white")
26 def __init__(
27 self,
28 label: str,
29 value: str = " --",
30 trend: str = "",
31 color: str = "white",
32 **kwargs,
33 ) -> None:
34 super().__init__(**kwargs)
35 self.label = label
36 self.value = value
37 self.trend = trend
38 self.color = color
40 def render(self) -> str:
41 trend_icon = self.trend or ""
42 return f"[{self.color}]{self.label}[/]\n[bold {self.color}]{self.value}[/] {trend_icon}"
45class AgentActivityWidget(Widget):
46 def __init__(self, **kwargs) -> None:
47 super().__init__(**kwargs)
48 self.border_title = "🤖 AI Agent Activity"
49 self.border_title_align = "left"
51 def compose(self) -> ComposeResult:
52 with Vertical():
53 with Horizontal(id="agent-metrics"):
54 yield MetricCard(
55 "Active Agents",
56 "0",
57 color="cyan",
58 id="active-agents-metric",
59 )
60 yield MetricCard(
61 "Issues Fixed",
62 "0",
63 "↑",
64 color="green",
65 id="issues-fixed-metric",
66 )
67 yield MetricCard(
68 "Confidence",
69 "0%",
70 color="yellow",
71 id="confidence-metric",
72 )
73 yield MetricCard(
74 "Cache Hits",
75 "0",
76 color="magenta",
77 id="cache-hits-metric",
78 )
80 yield DataTable(id="agents-detail-table")
82 yield Label("⏸️ Coordinator: Idle", id="coordinator-status-bar")
84 def on_mount(self) -> None:
85 table = self.query_one("#agents-detail-table", DataTable)
86 table.add_columns(
87 ("Agent", 20),
88 ("Status", 10),
89 ("Type", 15),
90 ("Conf.", 8),
91 ("Time", 10),
92 )
93 table.zebra_stripes = True
94 table.styles.max_height = 6
96 def update_metrics(self, data: dict) -> None:
97 with suppress(Exception):
98 activity = data.get("agent_activity", {})
99 activity.get("agent_registry", {})
100 active_agents = activity.get("active_agents", [])
102 active_count = len(active_agents)
103 self.query_one("#active-agents-metric", MetricCard).value = str(
104 active_count,
105 )
107 total_fixed = sum(agent.get("issues_fixed", 0) for agent in active_agents)
108 avg_confidence = sum(
109 agent.get("confidence", 0) for agent in active_agents
110 ) / max(active_count, 1)
111 cache_hits = activity.get("cache_hits", 0)
113 self.query_one("#issues-fixed-metric", MetricCard).value = str(
114 total_fixed,
115 )
116 self.query_one(
117 "#confidence-metric",
118 MetricCard,
119 ).value = f"{avg_confidence:.0%}"
120 self.query_one("#cache-hits-metric", MetricCard).value = str(cache_hits)
122 self._update_coordinator_status(activity)
124 self._update_agent_table(active_agents)
126 def _update_coordinator_status(self, activity: dict) -> None:
127 status = activity.get("coordinator_status", "idle")
128 total_agents = activity.get("agent_registry", {}).get("total_agents", 0)
130 status_icons = {"active": "🟢", "processing": "🔄", "idle": "⏸️", "error": "🔴"}
132 icon = status_icons.get(status) or "⏸️"
133 status_bar = self.query_one("#coordinator-status-bar", Label)
134 status_bar.update(
135 f"{icon} Coordinator: {status.title()} ({total_agents} agents available)",
136 )
138 def _update_agent_table(self, agents: list) -> None:
139 table = self.query_one("#agents-detail-table", DataTable)
140 table.clear()
142 if not agents:
143 table.add_row("No active agents", "-", "-", "-", "-")
144 return
146 for agent in agents:
147 name = agent.get("agent_type", "Unknown")
148 status = agent.get("status", "idle")
149 issue_type = agent.get("issue_type", "-")
150 confidence = f"{agent.get('confidence', 0):.0%}"
151 time_elapsed = f"{agent.get('processing_time', 0):.1f}s"
153 status_emoji = {
154 "processing": "🔄",
155 "success": "✅",
156 "failed": "❌",
157 "idle": "⏸️",
158 }.get(status, "❓")
160 agent_emoji = {
161 "FormattingAgent": "🎨",
162 "SecurityAgent": "🛡️",
163 "TestCreationAgent": "🧪",
164 "TestSpecialistAgent": "🔬",
165 "RefactoringAgent": "🔧",
166 "ImportOptimizationAgent": "📦",
167 }.get(name, "🤖")
169 table.add_row(
170 f"{agent_emoji} {name}",
171 f"{status_emoji} {status}",
172 issue_type,
173 confidence,
174 time_elapsed,
175 )
178class JobProgressPanel(Widget):
179 def __init__(self, job_data: dict, **kwargs) -> None:
180 super().__init__(**kwargs)
181 self.job_data = job_data
182 self.start_time = time.time()
184 def compose(self) -> ComposeResult:
185 project = self.job_data.get("project", "unknown")
186 job_id = self.job_data.get("job_id", "unknown")[:8]
188 status = self.job_data.get("status", "").lower()
189 status_emoji = {
190 "running": "🔄",
191 "completed": "✅",
192 "failed": "❌",
193 "pending": "⏳",
194 }.get(status, "❓")
196 self.border_title = f"{status_emoji} {project} [{job_id}]"
197 self.border_title_align = "left"
199 with Horizontal():
200 with Vertical(id="job-progress-section"):
201 yield self._compose_progress_section()
203 with Vertical(id="job-metrics-section"):
204 yield self._compose_metrics_section()
206 def _compose_progress_section(self) -> ComposeResult:
207 iteration = self.job_data.get("iteration", 1)
208 max_iterations = self.job_data.get("max_iterations", 10)
209 progress = self.job_data.get("progress", 0)
211 stage = self.job_data.get("stage", "Unknown")
212 status = self.job_data.get("status", "Unknown")
214 yield Label(f"Stage: {stage}", classes="stage-label")
215 yield Label(f"Status: {status}", classes="status-label")
216 yield Label(f"Iteration: {iteration} / {max_iterations}")
218 yield ProgressBar(
219 total=100,
220 progress=progress,
221 id=f"job-progress-{self.job_data.get('job_id', 'unknown')}",
222 )
224 elapsed = time.time() - self.start_time
225 yield Label(f"⏱️ Elapsed: {self._format_time(elapsed)}")
227 def _compose_metrics_section(self) -> ComposeResult:
228 total_issues = self.job_data.get("total_issues", 0)
229 fixed = self.job_data.get("errors_fixed", 0)
230 remaining = max(0, total_issues - fixed)
232 with Horizontal(classes="metrics-grid"):
233 yield MetricCard("Issues Found", str(total_issues), color="yellow")
234 yield MetricCard(
235 "Fixed",
236 str(fixed),
237 "↑" if fixed > 0 else "",
238 color="green",
239 )
240 yield MetricCard(
241 "Remaining",
242 str(remaining),
243 "↓" if fixed > 0 else "",
244 color="red",
245 )
247 if total_issues > 0:
248 success_rate = (fixed / total_issues) * 100
249 yield Label(
250 f"Success Rate: {success_rate:.1f}%",
251 classes="success-rate",
252 )
254 def _format_time(self, seconds: float) -> str:
255 if seconds < 60:
256 return f"{seconds:.0f}s"
257 if seconds < 3600:
258 return f"{seconds / 60:.0f}m {seconds % 60:.0f}s"
259 return f"{seconds / 3600:.0f}h {(seconds % 3600) / 60:.0f}m"
262class ServiceHealthPanel(Widget):
263 def __init__(self, **kwargs) -> None:
264 super().__init__(**kwargs)
265 self.border_title = "🏥 Service Health"
266 self.border_title_align = "left"
268 def compose(self) -> ComposeResult:
269 yield DataTable(id="services-table")
271 def on_mount(self) -> None:
272 table = self.query_one("#services-table", DataTable)
273 table.add_columns(
274 ("Service", 20),
275 ("Status", 12),
276 ("Health", 10),
277 ("Uptime", 15),
278 ("Last Check", 20),
279 )
280 table.zebra_stripes = True
282 def update_services(self, services: list[dict]) -> None:
283 table = self.query_one("#services-table", DataTable)
284 table.clear()
286 for service in services:
287 name = service.get("name", "Unknown")
288 status = service.get("status", "unknown")
289 health = service.get("health", "unknown")
290 uptime = service.get("uptime", 0)
291 last_check = service.get("last_check", "Never")
293 status_indicator = {
294 "running": "🟢 Running",
295 "stopped": "🔴 Stopped",
296 "starting": "🟡 Starting",
297 "error": "❌ Error",
298 }.get(status, "❓ Unknown")
300 health_indicator = {
301 "healthy": "✅",
302 "unhealthy": "❌",
303 "degraded": "⚠️",
304 "unknown": "❓",
305 }.get(health, "❓")
307 uptime_str = self._format_uptime(uptime)
309 if isinstance(last_check, int | float):
310 last_check_str = datetime.fromtimestamp(last_check).strftime(
311 "%H:%M:%S",
312 )
313 else:
314 last_check_str = str(last_check)
316 table.add_row(
317 f"🔧 {name}",
318 status_indicator,
319 health_indicator,
320 uptime_str,
321 last_check_str,
322 )
324 def _format_uptime(self, seconds: float) -> str:
325 if seconds < 60:
326 return f"{seconds:.0f}s"
327 if seconds < 3600:
328 return f"{seconds / 60:.0f}m"
329 if seconds < 86400:
330 return f"{seconds / 3600:.1f}h"
331 return f"{seconds / 86400:.1f}d"
334class EnhancedCrackerjackDashboard(App):
335 TITLE = "Crackerjack Progress Monitor"
336 CSS_PATH = Path(__file__).parent / "enhanced_progress_monitor.tcss"
338 def __init__(
339 self, progress_dir: Path, websocket_url: str = "ws://localhost:8675"
340 ) -> None:
341 super().__init__()
342 self.progress_dir = progress_dir
343 self.websocket_url = websocket_url
344 self.data_collector = JobDataCollector(progress_dir, websocket_url)
345 self.service_manager = ServiceManager()
346 self.update_timer = None
347 self.jobs_data = {}
349 def compose(self) -> ComposeResult:
350 yield Label("🚀 Crackerjack Progress Monitor", id="header")
352 with Container(id="main-content"):
353 yield AgentActivityWidget(id="agent-panel")
355 yield ServiceHealthPanel(id="service-panel")
357 with Container(id="jobs-container"):
358 yield Label("Loading jobs...", id="jobs-placeholder")
360 yield Footer()
362 def on_mount(self) -> None:
363 self.update_timer = self.set_interval(1.0, self.update_dashboard)
365 async def update_dashboard(self) -> None:
366 try:
367 jobs_result = await self.data_collector.discover_jobs()
368 jobs_data = jobs_result.get("data", {})
370 services = self.service_manager.check_all_services()
371 self.query_one("#service-panel", ServiceHealthPanel).update_services(
372 services,
373 )
375 if jobs_data.get("individual_jobs"):
376 aggregated_agent_data = self._aggregate_agent_data(
377 jobs_data["individual_jobs"],
378 )
379 self.query_one("#agent-panel", AgentActivityWidget).update_metrics(
380 aggregated_agent_data,
381 )
383 self._update_job_panels(jobs_data.get("individual_jobs", []))
385 except Exception as e:
386 self.console.print(f"[red]Dashboard update error: {e}[/]")
388 def _aggregate_agent_data(self, jobs: list[dict]) -> dict:
389 aggregated = {
390 "agent_activity": {
391 "active_agents": [],
392 "coordinator_status": "idle",
393 "agent_registry": {"total_agents": 6},
394 "cache_hits": 0,
395 },
396 }
398 for job in jobs:
399 if job.get("status", "").lower() == "running":
400 agent_summary = job.get("agent_summary", {})
401 if agent_summary:
402 aggregated["agent_activity"]["coordinator_status"] = "active"
404 aggregated["agent_activity"]["active_agents"].extend(
405 [
406 {
407 "agent_type": agent_type,
408 "status": "processing",
409 "confidence": 0.85,
410 "processing_time": 2.3,
411 "issue_type": "complexity"
412 if agent_type == "RefactoringAgent"
413 else "formatting",
414 }
415 for agent_type in ("RefactoringAgent", "FormattingAgent")
416 ],
417 )
419 return aggregated
421 def _update_job_panels(self, jobs: list[dict]) -> None:
422 container = self.query_one("#jobs-container", Container)
424 with suppress(Exception):
425 container.remove_children("#jobs-placeholder")
427 existing_job_ids = {panel.id for panel in container.query(".job-panel")}
428 current_job_ids = {f"job-{job['job_id']}" for job in jobs}
430 for panel_id in existing_job_ids - current_job_ids:
431 with suppress(Exception):
432 panel = container.query_one(f"#{panel_id}")
433 panel.remove()
435 for job in jobs:
436 panel_id = f"job-{job['job_id']}"
437 if panel_id not in existing_job_ids:
438 panel = JobProgressPanel(job, id=panel_id, classes="job-panel")
439 container.mount(panel)
440 else:
441 panel = container.query_one(f"#{panel_id}", JobProgressPanel)
442 panel.job_data = job
443 panel.refresh()
446async def run_enhanced_progress_monitor(
447 progress_dir: Path | None = None,
448 websocket_url: str = "ws://localhost:8675",
449 dev_mode: bool = False,
450) -> None:
451 if progress_dir is None:
452 progress_dir = Path(tempfile.gettempdir()) / "crackerjack-mcp-progress"
454 restorer = TerminalRestorer()
455 restorer.setup_handlers()
457 try:
458 app = EnhancedCrackerjackDashboard(progress_dir, websocket_url)
460 if dev_mode:
461 from rich.console import Console
463 console = Console()
464 console.print("[bold cyan]🛠️ Development Mode: Enabled[/bold cyan]")
465 app.dev = True
467 await app.run_async()
468 finally:
469 restorer.restore()
472if __name__ == "__main__":
473 import sys
474 import tempfile
476 progress_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else None
477 websocket_url = sys.argv[2] if len(sys.argv) > 2 else "ws://localhost:8675"
479 asyncio.run(run_enhanced_progress_monitor(progress_dir, websocket_url))