refactor: 移除不必要的监控和健康检查接口,简化代码结构,增强API的可读性和维护性

This commit is contained in:
iHeyTang
2025-08-19 15:07:31 +08:00
parent c0354b35d2
commit 0d8d9ffc4e
3 changed files with 86 additions and 366 deletions

View File

@@ -1,12 +1,13 @@
import json
from typing import Dict, Optional
from typing import Dict, Optional, List, Any
from datetime import datetime, timedelta
from fastapi import APIRouter, Body, HTTPException
from fastapi.responses import JSONResponse
from workflow_service.comfy import comfy_workflow
from workflow_service.comfy.comfy_queue import queue_manager
from workflow_service.database.api import get_workflow
from workflow_service.database.api import get_workflow, get_workflow_runs_recent
run_router = APIRouter(
prefix="/api/run",
@@ -62,6 +63,72 @@ async def run_workflow(
raise HTTPException(status_code=500, detail=f"提交工作流失败: {str(e)}")
@run_router.get("")
async def get_runs(
limit: int = 10, status: Optional[str] = None
) -> List[Dict[str, Any]]:
"""获取运行列表,支持分页和状态过滤"""
try:
end_time = datetime.now()
start_time = end_time - timedelta(hours=24)
recent_runs = await get_workflow_runs_recent(start_time, end_time)
# 如果指定了状态过滤
if status:
recent_runs = [run for run in recent_runs if run.get("status") == status]
# 限制返回数量
limited_runs = recent_runs[:limit]
# 格式化返回数据
formatted_runs = []
for run in limited_runs:
formatted_runs.append(
{
"id": run.get("id"), # 使用数据库中的id字段
"workflow_name": run.get("workflow_name"),
"status": run.get("status", "unknown"),
"created_at": run.get("created_at"),
"updated_at": run.get("updated_at"),
"api_spec": run.get("api_spec"),
}
)
return formatted_runs
except Exception as e:
raise HTTPException(status_code=500, detail=f"获取运行列表失败: {str(e)}")
@run_router.get("/metrics")
async def get_run_metrics() -> Dict[str, Any]:
"""获取运行概览统计信息"""
try:
# 获取最近24小时的任务统计
end_time = datetime.now()
start_time = end_time - timedelta(hours=24)
recent_runs = await get_workflow_runs_recent(start_time, end_time)
# 统计各种状态的任务数量
status_counts = {}
for run in recent_runs:
status = run.get("status", "unknown")
status_counts[status] = status_counts.get(status, 0) + 1
return {
"running_tasks": status_counts.get("running", 0),
"pending_tasks": status_counts.get("pending", 0),
"completed_tasks": status_counts.get("completed", 0),
"failed_tasks": status_counts.get("failed", 0),
"total_tasks_24h": len(recent_runs),
"timestamp": datetime.now().isoformat(),
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"获取运行概览失败: {str(e)}")
@run_router.get("/{workflow_run_id}")
async def get_run_status(workflow_run_id: str):
"""