""" FastAPI Backend — serves metrics and trade data to the React dashboard. """ from contextlib import asynccontextmanager import os from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from bot.data.db import Database db = Database() @asynccontextmanager async def lifespan(app: FastAPI): await db.connect() yield await db.disconnect() app = FastAPI(title="Polymarket Bot API", lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["GET"], allow_headers=["*"], ) @app.get("/health") async def health(): return {"status": "ok", "paper_mode": os.getenv("PAPER_MODE", "true")} @app.get("/api/metrics") async def get_metrics(): history = await db.get_metrics_history(days=42) if not history: return {"history": [], "latest": None} return {"history": history, "latest": history[0]} @app.get("/api/trades") async def get_trades(limit: int = 50, status: str = "open"): """ status: "open" (default) | "closed" | "all" Each trade includes a computed "status" field. """ if status not in ("open", "closed", "all"): status = "open" filter_status = None if status == "all" else status trades = await db.get_recent_trades(limit=limit, status=filter_status) return {"trades": trades, "count": len(trades), "status_filter": status} @app.get("/api/summary") async def get_summary(): """Dashboard summary card data.""" history = await db.get_metrics_history(days=1) trades = await db.get_recent_trades(limit=500) latest = history[0] if history else {} paper_bankroll = float(os.getenv("PAPER_BANKROLL", "10000")) total_deployed = sum(t.get("net_cost", 0) for t in trades) return { "paper_mode": os.getenv("PAPER_MODE", "true") == "true", "paper_bankroll": paper_bankroll, "total_trades": len(trades), "total_deployed": total_deployed, "total_pnl": latest.get("total_pnl", 0), "win_rate": latest.get("win_rate", 0), "sharpe_ratio": latest.get("sharpe_ratio", 0), "calibration_score": latest.get("calibration_score", 0), "promotion_ready": ( latest.get("sharpe_ratio", 0) >= 0.5 and latest.get("win_rate", 0) >= 0.52 and latest.get("calibration_score", 0) >= 0.7 and len(trades) >= 50 ), }