fix(dashboard): 复用智能体仓库查询名称

通过 AgentRepository 封装仪表盘智能体名称映射查询,避免在路由中直接筛选 Agent 表。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-06-09 08:27:43 +08:00
parent 5a9827b862
commit 321eab2ea1
2 changed files with 11 additions and 6 deletions

View File

@ -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):

View File

@ -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 = {}