Merge branch 'dev' into hugo-shipping-notice
This commit is contained in:
commit
dde25344ca
9 files changed
+110
-19
No files matched your search
@@ -30,6 +30,10 @@ APPLICATION_REPORT_SORT_KEYS = frozenset({
|
||||
'tokens_used',
|
||||
})
|
||||
DEFAULT_APPLICATION_SORT = 'chat_count'
|
||||
# 运营提醒:启用应用在范围内对话次数 ≤ 该阈值视为低使用(含零使用)
|
||||
USAGE_LOW_AGENT_MAX_CHATS = 2
|
||||
# 运营提醒:低使用应用最多返回条数
|
||||
USAGE_LOW_AGENT_LIST_LIMIT = 5
|
||||
|
||||
# 使用分析时间范围:key → 回溯天数(与前端下拉选项一致)
|
||||
USAGE_RANGE_DAYS = {
|
||||
@@ -65,7 +69,8 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
|
||||
"""
|
||||
聊天使用分析报表(按应用 / 按用户),限定时间范围内的 ChatLog。
|
||||
|
||||
核心逻辑:按 Agent / User 聚合 ChatLog 次数;按用户统计同时返回 username 与 real_name。
|
||||
核心逻辑:按 Agent / User 聚合 ChatLog 次数;按用户统计同时返回 username 与 real_name;
|
||||
另对启用 Agent 左连接对话量,产出含零使用的低使用应用列表(与应用运营口径对齐)。
|
||||
|
||||
Args:
|
||||
user_page: 按用户统计分页页码。
|
||||
@@ -73,7 +78,7 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
|
||||
range_key: 时间范围 key,见 USAGE_RANGE_DAYS。
|
||||
|
||||
Returns:
|
||||
含 total / by_user(username、real_name、count)/ by_agent 及用户分页字段的字典。
|
||||
含 total / by_user / by_agent / by_day / low_agents 及用户分页字段的字典。
|
||||
"""
|
||||
normalized_range, since = resolve_usage_range_start(range_key)
|
||||
base_filter = ChatLog.created_at >= since
|
||||
@@ -95,9 +100,29 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
|
||||
.order_by(asc(func.date(ChatLog.created_at)))
|
||||
.all()
|
||||
)
|
||||
# [STEP 2] 低使用应用:以启用 Agent 为主表左连接范围内对话量,含零使用(by_agent 只有有对话的应用)
|
||||
agent_chat_counts = (
|
||||
db.session.query(
|
||||
ChatLog.agent_id.label('agent_id'),
|
||||
func.count(ChatLog.id).label('chat_count'),
|
||||
)
|
||||
.filter(base_filter)
|
||||
.group_by(ChatLog.agent_id)
|
||||
.subquery()
|
||||
)
|
||||
chat_count_col = func.coalesce(agent_chat_counts.c.chat_count, 0)
|
||||
low_agents = (
|
||||
db.session.query(Agent.name, chat_count_col.label('count'))
|
||||
.outerjoin(agent_chat_counts, agent_chat_counts.c.agent_id == Agent.id)
|
||||
.filter(Agent.is_active.is_(True))
|
||||
.filter(chat_count_col <= USAGE_LOW_AGENT_MAX_CHATS)
|
||||
.order_by(asc(chat_count_col), Agent.name)
|
||||
.limit(USAGE_LOW_AGENT_LIST_LIMIT)
|
||||
.all()
|
||||
)
|
||||
user_page = max(1, user_page)
|
||||
user_page_size = max(1, min(user_page_size, USAGE_BY_USER_TOP_LIMIT))
|
||||
# [STEP 1] 按用户聚合对话量,一并选出真实姓名供管理端展示
|
||||
# [STEP 3] 按用户聚合对话量,一并选出真实姓名供管理端展示
|
||||
by_user_top = (
|
||||
db.session.query(User.username, User.real_name, func.count(ChatLog.id))
|
||||
.join(ChatLog)
|
||||
@@ -121,6 +146,9 @@ def build_usage_report(*, user_page: int, user_page_size: int, range_key: str |
|
||||
],
|
||||
'by_agent': [{'agent': a, 'count': c} for a, c in by_agent],
|
||||
'by_day': [{'date': str(day), 'count': c} for day, c in by_day],
|
||||
'low_agents': [{'agent': a, 'count': int(c)} for a, c in low_agents],
|
||||
'low_agent_max_chats': USAGE_LOW_AGENT_MAX_CHATS,
|
||||
'low_agent_list_limit': USAGE_LOW_AGENT_LIST_LIMIT,
|
||||
'user_total': user_total,
|
||||
'user_page': user_page,
|
||||
'user_page_size': user_page_size,
|
||||
|
||||
Reference in new issue
Block a user