40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
import json
|
|
import logging
|
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
|
from common.db import fetchone, fetch_raw
|
|
from common.deps import verify_api_key
|
|
from common.utils import _with_scheme
|
|
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, ar.gangnam_unni_data_id, 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)
|
|
gangnam_unni = await fetch_raw("gangnam_unni_data", row["gangnam_unni_data_id"]) or {}
|
|
clinic_name = gangnam_unni.get("name") or row["hospital_name"]
|
|
return PlanApiResponse(
|
|
id=run_id,
|
|
clinic_name=clinic_name,
|
|
clinic_name_en=row["hospital_name_en"],
|
|
created_at=str(row["created_at"]),
|
|
target_url=_with_scheme(row["url"]),
|
|
**plan.model_dump(),
|
|
)
|