feat(account-events): 账号详情页与账号事件历史全链路支持
1. 新增账号事件数据表 AccountEvent 与迁移版本 20261002_0040_account_events,支持审计 Cookie 变更、自动重登、健康检查及任务执行事件; 2. 后端新增 account_events_service 并扩展 /api/accounts 路由,支持事件查询、过滤与上报; 3. 前端新增 AccountDetailPage 账号详情页组件,支持账号元信息展示、事件历史瀑布流与快捷重登操作; 4. 修复 CK 重登与自动化定时任务中事件关联逻辑,补充 test_account_events 单元测试; 5. 新增生产服务器高负载排查分析报告及相关设计文档。
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
"""账号管理路由"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy import func, or_, select
|
||||
from sqlalchemy import func, or_, select, text
|
||||
from sqlalchemy.orm import Session, defer, joinedload
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import get_current_user, require_permission
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountEvent,
|
||||
AuditLog,
|
||||
DouyuTask,
|
||||
DouyuWorkbenchAccount,
|
||||
@@ -24,6 +27,13 @@ from ..schemas import (
|
||||
AccountTag,
|
||||
BatchAssign,
|
||||
)
|
||||
from ..services.account_events_service import (
|
||||
EVENT_CK_INVALID,
|
||||
EVENT_ELITE_OPENED,
|
||||
EVENT_ESPORTS_OPENED,
|
||||
EVENT_EXCHANGE,
|
||||
EVENT_RECHARGE,
|
||||
)
|
||||
from ..services.account_service import (
|
||||
cookie_account_ids_query,
|
||||
parse_and_build_accounts,
|
||||
@@ -565,6 +575,168 @@ def batch_delete_accounts_selection(
|
||||
return {"message": f"已删除 {deleted} 个账号", "deleted": deleted, "success": True}
|
||||
|
||||
|
||||
@router.get("/{account_id}/overview")
|
||||
def account_overview(
|
||||
account_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""账号详情页的「状态卡 + 统计」。
|
||||
|
||||
状态取 `accounts` 的当前值(覆盖式),统计由 `account_events` 聚合 —— 这正是
|
||||
「状态与流水分离」的用法:页面上两者拼起来看。
|
||||
"""
|
||||
acc = _visible_accounts_query(db, current).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
|
||||
stats = (
|
||||
db.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT
|
||||
SUM(CASE WHEN event_type = :recharge THEN amount ELSE 0 END) AS recharge_total,
|
||||
SUM(CASE WHEN event_type = :exchange THEN 1 ELSE 0 END) AS exchange_count,
|
||||
SUM(CASE WHEN event_type = :exchange THEN -amount ELSE 0 END) AS points_spent,
|
||||
SUM(CASE WHEN event_type = :ck_invalid THEN 1 ELSE 0 END) AS ck_invalid_count,
|
||||
MIN(CASE WHEN event_type = :elite THEN occurred_at END) AS elite_opened_at,
|
||||
MIN(CASE WHEN event_type = :esports THEN occurred_at END) AS esports_opened_at,
|
||||
COUNT(*) AS event_count
|
||||
FROM account_events WHERE account_id = :aid
|
||||
"""
|
||||
),
|
||||
{
|
||||
"aid": account_id,
|
||||
"recharge": EVENT_RECHARGE,
|
||||
"exchange": EVENT_EXCHANGE,
|
||||
"ck_invalid": EVENT_CK_INVALID,
|
||||
"elite": EVENT_ELITE_OPENED,
|
||||
"esports": EVENT_ESPORTS_OPENED,
|
||||
},
|
||||
)
|
||||
.mappings()
|
||||
.first()
|
||||
or {}
|
||||
)
|
||||
|
||||
# 登录次数取历史记录(事件表不回填,所以 CK 相关计数仍以 login_tasks 为准)。
|
||||
login_stats = (
|
||||
db.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT COUNT(*) AS login_count,
|
||||
MAX(finished_at) AS last_login_at,
|
||||
MAX(ck_checked_at) AS last_ck_check_at
|
||||
FROM login_tasks
|
||||
WHERE account_id = :aid AND cookie IS NOT NULL AND cookie <> ''
|
||||
"""
|
||||
),
|
||||
{"aid": account_id},
|
||||
)
|
||||
.mappings()
|
||||
.first()
|
||||
or {}
|
||||
)
|
||||
|
||||
return {
|
||||
"id": acc.id,
|
||||
"username": acc.username,
|
||||
"nickname": acc.nickname,
|
||||
"uid": acc.uid,
|
||||
"tag": acc.tag,
|
||||
"remark": acc.remark,
|
||||
"assigned_to": acc.assigned_to,
|
||||
"created_at": acc.created_at, # 导入时间
|
||||
# 当前状态(覆盖式字段)
|
||||
"points": acc.points,
|
||||
"gold_balance": acc.gold_balance,
|
||||
"exchange_balance": acc.exchange_balance,
|
||||
"game_name": acc.game_name,
|
||||
"game_channel": acc.game_channel,
|
||||
"bind_status": acc.bind_status,
|
||||
"elite_opened": acc.elite_opened,
|
||||
"elite_expire_time": acc.elite_expire_time,
|
||||
"esports_points": acc.esports_points,
|
||||
"esports_bind_status": acc.esports_bind_status,
|
||||
"esports_game_name": acc.esports_game_name,
|
||||
"esports_can_change_time": acc.esports_can_change_time,
|
||||
"auto_managed": acc.auto_managed,
|
||||
# 统计
|
||||
"stats": {
|
||||
"recharge_total": int(stats.get("recharge_total") or 0),
|
||||
"exchange_count": int(stats.get("exchange_count") or 0),
|
||||
"points_spent": int(stats.get("points_spent") or 0),
|
||||
"ck_invalid_count": int(stats.get("ck_invalid_count") or 0),
|
||||
"event_count": int(stats.get("event_count") or 0),
|
||||
"login_count": int(login_stats.get("login_count") or 0),
|
||||
"last_login_at": login_stats.get("last_login_at"),
|
||||
"last_ck_check_at": login_stats.get("last_ck_check_at"),
|
||||
},
|
||||
# 「开通时间」由事件表提供;事件表从上线起才有,早于上线的不显示。
|
||||
"opened_at": {
|
||||
"elite": stats.get("elite_opened_at"),
|
||||
"esports": stats.get("esports_opened_at"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{account_id}/events")
|
||||
def account_events(
|
||||
account_id: int,
|
||||
page: int = 1,
|
||||
page_size: int = 50,
|
||||
event_type: str = "",
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""账号时间线(倒序分页)。
|
||||
|
||||
权限沿用账号可见性:客服只能看自己名下账号的事件。
|
||||
"""
|
||||
acc = _visible_accounts_query(db, current).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
|
||||
page = max(1, int(page))
|
||||
page_size = max(1, min(int(page_size), 200))
|
||||
where = "WHERE account_id = :aid"
|
||||
params: dict[str, Any] = {"aid": account_id}
|
||||
if event_type:
|
||||
where += " AND event_type = :etype"
|
||||
params["etype"] = event_type
|
||||
|
||||
total = (
|
||||
db.execute(
|
||||
text(f"SELECT COUNT(*) FROM account_events {where}"), params
|
||||
).scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
rows = (
|
||||
db.execute(
|
||||
text(
|
||||
f"""
|
||||
SELECT id, event_type, occurred_at, title, amount, balance_after,
|
||||
detail, source
|
||||
FROM account_events {where}
|
||||
ORDER BY occurred_at DESC, id DESC
|
||||
LIMIT :limit OFFSET :offset
|
||||
"""
|
||||
),
|
||||
{**params, "limit": page_size, "offset": (page - 1) * page_size},
|
||||
)
|
||||
.mappings()
|
||||
.all()
|
||||
)
|
||||
|
||||
return {
|
||||
"items": [dict(row) for row in rows],
|
||||
"total": int(total),
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/{account_id}")
|
||||
def delete_account(
|
||||
account_id: int,
|
||||
@@ -585,6 +757,11 @@ def delete_account(
|
||||
db.query(DouyuWorkbenchAccount).filter(
|
||||
DouyuWorkbenchAccount.account_id == account_id
|
||||
).delete(synchronize_session=False)
|
||||
# 事件表有指向 accounts 的外键,不清会直接删不掉账号。
|
||||
# 账号都没了,它的时间线也没有保留意义(审计仍在 audit_logs 里,那份不带外键)。
|
||||
db.query(AccountEvent).filter(AccountEvent.account_id == account_id).delete(
|
||||
synchronize_session=False
|
||||
)
|
||||
|
||||
db.add(
|
||||
AuditLog(
|
||||
@@ -592,6 +769,7 @@ def delete_account(
|
||||
username=current.username,
|
||||
action="account:delete",
|
||||
target=acc.username,
|
||||
account_id=account_id,
|
||||
)
|
||||
)
|
||||
db.delete(acc)
|
||||
|
||||
Reference in New Issue
Block a user