fix(admin): 优化聊天日志显示
This commit is contained in:
1 parent
db067085e4
commit
2ebac99d43
5 files changed
+41
-10
No files matched your search
Vendored
+3
-2
@@ -1,7 +1,8 @@
|
||||
{
|
||||
// 后端 Python 虚拟环境
|
||||
// 后端 Python 虚拟环境(解释器路径供语言服务/调试使用)
|
||||
"python.defaultInterpreterPath": "${workspaceFolder}/backend/.venv/Scripts/python.exe",
|
||||
"python.terminal.activateEnvironment": true,
|
||||
// 关闭 Python 扩展在新建终端时自动 activate,避免与下方 PowerShell (HXOPS) 的 cd 钩子重复激活
|
||||
"python.terminal.activateEnvironment": false,
|
||||
"python.venvPath": "${workspaceFolder}/backend",
|
||||
"python.venvFolders": [".venv"],
|
||||
"github.copilot.chat.commitMessageGeneration.instructions": [
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -11,7 +11,13 @@ from app.services.admin import report_service
|
||||
def test_build_usage_report(app):
|
||||
with app.app_context():
|
||||
agent = Agent(name='report-agent', type='api', dify_app_id='app-r', dify_api_key='key-r')
|
||||
user = User(username='report-user', email='report@example.com', role='user')
|
||||
# 断言:by_user 需带回 real_name,便于管理端展示真实姓名
|
||||
user = User(
|
||||
username='report-user',
|
||||
email='report@example.com',
|
||||
role='user',
|
||||
real_name='报表用户',
|
||||
)
|
||||
db.session.add_all([agent, user])
|
||||
db.session.flush()
|
||||
db.session.add(ChatLog(user_id=user.id, agent_id=agent.id, query='hi', answer='ok', tokens_used=1))
|
||||
@@ -21,6 +27,7 @@ def test_build_usage_report(app):
|
||||
assert data['total'] == 1
|
||||
assert data['range'] == '30d'
|
||||
assert data['by_user'][0]['username'] == 'report-user'
|
||||
assert data['by_user'][0]['real_name'] == '报表用户'
|
||||
assert data['by_agent'][0]['agent'] == 'report-agent'
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,20 @@ import { useReportsAdmin } from './composables/useReportsAdmin';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
/**
|
||||
* 格式化使用分析「按用户统计」展示名:优先真实姓名,并附带用户名便于对照工号。
|
||||
* 核心逻辑:有 real_name 时显示「姓名(username)」,否则仅显示 username。
|
||||
* @param user - 报表 by_user 项
|
||||
* @returns 列表左侧展示文案
|
||||
*/
|
||||
function formatUsageUserLabel(user: { username: string; real_name?: string }): string {
|
||||
const realName = (user.real_name || '').trim();
|
||||
if (realName) {
|
||||
return `${realName}(${user.username})`;
|
||||
}
|
||||
return user.username;
|
||||
}
|
||||
|
||||
const {
|
||||
usageReport,
|
||||
usageRange,
|
||||
@@ -407,9 +421,9 @@ onMounted(() => {
|
||||
<p class="mt-1 text-xs text-muted">{{ t('admin.report.usageByUserTopHint', { limit: usageUserTopLimit }) }}</p>
|
||||
</div>
|
||||
<div class="space-y-3 flex-1">
|
||||
<div v-for="u in usageReport.by_user" :key="u.username" class="flex items-center justify-between p-3 bg-surface-soft rounded-xl">
|
||||
<span class="font-medium text-secondary">{{ u.username }}</span>
|
||||
<span class="admin-report-badge admin-report-badge--info font-bold">{{ u.count }}</span>
|
||||
<div v-for="u in usageReport.by_user" :key="u.username" class="flex items-center justify-between gap-3 p-3 bg-surface-soft rounded-xl">
|
||||
<span class="font-medium text-secondary truncate" :title="formatUsageUserLabel(u)">{{ formatUsageUserLabel(u) }}</span>
|
||||
<span class="admin-report-badge admin-report-badge--info font-bold shrink-0">{{ u.count }}</span>
|
||||
</div>
|
||||
<div v-if="usageReport.by_user.length === 0" class="text-center text-muted text-sm py-6">
|
||||
{{ t('admin.report.usageByUserEmpty') }}
|
||||
|
||||
@@ -12,7 +12,7 @@ export interface UsageReport {
|
||||
total: number;
|
||||
/** 当前统计时间范围 */
|
||||
range?: UsageRangeKey;
|
||||
by_user: { username: string; count: number }[];
|
||||
by_user: { username: string; real_name?: string; count: number }[];
|
||||
by_agent: { agent: string; count: number }[];
|
||||
by_day?: { date: string; count: number }[];
|
||||
user_total: number;
|
||||
|
||||
Reference in new issue
Block a user