55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
|
|
from uuid import UUID
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.core.database import get_db
|
||
|
|
from app.repositories.case_repo import CaseRepository
|
||
|
|
from app.schemas.analysis import AnalysisStatusOut, AnalysisTriggerOut
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/cases/{case_id}/analyze", response_model=AnalysisTriggerOut)
|
||
|
|
async def trigger_analysis(case_id: UUID, db: AsyncSession = Depends(get_db)):
|
||
|
|
repo = CaseRepository(db)
|
||
|
|
case = await repo.get(case_id)
|
||
|
|
if not case:
|
||
|
|
raise HTTPException(404, "案件不存在")
|
||
|
|
|
||
|
|
from app.workers.analysis_tasks import run_full_analysis
|
||
|
|
try:
|
||
|
|
task = run_full_analysis.delay(str(case_id))
|
||
|
|
task_id = task.id
|
||
|
|
except Exception:
|
||
|
|
task_id = "sync-fallback"
|
||
|
|
from app.services.analysis_pipeline import run_analysis_sync
|
||
|
|
await run_analysis_sync(case_id, db)
|
||
|
|
|
||
|
|
return AnalysisTriggerOut(task_id=task_id, message="分析任务已提交")
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/cases/{case_id}/analyze/status", response_model=AnalysisStatusOut)
|
||
|
|
async def analysis_status(case_id: UUID, db: AsyncSession = Depends(get_db)):
|
||
|
|
repo = CaseRepository(db)
|
||
|
|
case = await repo.get(case_id)
|
||
|
|
if not case:
|
||
|
|
raise HTTPException(404, "案件不存在")
|
||
|
|
|
||
|
|
step_map = {
|
||
|
|
"pending": ("等待上传", 0),
|
||
|
|
"uploading": ("上传中", 15),
|
||
|
|
"analyzing": ("分析中", 50),
|
||
|
|
"reviewing": ("待复核", 85),
|
||
|
|
"completed": ("已完成", 100),
|
||
|
|
}
|
||
|
|
step_label, progress = step_map.get(case.status.value, ("未知", 0))
|
||
|
|
|
||
|
|
return AnalysisStatusOut(
|
||
|
|
case_id=str(case_id),
|
||
|
|
status=case.status.value,
|
||
|
|
progress=progress,
|
||
|
|
current_step=step_label,
|
||
|
|
message=f"当前状态: {step_label}",
|
||
|
|
)
|