import json from fastapi import APIRouter from app.config import LOG_DIR router = APIRouter() @router.get("") async def list_reports(): """List all daily detection reports with summaries.""" reports = [] for path in sorted(LOG_DIR.glob("report_*.json"), reverse=True): try: with open(path) as f: data = json.load(f) iso_count = len(data.get("isotopes_detected", [])) reports.append({ "date": data.get("date", ""), "live_time_hours": data.get("live_time_hours", 0), "total_counts": data.get("total_counts", 0), "cps_mean": data.get("cps_mean", 0), "background_subtracted": data.get("background_subtracted", False), "isotope_count": iso_count, "isotopes": [i["isotope"] for i in data.get("isotopes_detected", [])], }) except (json.JSONDecodeError, OSError): continue return reports @router.get("/{date}") async def get_report(date: str): """Get a specific day's full report.""" report_path = LOG_DIR / f"report_{date}.json" if not report_path.exists(): from fastapi import HTTPException raise HTTPException(status_code=404, detail=f"No report for {date}") try: with open(report_path) as f: data = json.load(f) except (json.JSONDecodeError, OSError): raise HTTPException(status_code=500, detail="Report file corrupt") return data