""" 进度追踪API路由 """ from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from uuid import UUID import logging from ..core.dependencies import get_database_session from ..schemas.progress import ProgressResponse from ..services.progress_tracker import ProgressTracker from ..core.exceptions import DatabaseError logger = logging.getLogger(__name__) router = APIRouter() @router.get("/{report_id}", response_model=ProgressResponse) async def get_report_progress( report_id: UUID, db: AsyncSession = Depends(get_database_session) ): """获取报告生成进度""" try: progress_tracker = ProgressTracker(db) progress = await progress_tracker.get_progress(report_id) logger.info(f"获取进度成功: {report_id}, 当前步骤: {progress.current_step}/{progress.total_steps}") return progress except ValueError as e: logger.warning(f"报告不存在或无进度记录: {report_id}") raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=str(e) ) except DatabaseError as e: logger.error(f"获取进度时数据库错误: {str(e)}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"数据库错误: {str(e)}" ) except Exception as e: logger.error(f"获取进度失败: {str(e)}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"获取进度失败: {str(e)}" ) @router.post("/{report_id}/reset") async def reset_report_progress( report_id: UUID, db: AsyncSession = Depends(get_database_session) ): """重置报告生成进度""" try: progress_tracker = ProgressTracker(db) await progress_tracker.reset_progress(report_id) logger.info(f"重置进度成功: {report_id}") return {"message": "进度重置成功", "report_id": str(report_id)} except ValueError as e: logger.warning(f"报告不存在: {report_id}") raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=str(e) ) except DatabaseError as e: logger.error(f"重置进度时数据库错误: {str(e)}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"数据库错误: {str(e)}" ) except Exception as e: logger.error(f"重置进度失败: {str(e)}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"重置进度失败: {str(e)}" )