fix(admin): 优化聊天日志显示

This commit is contained in:
sophie committed 2026-07-13 14:35:39 +08:00
1 parent db067085e4
commit 2ebac99d43
5 files changed
+41 -10

No files matched your search

+12 -3
View File
@@ -52,10 +52,15 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
"""
聊天使用分析报表(按应用 / 按用户),限定时间范围内的 ChatLog。
核心逻辑:按 Agent / User 聚合 ChatLog 次数;按用户统计同时返回 username 与 real_name。
Args:
user_page: 按用户统计分页页码。
user_page_size: 每页条数(上限 USAGE_BY_USER_TOP_LIMIT)。
range_key: 时间范围 key,见 USAGE_RANGE_DAYS。
Returns:
含 total / by_user(username、real_name、count)/ by_agent 及用户分页字段的字典。
"""
normalized_range, since = resolve_usage_range_start(range_key)
base_filter = ChatLog.created_at >= since
@@ -71,11 +76,12 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
)
user_page = max(1, user_page)
user_page_size = max(1, min(user_page_size, USAGE_BY_USER_TOP_LIMIT))
# [STEP 1] 按用户聚合对话量,一并选出真实姓名供管理端展示
by_user_top = (
db.session.query(User.username, func.count(ChatLog.id))
db.session.query(User.username, User.real_name, func.count(ChatLog.id))
.join(ChatLog)
.filter(base_filter)
.group_by(User.username)
.group_by(User.username, User.real_name)
.order_by(desc(func.count(ChatLog.id)), User.username)
.limit(USAGE_BY_USER_TOP_LIMIT)
.all()
@@ -88,7 +94,10 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
return {
'total': total,
'range': normalized_range,
'by_user': [{'username': u, 'count': c} for u, c in by_user_page],
'by_user': [
{'username': u, 'real_name': rn or '', 'count': c}
for u, rn, c in by_user_page
],
'by_agent': [{'agent': a, 'count': c} for a, c in by_agent],
'user_total': user_total,
'user_page': user_page,