#!/usr/bin/env python3 """Real-time dashboard for autoresearch experiments. Usage: python dashboard.py # start on port 8050 python dashboard.py --port 8080 # custom port """ import argparse import asyncio import json from datetime import datetime from pathlib import Path from fastapi import FastAPI from fastapi.responses import HTMLResponse, StreamingResponse import uvicorn ROOT = Path(__file__).parent RUNS_DIR = ROOT / "runs" BEST_DIR = ROOT / "best" STATUS_FILE = ROOT / "status.json" RUNS_WF_DIR = ROOT / "runs_wf" BEST_WF_DIR = ROOT / "best_wf" STATUS_WF_FILE = ROOT / "status_wf.json" HTML_FILE = ROOT / "dashboard.html" app = FastAPI(title="Autoresearch Dashboard") @app.get("/", response_class=HTMLResponse) async def index(): """Serve the dashboard HTML.""" if HTML_FILE.exists(): return HTML_FILE.read_text() return "

dashboard.html not found

" @app.get("/api/status") async def api_status(): """Current runner status.""" if STATUS_FILE.exists(): try: return json.loads(STATUS_FILE.read_text()) except json.JSONDecodeError: pass return {"state": "idle", "run_number": 0, "total_planned": 0} @app.get("/api/runs") async def api_runs(): """All runs sorted by timestamp.""" if not RUNS_DIR.exists(): return [] runs = [] for f in sorted(RUNS_DIR.glob("run_*.json")): try: data = json.loads(f.read_text()) data["_file"] = f.name runs.append(data) except (json.JSONDecodeError, OSError): continue return runs @app.get("/api/best") async def api_best(): """Current best result.""" best_file = BEST_DIR / "best_sharpe.json" if best_file.exists(): try: return json.loads(best_file.read_text()) except json.JSONDecodeError: pass return None @app.get("/api/events") async def sse_events(): """Server-Sent Events stream for real-time updates.""" async def event_stream(): last_count = 0 last_status = "" while True: # Check for new runs count = len(list(RUNS_DIR.glob("run_*.json"))) if RUNS_DIR.exists() else 0 status_data = "" if STATUS_FILE.exists(): try: status_data = STATUS_FILE.read_text() except OSError: pass if count != last_count or status_data != last_status: last_count = count last_status = status_data event = json.dumps({ "runs_count": count, "status": json.loads(status_data) if status_data else None, "timestamp": datetime.now().isoformat(), }) yield f"data: {event}\n\n" await asyncio.sleep(2) return StreamingResponse(event_stream(), media_type="text/event-stream") @app.get("/api/wf/status") async def api_wf_status(): if STATUS_WF_FILE.exists(): try: return json.loads(STATUS_WF_FILE.read_text()) except json.JSONDecodeError: pass return {"state": "idle", "run_number": 0, "total_planned": 0} @app.get("/api/wf/runs") async def api_wf_runs(): if not RUNS_WF_DIR.exists(): return [] runs = [] for f in sorted(RUNS_WF_DIR.glob("run_wf_*.json")): try: data = json.loads(f.read_text()) data["_file"] = f.name runs.append(data) except (json.JSONDecodeError, OSError): continue return runs @app.get("/api/wf/best") async def api_wf_best(): best_file = BEST_WF_DIR / "best_wf_sharpe.json" if best_file.exists(): try: return json.loads(best_file.read_text()) except json.JSONDecodeError: pass return None @app.get("/api/wf/events") async def sse_wf_events(): async def event_stream(): last_count = 0 last_status = "" while True: count = len(list(RUNS_WF_DIR.glob("run_wf_*.json"))) if RUNS_WF_DIR.exists() else 0 status_data = "" if STATUS_WF_FILE.exists(): try: status_data = STATUS_WF_FILE.read_text() except OSError: pass if count != last_count or status_data != last_status: last_count = count last_status = status_data event = json.dumps({ "runs_count": count, "status": json.loads(status_data) if status_data else None, "timestamp": datetime.now().isoformat(), }) yield f"data: {event}\n\n" await asyncio.sleep(2) return StreamingResponse(event_stream(), media_type="text/event-stream") def main(): parser = argparse.ArgumentParser() parser.add_argument("--port", type=int, default=8050) parser.add_argument("--host", default="0.0.0.0") args = parser.parse_args() print(f"Dashboard: http://localhost:{args.port}") uvicorn.run(app, host=args.host, port=args.port, log_level="warning") if __name__ == "__main__": main()