37 lines
1.3 KiB
Python
37 lines
1.3 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 integrations.llm.schemas.plan import PlanOutput
|
|
from models.plan import PlanApiResponse
|
|
|
|
router = APIRouter(prefix="/api/plan", tags=["plan"], dependencies=[Depends(verify_api_key)])
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.get("/{run_id}", response_model=PlanApiResponse, response_model_by_alias=True)
|
|
async def get_plan(run_id: str):
|
|
logger.info("GET /api/plan/%s", run_id)
|
|
row = await fetchone(
|
|
"SELECT ar.plan_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["plan_data"] is None:
|
|
return Response(status_code=204)
|
|
data = json.loads(row["plan_data"]) if isinstance(row["plan_data"], str) else row["plan_data"]
|
|
plan = PlanOutput(**data)
|
|
return PlanApiResponse(
|
|
id=run_id,
|
|
clinic_name=row["hospital_name"],
|
|
clinic_name_en=row["hospital_name_en"],
|
|
created_at=str(row["created_at"]),
|
|
target_url=row["url"],
|
|
**plan.model_dump(),
|
|
)
|