64 lines
3.2 KiB
Python
64 lines
3.2 KiB
Python
import uuid6
|
|
from fastapi import APIRouter, BackgroundTasks, Depends, status
|
|
from common.deps import verify_api_key
|
|
from common.db import fetchone, insert_instagram_row, insert_facebook_row, insert_naver_blog_row, insert_youtube_row, insert_gangnam_unni_row, insert_analysis_run
|
|
from models.analysis import AnalysisCreate, AnalysisStartResponse, AnalysisStatusResponse
|
|
from models.status import AnalysisStatus
|
|
from services.collect import collect_instagram, collect_facebook, collect_naver_blog, collect_youtube, collect_gangnam_unni
|
|
|
|
router = APIRouter(prefix="/api/analyses", tags=["analyses"], dependencies=[Depends(verify_api_key)])
|
|
|
|
|
|
@router.post("", status_code=status.HTTP_202_ACCEPTED, response_model=AnalysisStartResponse)
|
|
async def start_analysis(body: AnalysisCreate, background_tasks: BackgroundTasks):
|
|
analysis_run_id = str(uuid6.uuid7())
|
|
hospital_id = body.clinic_id
|
|
|
|
# 사실 hospital과 owner_user_id 비교 후 검증이 필요한 거지만 일단 PoC 니까. 나중에 바꿉니다.
|
|
hospital = await fetchone(
|
|
"SELECT owner_user_id FROM hospital_baseinfo WHERE hospital_id = %s",
|
|
(hospital_id,),
|
|
)
|
|
owner_user_id = hospital["owner_user_id"] if hospital else 0
|
|
|
|
ig_url = body.channels.instagram[0] if isinstance(body.channels.instagram, list) else body.channels.instagram
|
|
ig_id = await insert_instagram_row(hospital_id, ig_url) if ig_url else None
|
|
fb_id = await insert_facebook_row(hospital_id, body.channels.facebook) if body.channels.facebook else None
|
|
nb_id = await insert_naver_blog_row(hospital_id, body.channels.naver_blog) if body.channels.naver_blog else None
|
|
yt_id = await insert_youtube_row(hospital_id, body.channels.youtube) if body.channels.youtube else None
|
|
gu_id = await insert_gangnam_unni_row(hospital_id, body.channels.gangnam_unni) if body.channels.gangnam_unni else None
|
|
|
|
analysis_run_id = await insert_analysis_run(analysis_run_id, hospital_id, owner_user_id, ig_id, fb_id, nb_id, yt_id, gu_id)
|
|
|
|
if ig_id:
|
|
background_tasks.add_task(collect_instagram, analysis_run_id, ig_id, ig_url)
|
|
if fb_id:
|
|
background_tasks.add_task(collect_facebook, analysis_run_id, fb_id, body.channels.facebook)
|
|
if nb_id:
|
|
background_tasks.add_task(collect_naver_blog, analysis_run_id, nb_id, body.channels.naver_blog)
|
|
if yt_id:
|
|
background_tasks.add_task(collect_youtube, analysis_run_id, yt_id, body.channels.youtube)
|
|
if gu_id:
|
|
background_tasks.add_task(collect_gangnam_unni, analysis_run_id, gu_id, body.channels.gangnam_unni)
|
|
|
|
return AnalysisStartResponse(
|
|
analysis_run_id=analysis_run_id,
|
|
clinic_id=hospital_id,
|
|
status=AnalysisStatus.DISCOVERING,
|
|
estimated_seconds=90,
|
|
poll_url=f"/api/analyses/{analysis_run_id}/status",
|
|
)
|
|
|
|
|
|
@router.get("/{run_id}/status", response_model=AnalysisStatusResponse)
|
|
async def get_analysis_status(run_id: str):
|
|
row = await fetchone("SELECT status FROM analysis_runs WHERE analysis_run_id = %s", (run_id,))
|
|
return AnalysisStatusResponse(
|
|
analysis_run_id=run_id,
|
|
status=AnalysisStatus(row["status"]),
|
|
progress=50.0,
|
|
current_step="",
|
|
channel_errors={},
|
|
completed_at=None,
|
|
)
|