From 321eab2ea14cb46a899c5b439b8e614d91628aca Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 9 Jun 2026 08:27:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(dashboard):=20=E5=A4=8D=E7=94=A8=E6=99=BA?= =?UTF-8?q?=E8=83=BD=E4=BD=93=E4=BB=93=E5=BA=93=E6=9F=A5=E8=AF=A2=E5=90=8D?= =?UTF-8?q?=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过 AgentRepository 封装仪表盘智能体名称映射查询,避免在路由中直接筛选 Agent 表。 Co-Authored-By: Claude Opus 4.7 --- .../package/yuxi/repositories/agent_repository.py | 4 ++++ backend/server/routers/dashboard_router.py | 13 +++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/backend/package/yuxi/repositories/agent_repository.py b/backend/package/yuxi/repositories/agent_repository.py index 716e82f5..43107a86 100644 --- a/backend/package/yuxi/repositories/agent_repository.py +++ b/backend/package/yuxi/repositories/agent_repository.py @@ -349,6 +349,10 @@ class AgentRepository: result = await self.db.execute(select(Agent).where(Agent.slug == slug)) return result.scalar_one_or_none() + async def list_by_slugs(self, slugs: list[str]) -> list[Agent]: + result = await self.db.execute(select(Agent).where(Agent.slug.in_(slugs))) + return list(result.scalars().all()) + async def get_visible_by_slug(self, *, slug: str, user: User, include_subagents: bool = False) -> Agent | None: agent = await self.get_by_slug(slug) if not agent or (agent.is_subagent and not include_subagents): diff --git a/backend/server/routers/dashboard_router.py b/backend/server/routers/dashboard_router.py index 82db7072..bb30cc7e 100644 --- a/backend/server/routers/dashboard_router.py +++ b/backend/server/routers/dashboard_router.py @@ -16,6 +16,7 @@ from sqlalchemy import Integer, String, cast, distinct, func, select, text from sqlalchemy.ext.asyncio import AsyncSession from server.utils.auth_middleware import get_admin_user, get_db +from yuxi.repositories.agent_repository import AgentRepository from yuxi.repositories.conversation_repository import ConversationRepository from yuxi.storage.postgres.models_business import User from yuxi.utils.datetime_utils import UTC, ensure_shanghai, shanghai_now, utc_now @@ -487,7 +488,7 @@ async def get_agent_analytics( ): """获取智能体分析(管理员权限)""" try: - from yuxi.storage.postgres.models_business import Agent, Conversation, Message, MessageFeedback, ToolCall + from yuxi.storage.postgres.models_business import Conversation, Message, MessageFeedback, ToolCall # 获取所有智能体 agents_result = await db.execute( @@ -561,8 +562,8 @@ async def get_agent_analytics( agent_slugs = [agent_id for agent_id, _ in agents if agent_id] agent_names = {} if agent_slugs: - agent_names_result = await db.execute(select(Agent.slug, Agent.name).where(Agent.slug.in_(agent_slugs))) - agent_names = {slug: name for slug, name in agent_names_result.all()} + agent_repo = AgentRepository(db) + agent_names = {agent.slug: agent.name for agent in await agent_repo.list_by_slugs(agent_slugs)} return AgentAnalytics( total_agents=total_agents, @@ -738,7 +739,7 @@ async def get_call_timeseries_stats( ): """获取调用分析时间序列统计(管理员权限)""" try: - from yuxi.storage.postgres.models_business import Agent, Conversation, Message, ToolCall + from yuxi.storage.postgres.models_business import Conversation, Message, ToolCall # 计算时间范围(使用北京时间 UTC+8) now = utc_now() @@ -900,8 +901,8 @@ async def get_call_timeseries_stats( if type == "agents" and categories: agent_slugs = [c for c in categories if c] if agent_slugs: - result = await db.execute(select(Agent.slug, Agent.name).where(Agent.slug.in_(agent_slugs))) - agent_names = {slug: name for slug, name in result.all()} + agent_repo = AgentRepository(db) + agent_names = {agent.slug: agent.name for agent in await agent_repo.list_by_slugs(agent_slugs)} # 重新组织数据:按时间点分组每个类别的数据 time_data = {}