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:
yml2213
2026-10-03 12:58:58 +08:00
parent 294c6cd058
commit 834bfdd1d0
29 changed files with 2321 additions and 1934 deletions
+37
View File
@@ -6,6 +6,7 @@ from datetime import UTC, datetime
from sqlalchemy import (
JSON,
BigInteger,
Boolean,
ForeignKey,
Integer,
@@ -690,11 +691,47 @@ class AuditLog(Base):
action: Mapped[str] = mapped_column(String(128), nullable=False)
target: Mapped[str] = mapped_column(String(256), default="")
detail: Mapped[str] = mapped_column(Text, default="")
# 账号相关操作用它关联到具体账号(旧记录为 NULL)。
# target 是自由文本(用户名/批次号/整句话),不能可靠反查账号,所以单独存 id。
account_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
# 旧记录未标记结果时为 NULL,避免把历史操作误判成失败。
success: Mapped[bool | None] = mapped_column(Boolean, nullable=True, index=True)
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
class AccountEvent(Base):
"""账号事件流(append-only)。
`accounts` 存**当前状态**,这张表存**发生过什么** —— 账号详情页的时间线就读它。
状态字段都是覆盖式更新(积分、余额、开通标记),事件表补上「什么时候变成这样」。
设计约定:
- `occurred_at` 是**业务发生时间**(不是入库时间);
- `amount` 有符号(充值为正、兑换消耗为负),便于页面直接算累计;
- `source_ref` 是幂等键,可空且唯一:收集器重跑、事件重复发射都靠它兜住;
- `detail` 只存精简字段,不放原始大 JSON(那是 douyu_tasks 的包袱)。
"""
__tablename__ = "account_events"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
# 不单独建 account_id 索引:迁移建的是 (account_id, occurred_at) 复合索引,
# 前缀已覆盖按账号查,再单建一个纯属冗余。
account_id: Mapped[int] = mapped_column(ForeignKey("accounts.id"), nullable=False)
event_type: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
occurred_at: Mapped[datetime] = mapped_column(UTCDateTime, nullable=False)
title: Mapped[str] = mapped_column(String(256), default="", server_default="")
amount: Mapped[int | None] = mapped_column(Integer, nullable=True)
balance_after: Mapped[int | None] = mapped_column(Integer, nullable=True)
detail: Mapped[dict | None] = mapped_column(JSON, nullable=True)
source: Mapped[str] = mapped_column(String(24), default="", server_default="")
# 幂等键:可空且唯一(MySQL 允许多个 NULL,便于「没有幂等键」的事件共存)
source_ref: Mapped[str | None] = mapped_column(
String(96), nullable=True, index=True, unique=True
)
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
class AutomationJob(Base):
"""自动化作业定义。状态落库,进程重启不丢调度进度。"""