添加任务ID跟踪功能到模板填充接口
- 在FillRequest中添加可选的task_id字段,用于任务历史跟踪 - 实现任务状态管理,包括创建、更新和错误处理 - 集成MongoDB任务记录功能,在处理过程中更新进度 - 添加任务进度更新逻辑,支持开始、处理中、成功和失败状态 - 修改模板填充服务以接收并传递task_id参数
This commit is contained in:
@@ -79,6 +79,7 @@ class FillRequest(BaseModel):
|
|||||||
source_doc_ids: Optional[List[str]] = None # MongoDB 文档 ID 列表
|
source_doc_ids: Optional[List[str]] = None # MongoDB 文档 ID 列表
|
||||||
source_file_paths: Optional[List[str]] = None # 源文档文件路径列表
|
source_file_paths: Optional[List[str]] = None # 源文档文件路径列表
|
||||||
user_hint: Optional[str] = None
|
user_hint: Optional[str] = None
|
||||||
|
task_id: Optional[str] = None # 可选的任务ID,用于任务历史跟踪
|
||||||
|
|
||||||
|
|
||||||
class ExportRequest(BaseModel):
|
class ExportRequest(BaseModel):
|
||||||
@@ -439,7 +440,27 @@ async def fill_template(
|
|||||||
Returns:
|
Returns:
|
||||||
填写结果
|
填写结果
|
||||||
"""
|
"""
|
||||||
|
# 生成或使用传入的 task_id
|
||||||
|
task_id = request.task_id or str(uuid.uuid4())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# 创建任务记录到 MongoDB
|
||||||
|
try:
|
||||||
|
await mongodb.insert_task(
|
||||||
|
task_id=task_id,
|
||||||
|
task_type="template_fill",
|
||||||
|
status="processing",
|
||||||
|
message=f"开始填表任务: {len(request.template_fields)} 个字段"
|
||||||
|
)
|
||||||
|
except Exception as mongo_err:
|
||||||
|
logger.warning(f"MongoDB 创建任务记录失败: {mongo_err}")
|
||||||
|
|
||||||
|
# 更新进度 - 开始
|
||||||
|
await update_task_status(
|
||||||
|
task_id, "processing",
|
||||||
|
progress=0, message="开始处理..."
|
||||||
|
)
|
||||||
|
|
||||||
# 转换字段
|
# 转换字段
|
||||||
fields = [
|
fields = [
|
||||||
TemplateField(
|
TemplateField(
|
||||||
@@ -461,6 +482,12 @@ async def fill_template(
|
|||||||
elif ext == "docx":
|
elif ext == "docx":
|
||||||
template_file_type = "docx"
|
template_file_type = "docx"
|
||||||
|
|
||||||
|
# 更新进度 - 准备开始填写
|
||||||
|
await update_task_status(
|
||||||
|
task_id, "processing",
|
||||||
|
progress=10, message=f"准备填写 {len(fields)} 个字段..."
|
||||||
|
)
|
||||||
|
|
||||||
# 执行填写
|
# 执行填写
|
||||||
result = await template_fill_service.fill_template(
|
result = await template_fill_service.fill_template(
|
||||||
template_fields=fields,
|
template_fields=fields,
|
||||||
@@ -468,12 +495,29 @@ async def fill_template(
|
|||||||
source_file_paths=request.source_file_paths,
|
source_file_paths=request.source_file_paths,
|
||||||
user_hint=request.user_hint,
|
user_hint=request.user_hint,
|
||||||
template_id=request.template_id,
|
template_id=request.template_id,
|
||||||
template_file_type=template_file_type
|
template_file_type=template_file_type,
|
||||||
|
task_id=task_id
|
||||||
)
|
)
|
||||||
|
|
||||||
return result
|
# 更新为成功
|
||||||
|
await update_task_status(
|
||||||
|
task_id, "success",
|
||||||
|
progress=100, message="填表完成",
|
||||||
|
result={
|
||||||
|
"field_count": len(fields),
|
||||||
|
"max_rows": result.get("max_rows", 0)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return {**result, "task_id": task_id}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# 更新为失败
|
||||||
|
await update_task_status(
|
||||||
|
task_id, "failure",
|
||||||
|
progress=0, message="填表失败",
|
||||||
|
error=str(e)
|
||||||
|
)
|
||||||
logger.error(f"填写表格失败: {str(e)}")
|
logger.error(f"填写表格失败: {str(e)}")
|
||||||
raise HTTPException(status_code=500, detail=f"填写失败: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"填写失败: {str(e)}")
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,8 @@ class TemplateFillService:
|
|||||||
source_file_paths: Optional[List[str]] = None,
|
source_file_paths: Optional[List[str]] = None,
|
||||||
user_hint: Optional[str] = None,
|
user_hint: Optional[str] = None,
|
||||||
template_id: Optional[str] = None,
|
template_id: Optional[str] = None,
|
||||||
template_file_type: Optional[str] = "xlsx"
|
template_file_type: Optional[str] = "xlsx",
|
||||||
|
task_id: Optional[str] = None
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
填写表格模板
|
填写表格模板
|
||||||
@@ -74,6 +75,7 @@ class TemplateFillService:
|
|||||||
user_hint: 用户提示(如"请从合同文档中提取")
|
user_hint: 用户提示(如"请从合同文档中提取")
|
||||||
template_id: 模板文件路径(用于重新生成表头)
|
template_id: 模板文件路径(用于重新生成表头)
|
||||||
template_file_type: 模板文件类型
|
template_file_type: 模板文件类型
|
||||||
|
task_id: 可选的任务ID,用于任务进度跟踪
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
填写结果
|
填写结果
|
||||||
|
|||||||
Reference in New Issue
Block a user