From d2ed246a4ca44ad727f6d812e8614b79aa2a8e1d Mon Sep 17 00:00:00 2001 From: Code AI Agent Date: Fri, 27 Mar 2026 12:49:28 +0000 Subject: [PATCH] feat: add stats API endpoints (overview, by-template, by-owner) --- patch_stats.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ stats_patch.py | 24 +++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 patch_stats.py create mode 100644 stats_patch.py diff --git a/patch_stats.py b/patch_stats.py new file mode 100644 index 0000000..1751c7c --- /dev/null +++ b/patch_stats.py @@ -0,0 +1,52 @@ +patch_code = ''' + +# ==================== 统计 API ==================== + +@app.get("/stats/overview") +async def get_stats_overview(db: Session = Depends(get_db)): + from sqlalchemy import func as sqlfunc + total_agents = db.query(sqlfunc.count(Agent.id)).scalar() + running_agents = db.query(sqlfunc.count(Agent.id)).filter(Agent.status == AgentStatus.RUNNING).scalar() + stopped_agents = db.query(sqlfunc.count(Agent.id)).filter(Agent.status == AgentStatus.STOPPED).scalar() + total_templates = db.query(sqlfunc.count(Agent.template_id.distinct())).scalar() + return {"total_agents": total_agents, "running_agents": running_agents, "stopped_agents": stopped_agents, "total_templates": total_templates} + +@app.get("/stats/by-template") +async def get_stats_by_template(db: Session = Depends(get_db)): + from sqlalchemy import func as sqlfunc + result = db.query( + Template.name, + sqlfunc.count(Agent.id).label("agent_count"), + sqlfunc.sum(Agent.current_replicas).label("total_replicas") + ).outerjoin(Template, Agent.template_id == Template.id).group_by(Template.name).all() + return [{"template_name": r[0], "agent_count": r[1], "total_replicas": r[2] or 0} for r in result] + +@app.get("/stats/by-owner") +async def get_stats_by_owner(db: Session = Depends(get_db)): + from sqlalchemy import func as sqlfunc + agents = db.query(Agent).all() + owner_map = {} + for a in agents: + oid = a.owner_id + if oid not in owner_map: + owner_map[oid] = {"owner_id": oid, "agent_count": 0, "total_replicas": 0, "platform_agents": 0, "custom_agents": 0} + owner_map[oid]["agent_count"] += 1 + owner_map[oid]["total_replicas"] += a.current_replicas or 0 + if a.agent_type == AgentType.PLATFORM: + owner_map[oid]["platform_agents"] += 1 + else: + owner_map[oid]["custom_agents"] += 1 + return list(owner_map.values()) + +''' + +import re +path = '/home/xiaohei/agent_management/app.py' +with open(path, 'r') as f: + content = f.read() + +content = re.sub(r'\n# ==================== 统计 API ====================.*?(?=\nif __name__)', '', content, flags=re.DOTALL) +content = content.replace('if __name__ == "__main__":', patch_code + 'if __name__ == "__main__":') +with open(path, 'w') as f: + f.write(content) +print('patched ok') diff --git a/stats_patch.py b/stats_patch.py new file mode 100644 index 0000000..03b92df --- /dev/null +++ b/stats_patch.py @@ -0,0 +1,24 @@ + + +# ==================== 统计 API ==================== + +@app.get("/stats/overview") +async def get_stats_overview(db: Session = Depends(get_db)): + from sqlalchemy import func as sqlfunc + total_agents = db.query(sqlfunc.count(Agent.id)).scalar() + running_agents = db.query(sqlfunc.count(Agent.id)).filter(Agent.status == AgentStatus.running).scalar() + stopped_agents = db.query(sqlfunc.count(Agent.id)).filter(Agent.status == AgentStatus.stopped).scalar() + total_templates = db.query(sqlfunc.count(Agent.template_name.distinct())).scalar() + return {"total_agents": total_agents, "running_agents": running_agents, "stopped_agents": stopped_agents, "total_templates": total_templates} + +@app.get("/stats/by-template") +async def get_stats_by_template(db: Session = Depends(get_db)): + from sqlalchemy import func as sqlfunc + result = db.query(Agent.template_name, sqlfunc.count(Agent.id).label("agent_count"), sqlfunc.sum(Agent.current_replicas).label("total_replicas")).group_by(Agent.template_name).all() + return [{"template_name": r[0], "agent_count": r[1], "total_replicas": r[2] or 0} for r in result] + +@app.get("/stats/by-owner") +async def get_stats_by_owner(db: Session = Depends(get_db)): + from sqlalchemy import func as sqlfunc + result = db.query(Agent.owner_id, sqlfunc.count(Agent.id).label("agent_count"), sqlfunc.sum(Agent.current_replicas).label("total_replicas")).group_by(Agent.owner_id).all() + return [{"owner_id": r[0], "agent_count": r[1], "total_replicas": r[2] or 0} for r in result]