31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
import json
|
|
import logging
|
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
|
from common.db.run import select_run_with_clinic
|
|
from common.deps import verify_api_key
|
|
from integrations.llm.schemas.report import ReportOutput
|
|
from models.report import MarketingReportResponse
|
|
|
|
router = APIRouter(prefix="/api/report", tags=["report"], dependencies=[Depends(verify_api_key)])
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.get("/{run_id}", response_model=MarketingReportResponse, response_model_by_alias=True)
|
|
async def get_report(run_id: str):
|
|
logger.info("GET /api/report/%s", run_id)
|
|
row = await select_run_with_clinic(run_id)
|
|
if row is None:
|
|
raise HTTPException(status_code=404, detail="Run not found")
|
|
if row["report_data"] is None:
|
|
return Response(status_code=204)
|
|
data = json.loads(row["report_data"]) if isinstance(row["report_data"], str) else row["report_data"]
|
|
llm_output = ReportOutput(**data)
|
|
return MarketingReportResponse(
|
|
id=run_id,
|
|
clinic_name=row["hospital_name"],
|
|
clinic_name_en=row["hospital_name_en"],
|
|
created_at=str(row["created_at"]),
|
|
target_url=row["target_url"],
|
|
**llm_output.model_dump(exclude={"id", "created_at", "target_url"}),
|
|
)
|