fix(analytics): recompute_all 全表清空自愈+whatsapp/多日/空日测试+映射缓存

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-04 16:28:39 +08:00
parent e3bb97bdf3
commit 5b6a9bd35b
2 changed files with 106 additions and 4 deletions
+14 -3
View File
@@ -75,9 +75,12 @@ async def recompute_days(
去重),写 SQL 反而绕且踩 only_full_group_by。
"""
n = 0
# 映射按渠道取一次缓存(而非 day×channel 每轮重查)——全量重建时
# ~600 次重复小表全查收敛为每渠道 1 次,且整个重建用同一份映射快照。
maps = {ch: await account_map(session, ch) for ch in channels}
for day in sorted(set(days)):
for channel in channels:
amap = await account_map(session, channel)
amap = maps[channel]
rows = (
await session.execute(
sa.select(
@@ -106,7 +109,10 @@ async def recompute_days(
)
a["sent"] += int(s or 0)
a["recv"] += int(r or 0)
(a["friends"] if kind == "friend" else a["rooms"]).add(peer)
if kind == "friend": # 显式分支:未来新 kind 跳过,不误入 rooms
a["friends"].add(peer)
elif kind == "room":
a["rooms"].add(peer)
for uid, a in sorted(agg.items()):
session.add(
StatDailyStaff(
@@ -124,7 +130,12 @@ async def recompute_days(
async def recompute_all(session: AsyncSession) -> int:
"""全量重建(夜间自愈 / 部署回填,幂等)。"""
"""全量重建(夜间自愈 / 部署回填,幂等)。
一次清空保证 msg_daily_stats 中已消失的日期不留陈旧行(夜间
summary.recompute_all 清空重建 msg_daily_stats 后,本层同步自愈)。
"""
await session.execute(sa.delete(StatDailyStaff))
days = (
await session.execute(sa.select(MsgDailyStats.day).distinct())
).scalars().all()