db migration done
This commit is contained in:
1 parent
c9c5ee9177
commit
3b4c154fb2
23 files changed
+622
-1049
No files matched your search
+25
-19
@@ -2,12 +2,14 @@ import logging
|
||||
import uuid6
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, File, Form, HTTPException, UploadFile, 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 common.db.hospital import select_hospital
|
||||
from common.db.source import select_source_mainpage, insert_source, insert_raw_info
|
||||
from common.db.run import insert_run, select_run_status
|
||||
from models.analysis import AnalysisCreate, AnalysisStartResponse, AnalysisStatusResponse
|
||||
from models.file import FileListItem, FileType, FileUploadResponse
|
||||
from models.status import AnalysisStatus
|
||||
from models.status import AnalysisStatus, SourceType
|
||||
from services.pipeline import run_pipeline
|
||||
from services.file import get_analysis_files_response, handle_analysis_file_upload, soft_delete_analysis_file
|
||||
from services.file_data import get_analysis_files_response, handle_analysis_file_upload, soft_delete_analysis_file
|
||||
|
||||
router = APIRouter(prefix="/api/analysis", tags=["analysis"], dependencies=[Depends(verify_api_key)])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -20,23 +22,27 @@ async def start_analysis(body: AnalysisCreate, background_tasks: BackgroundTasks
|
||||
hospital_id = body.clinic_id
|
||||
|
||||
# 사실 hospital과 owner_user_id 비교 후 검증이 필요한 거지만 일단 PoC 니까. 나중에 바꿉니다.
|
||||
hospital = await fetchone(
|
||||
"SELECT owner_user_id, url FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
hospital = await select_hospital(hospital_id)
|
||||
if not hospital:
|
||||
raise HTTPException(status_code=409, detail="Clinic not found")
|
||||
|
||||
ig_id = await insert_instagram_row(hospital_id, body.channels.instagram) if body.channels.instagram 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_run(analysis_run_id, hospital_id, hospital["owner_user_id"])
|
||||
|
||||
analysis_run_id = await insert_analysis_run(
|
||||
analysis_run_id, hospital_id, hospital["owner_user_id"],
|
||||
ig_id, fb_id, nb_id, yt_id, gu_id,
|
||||
)
|
||||
mainpage = await select_source_mainpage(hospital_id)
|
||||
if mainpage:
|
||||
await insert_raw_info(mainpage["source_id"], analysis_run_id, data_tag=SourceType.MAINPAGE)
|
||||
|
||||
channels = [
|
||||
(SourceType.INSTAGRAM, body.channels.instagram),
|
||||
(SourceType.FACEBOOK, body.channels.facebook),
|
||||
(SourceType.NAVER_BLOG, body.channels.naver_blog),
|
||||
(SourceType.YOUTUBE, body.channels.youtube),
|
||||
(SourceType.GANGNAM_UNNI, body.channels.gangnam_unni),
|
||||
]
|
||||
for source_type, url in channels:
|
||||
if url:
|
||||
source_id = await insert_source(hospital_id, source_type, url)
|
||||
await insert_raw_info(source_id, analysis_run_id, data_tag=source_type)
|
||||
|
||||
background_tasks.add_task(run_pipeline, analysis_run_id)
|
||||
|
||||
@@ -75,12 +81,12 @@ async def delete_analysis_run_file(run_id: str, file_id: int) -> None:
|
||||
@router.get("/{run_id}/status", response_model=AnalysisStatusResponse)
|
||||
async def get_analysis_status(run_id: str):
|
||||
logger.info("GET /api/analysis/%s/status", run_id)
|
||||
row = await fetchone("SELECT status FROM analysis_runs WHERE analysis_run_id = %s", (run_id,))
|
||||
if not row:
|
||||
run_status = await select_run_status(run_id)
|
||||
if run_status is None:
|
||||
raise HTTPException(status_code=404, detail="Run not found")
|
||||
return AnalysisStatusResponse(
|
||||
analysis_run_id=run_id,
|
||||
status=AnalysisStatus(row["status"]),
|
||||
status=AnalysisStatus(run_status),
|
||||
progress=50.0,
|
||||
current_step="",
|
||||
channel_errors={},
|
||||
|
||||
+4
-8
@@ -2,7 +2,8 @@ import logging
|
||||
import uuid6
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from common.deps import verify_api_key
|
||||
from common.db import insert_hospital, fetchone
|
||||
from common.db.hospital import select_hospital, insert_hospital
|
||||
from common.db.source import insert_source
|
||||
from common.utils import get_env
|
||||
from integrations.firecrawl import FirecrawlClient
|
||||
from models.clinic import ClinicCreate, ClinicCreateResponse, ClinicResponse, ClinicHistoryResponse, RunSummary
|
||||
@@ -30,9 +31,8 @@ async def create_clinic(body: ClinicCreate):
|
||||
name=info["clinicName"],
|
||||
name_en=info.get("clinicNameEn"),
|
||||
road_address=info.get("address"),
|
||||
url=body.url,
|
||||
raw_data=info,
|
||||
)
|
||||
await insert_source(hospital_id, "mainpage", body.url)
|
||||
return ClinicCreateResponse(
|
||||
id=hospital_id,
|
||||
url=body.url,
|
||||
@@ -44,11 +44,7 @@ async def create_clinic(body: ClinicCreate):
|
||||
@router.get("/{hospital_id}", response_model=ClinicResponse)
|
||||
async def get_clinic(hospital_id: str):
|
||||
logger.info("GET /api/clinics/%s", hospital_id)
|
||||
row = await fetchone(
|
||||
"SELECT hospital_id, hospital_name, hospital_name_en, road_address, url, status, raw_data, created_at, updated_at"
|
||||
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
row = await select_hospital(hospital_id)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="Clinic not found")
|
||||
return ClinicResponse(**{**row, "created_at": str(row["created_at"]), "updated_at": str(row["updated_at"])})
|
||||
|
||||
+3
-9
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from common.db import fetchone
|
||||
from common.db.run import select_run_with_clinic
|
||||
from common.deps import verify_api_key
|
||||
from integrations.llm.schemas.plan import PlanOutput
|
||||
from models.plan import PlanApiResponse
|
||||
@@ -13,13 +13,7 @@ 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,),
|
||||
)
|
||||
row = await select_run_with_clinic(run_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Run not found")
|
||||
if row["plan_data"] is None:
|
||||
@@ -31,6 +25,6 @@ async def get_plan(run_id: str):
|
||||
clinic_name=row["hospital_name"],
|
||||
clinic_name_en=row["hospital_name_en"],
|
||||
created_at=str(row["created_at"]),
|
||||
target_url=row["url"],
|
||||
target_url=row["target_url"],
|
||||
**plan.model_dump(),
|
||||
)
|
||||
+3
-9
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from common.db import fetchone
|
||||
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
|
||||
@@ -13,13 +13,7 @@ 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,),
|
||||
)
|
||||
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:
|
||||
@@ -31,6 +25,6 @@ async def get_report(run_id: str):
|
||||
clinic_name=row["hospital_name"],
|
||||
clinic_name_en=row["hospital_name_en"],
|
||||
created_at=str(row["created_at"]),
|
||||
target_url=row["url"],
|
||||
target_url=row["target_url"],
|
||||
**llm_output.model_dump(exclude={"id", "created_at", "target_url"}),
|
||||
)
|
||||
Reference in new issue
Block a user