VWED_server/routes/task_record_api.py
2025-05-12 15:43:21 +08:00

138 lines
4.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
任务运行记录API模块
提供任务运行记录相关的API接口
"""
from typing import Dict, List, Any, Optional
from fastapi import APIRouter, Query, Path, Request
from routes.common_api import format_response, error_response
from utils.logger import get_logger
from services.task_record_service import TaskRecordService
# 创建路由
router = APIRouter(
prefix="/api/vwed-task-record",
tags=["VWED任务运行记录"]
)
# 设置日志
logger = get_logger("app.task_record_api")
@router.get("/blocks/{task_record_id}")
async def get_task_blocks(
task_record_id: str = Path(..., description="任务记录ID")
):
"""
获取指定任务记录下的所有块运行情况
Args:
task_record_id: 任务记录ID由run_task接口返回的taskRecordId
Returns:
包含该任务记录下所有块运行记录的响应
"""
try:
result = await TaskRecordService.get_task_blocks(task_record_id)
if not result["success"]:
return error_response(message=result["message"], code=500)
return format_response(data=result["data"], message=result["message"])
except Exception as e:
logger.error(f"获取任务块运行情况失败: {str(e)}")
return error_response(message=f"获取任务块运行情况失败: {str(e)}", code=500)
@router.get("/block/{block_record_id}")
async def get_block_detail(
block_record_id: str = Path(..., description="块记录ID")
):
"""
获取指定块记录的详细信息
Args:
block_record_id: 块记录ID
Returns:
包含该块记录详细信息的响应
"""
try:
result = await TaskRecordService.get_block_detail(block_record_id)
if not result["success"]:
return error_response(message=result["message"], code=404)
return format_response(data=result["data"], message=result["message"])
except Exception as e:
logger.error(f"获取块记录详情失败: {str(e)}")
return error_response(message=f"获取块记录详情失败: {str(e)}", code=500)
@router.post("/stop/{task_record_id}")
async def stop_task_definition(task_record_id: str = Path(..., description="任务记录ID")):
"""
停止指定任务记录下的所有运行任务实例,同时禁用定时任务
Args:
task_record_id: 任务记录ID
Returns:
包含停止结果的响应,包括成功停止的任务数量、是否为定时任务等信息
"""
try:
# 调用服务层方法
result = await TaskRecordService.stop_task_record(task_record_id)
if not result.get("success", False):
return error_response(
message=result.get("message", "停止任务失败"),
code=400
)
return format_response(
data=result.get("data", {}),
message=result.get("message", "任务终止成功")
)
except Exception as e:
logger.log_error_with_trace(f"停止任务定义异常", e)
return error_response(message=f"任务记录终止失败: {str(e)}", code=500)
@router.get("/execution/block/results/{task_record_id}")
async def get_block_result(task_record_id: str = Path(..., description="任务记录ID")):
"""
获取指定任务记录的执行结果
Args:
task_record_id: 任务记录ID
Returns:
包含执行结果的响应
"""
try:
result = await TaskRecordService.get_block_results(task_record_id)
if not result["success"]:
return error_response(message=result["message"], code=404)
return format_response(data=result["data"], message=result["message"])
except Exception as e:
logger.error(f"获取任务记录执行结果失败: {str(e)}")
return error_response(message=f"获取任务记录执行结果失败: {str(e)}", code=500)
@router.get("/detail/{task_record_id}")
async def get_task_record_detail(
task_record_id: str = Path(..., description="任务记录ID")
):
"""
获取指定任务记录的详细信息
Args:
task_record_id: 任务记录ID
Returns:
包含该任务记录详细信息的响应
"""
try:
result = await TaskRecordService.get_task_record_detail(task_record_id)
if not result.get("success", False):
return error_response(message=result.get("message", "任务记录不存在"), code=404)
return format_response(data=result.get("data", {}), message="成功获取任务记录详情")
except Exception as e:
logger.error(f"获取任务记录详情失败: {str(e)}")
return error_response(message=f"获取任务记录详情失败: {str(e)}", code=500)