38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
import json
|
|
import logging
|
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
|
from common.db import fetchone
|
|
from common.deps import verify_api_key
|
|
from common.utils import _with_scheme
|
|
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 fetchone(
|
|
"SELECT ar.report_data, ar.created_at, h.hospital_name, h.hospital_name_en, h.url"
|
|
" FROM analysis_runs ar"
|
|
" JOIN hospital_baseinfo h ON ar.hospital_id = h.hospital_id"
|
|
" WHERE ar.analysis_run_id = %s",
|
|
(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=_with_scheme(row["url"]),
|
|
**llm_output.model_dump(exclude={"id", "created_at", "target_url"}),
|
|
)
|