This commit is contained in:
cjw
2026-02-17 00:25:18 +08:00
parent 64acdb0e8c
commit 9cdb46eec3
2 changed files with 590 additions and 9 deletions
+79 -7
View File
@@ -1,6 +1,6 @@
# api/routes.py
from fastapi import APIRouter, UploadFile, File, HTTPException, BackgroundTasks, Request, Depends
from typing import Optional
from typing import Optional, Dict, Any
import uuid
from datetime import datetime
from pathlib import Path
@@ -118,14 +118,86 @@ async def upload_stp(
@router.get("/status/{task_id}")
@router.post("/status/{task_id}")
async def get_status(task_id: str):
"""获取任务状态"""
if task_id not in tasks:
async def get_status(task_id: str, db_session: AsyncSession = Depends(get_db_session)):
"""
获取任务状态
优先返回内存中的任务信息;
如果内存中不存在,则从 PostgreSQL + RustFS 组装一个持久化的任务视图,
结构与内存任务保持尽量一致,便于前端集中展示总结性信息。
"""
# 1. 内存任务(进行中的任务)
if task_id in tasks:
task = tasks[task_id]
logger.info(f"返回内存任务状态: {task_id} - {task['status']}")
return task
# 2. 持久化任务(已完成/失败,或服务重启后的任务)
from sqlalchemy import select
from models.database import ProcessingTask, STPFile, GeometryData, MeshData, MoldCavityData
storage_service = StorageIntegrationService()
# 查询任务和文件元数据
result = await db_session.execute(
select(ProcessingTask, STPFile)
.join(STPFile, ProcessingTask.stp_file_id == STPFile.id)
.where(ProcessingTask.task_id == task_id)
)
row = result.first()
if not row:
raise HTTPException(404, "任务不存在")
task = tasks[task_id]
logger.info(f"返回任务状态: {task_id} - {task['status']}")
return task
processing_task, stp_file = row
# 从 RustFS 取几何 / 型腔 / 网格详细 JSON(小量数据,便于前端展示汇总)
file_with_data = await storage_service.get_stp_file_with_data(
db_session, stp_file_id=stp_file.id
)
geometry_json: Optional[Dict[str, Any]] = None
if file_with_data.get("geometry_data"):
# save_geometry_data 支持两种格式,这里直接拿回来的就是几何字典
geometry_json = file_with_data["geometry_data"]
cavity_json: Optional[Dict[str, Any]] = file_with_data.get("mold_cavity_data")
# 组装网格摘要:从 MeshData 表中读出摘要字段,避免把完整网格 JSON 丢给前端
mesh_summary = None
mesh_record = await db_session.execute(
select(MeshData).where(MeshData.stp_file_id == stp_file.id)
)
mesh_record = mesh_record.scalar_one_or_none()
if mesh_record:
mesh_summary = {
"vertex_count": mesh_record.vertex_count,
"face_count": mesh_record.face_count,
"point_count": mesh_record.point_count,
"quality": mesh_record.quality,
}
# 构造与内存任务兼容的任务视图
task_view = {
"task_id": processing_task.task_id,
"status": processing_task.status,
"filename": stp_file.original_filename if stp_file else "",
"file_path": stp_file.file_path or "",
"file_size": stp_file.file_size if stp_file else 0,
"upload_time": processing_task.created_time.isoformat()
if processing_task.created_time
else "",
"completed_at": processing_task.completed_time.isoformat()
if processing_task.completed_time
else "",
"geometry_data": geometry_json,
# key_info 沿用之前的结构,直接使用详细型腔 JSON,前端已按该结构解析
"key_info": cavity_json,
"mesh_summary": mesh_summary,
"analysis_result": None,
"error": processing_task.error_message or stp_file.error_message or None,
}
logger.info(f"返回持久化任务状态: {task_id} - {processing_task.status}")
return task_view
@router.get("/debug/tasks")