fix(admin): admin管理页面优化,增加排序过滤等
This commit is contained in:
1 parent
0939aeb0d8
commit
fc843905d1
31 files changed
+1090
-671
No files matched your search
@@ -8,7 +8,7 @@ backend/app/services/admin/report_service.py
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import desc, func
|
||||
@@ -18,13 +18,53 @@ from app.models import Agent, ChatLog, Feedback, GeneralFeedback, User
|
||||
|
||||
USAGE_BY_USER_TOP_LIMIT = 50
|
||||
|
||||
# 使用分析时间范围:key → 回溯天数(与前端下拉选项一致)
|
||||
USAGE_RANGE_DAYS = {
|
||||
'7d': 7,
|
||||
'30d': 30,
|
||||
'90d': 90,
|
||||
'180d': 180,
|
||||
'365d': 365,
|
||||
}
|
||||
DEFAULT_USAGE_RANGE = '30d'
|
||||
|
||||
def build_usage_report(*, user_page: int, user_page_size: int) -> dict:
|
||||
"""聊天使用分析报表。"""
|
||||
total = db.session.query(ChatLog).count()
|
||||
|
||||
def resolve_usage_range_start(range_key: str | None) -> tuple[str, datetime]:
|
||||
"""
|
||||
解析使用分析时间范围,返回规范化 key 与起始 UTC 时间。
|
||||
|
||||
Args:
|
||||
range_key: 前端传入的 range(7d/30d/90d/180d/365d),非法值回退近一月。
|
||||
|
||||
Returns:
|
||||
(normalized_key, since_utc)
|
||||
"""
|
||||
key = (range_key or DEFAULT_USAGE_RANGE).strip().lower()
|
||||
days = USAGE_RANGE_DAYS.get(key)
|
||||
if days is None:
|
||||
key = DEFAULT_USAGE_RANGE
|
||||
days = USAGE_RANGE_DAYS[key]
|
||||
since = datetime.utcnow() - timedelta(days=days)
|
||||
return key, since
|
||||
|
||||
|
||||
def build_usage_report(*, user_page: int, user_page_size: int, range_key: str | None = None) -> dict:
|
||||
"""
|
||||
聊天使用分析报表(按应用 / 按用户),限定时间范围内的 ChatLog。
|
||||
|
||||
Args:
|
||||
user_page: 按用户统计分页页码。
|
||||
user_page_size: 每页条数(上限 USAGE_BY_USER_TOP_LIMIT)。
|
||||
range_key: 时间范围 key,见 USAGE_RANGE_DAYS。
|
||||
"""
|
||||
normalized_range, since = resolve_usage_range_start(range_key)
|
||||
base_filter = ChatLog.created_at >= since
|
||||
|
||||
total = db.session.query(ChatLog).filter(base_filter).count()
|
||||
by_agent = (
|
||||
db.session.query(Agent.name, func.count(ChatLog.id))
|
||||
.join(ChatLog)
|
||||
.filter(base_filter)
|
||||
.group_by(Agent.name)
|
||||
.order_by(desc(func.count(ChatLog.id)), Agent.name)
|
||||
.all()
|
||||
@@ -34,6 +74,7 @@ def build_usage_report(*, user_page: int, user_page_size: int) -> dict:
|
||||
by_user_top = (
|
||||
db.session.query(User.username, func.count(ChatLog.id))
|
||||
.join(ChatLog)
|
||||
.filter(base_filter)
|
||||
.group_by(User.username)
|
||||
.order_by(desc(func.count(ChatLog.id)), User.username)
|
||||
.limit(USAGE_BY_USER_TOP_LIMIT)
|
||||
@@ -46,6 +87,7 @@ def build_usage_report(*, user_page: int, user_page_size: int) -> dict:
|
||||
by_user_page = by_user_top[offset:offset + user_page_size]
|
||||
return {
|
||||
'total': total,
|
||||
'range': normalized_range,
|
||||
'by_user': [{'username': u, 'count': c} for u, c in by_user_page],
|
||||
'by_agent': [{'agent': a, 'count': c} for a, c in by_agent],
|
||||
'user_total': user_total,
|
||||
|
||||
Reference in new issue
Block a user