94 lines
2.5 KiB
Python
94 lines
2.5 KiB
Python
"""
|
|
系统状态 API
|
|
"""
|
|
from fastapi import APIRouter, HTTPException
|
|
from typing import Dict, Any
|
|
from app.utils.logger import logger
|
|
from app.utils.system_status import get_system_monitor
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/status", response_model=Dict[str, Any])
|
|
async def get_system_status():
|
|
"""
|
|
获取系统状态
|
|
|
|
返回所有 Agent 的运行状态和系统信息
|
|
"""
|
|
try:
|
|
monitor = get_system_monitor()
|
|
summary = monitor.get_summary()
|
|
|
|
# 添加额外的系统信息
|
|
response = {
|
|
"status": "success",
|
|
"data": summary
|
|
}
|
|
|
|
return response
|
|
|
|
except Exception as e:
|
|
logger.error(f"获取系统状态失败: {e}")
|
|
raise HTTPException(status_code=500, detail=f"获取系统状态失败: {str(e)}")
|
|
|
|
|
|
@router.get("/status/summary")
|
|
async def get_status_summary():
|
|
"""
|
|
获取系统状态摘要
|
|
|
|
返回简化的状态信息,用于快速检查
|
|
"""
|
|
try:
|
|
monitor = get_system_monitor()
|
|
summary = monitor.get_summary()
|
|
|
|
return {
|
|
"status": "success",
|
|
"data": {
|
|
"total_agents": summary["total_agents"],
|
|
"running_agents": summary["running_agents"],
|
|
"error_agents": summary["error_agents"],
|
|
"uptime_seconds": summary["uptime_seconds"],
|
|
"agents": {
|
|
agent_id: {
|
|
"name": info["name"],
|
|
"status": info["status"]
|
|
}
|
|
for agent_id, info in summary["agents"].items()
|
|
}
|
|
}
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"获取状态摘要失败: {e}")
|
|
raise HTTPException(status_code=500, detail=f"获取状态摘要失败: {str(e)}")
|
|
|
|
|
|
@router.get("/status/agents/{agent_id}")
|
|
async def get_agent_status(agent_id: str):
|
|
"""
|
|
获取指定 Agent 的详细状态
|
|
|
|
Args:
|
|
agent_id: Agent ID (如: crypto_agent, stock_agent)
|
|
"""
|
|
try:
|
|
monitor = get_system_monitor()
|
|
agent_info = monitor.get_agent_status(agent_id)
|
|
|
|
if agent_info is None:
|
|
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' 不存在")
|
|
|
|
return {
|
|
"status": "success",
|
|
"data": agent_info.to_dict()
|
|
}
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"获取 Agent 状态失败: {e}")
|
|
raise HTTPException(status_code=500, detail=f"获取 Agent 状态失败: {str(e)}")
|