fix(management): 期聚合active_friends按(uid,peer)去重——多账号共享客户不再双计;reply计数并入Python/单一METRIC_VERSION
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -26,7 +26,6 @@ import datetime as dt
|
||||
import statistics
|
||||
from collections import namedtuple
|
||||
from collections.abc import Iterable
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -38,11 +37,9 @@ from app.db.models import (
|
||||
MsgDailyStats,
|
||||
Staff,
|
||||
)
|
||||
from app.storage.employee_metrics import METRIC_VERSION
|
||||
from app.storage.staff_summary import account_map
|
||||
|
||||
_SH = ZoneInfo("Asia/Shanghai")
|
||||
METRIC_VERSION = "v1"
|
||||
|
||||
PeriodMetrics = namedtuple("PeriodMetrics", [
|
||||
"user_id", "days_with_data", "msg_sent", "msg_recv", "cust_threads",
|
||||
"threads_answered", "fr_sum_ms", "fr_p50_ms", "fr_p90_ms",
|
||||
@@ -226,7 +223,7 @@ async def period_metrics(
|
||||
a["cooling_customer"] = int(cool or 0)
|
||||
a["dormant_customer"] = int(dorm or 0)
|
||||
|
||||
# ---- fact 分位 + 2h/24h 回复计数(同一过滤) ----
|
||||
# ---- fact 分位 + 2h/24h 回复计数(同一过滤,Python 侧一次聚合) ----
|
||||
fr_conds = [FirstReplyFact.day.between(start_day, end_day)]
|
||||
if channel is not None:
|
||||
fr_conds.append(FirstReplyFact.channel == channel)
|
||||
@@ -244,26 +241,11 @@ async def period_metrics(
|
||||
for uid, frs in fr_by_user.items():
|
||||
acc[uid]["fr_p50_ms"] = _percentile(frs, 1, 2)
|
||||
acc[uid]["fr_p90_ms"] = _percentile(frs, 9, 10)
|
||||
# 2h/24h 计数与分位同源同窗——回复率分子分母口径一致
|
||||
acc[uid]["reply_2h_count"] = sum(1 for fr in frs if fr <= 7_200_000)
|
||||
acc[uid]["reply_24h_count"] = sum(1 for fr in frs if fr <= 86_400_000)
|
||||
|
||||
reply = (await session.execute(
|
||||
sa.select(
|
||||
FirstReplyFact.owner_account_id.label("owner"),
|
||||
sa.func.sum(sa.case(
|
||||
(FirstReplyFact.fr_ms <= 7_200_000, 1), else_=0
|
||||
)).label("c2h"),
|
||||
sa.func.sum(sa.case(
|
||||
(FirstReplyFact.fr_ms <= 86_400_000, 1), else_=0
|
||||
)).label("c24h"),
|
||||
).where(*fr_conds).group_by(FirstReplyFact.owner_account_id)
|
||||
)).all()
|
||||
for owner, c2h, c24h in reply:
|
||||
uid = amap.get(owner or "")
|
||||
if uid is None or uid not in target:
|
||||
continue
|
||||
acc[uid]["reply_2h_count"] += int(c2h or 0)
|
||||
acc[uid]["reply_24h_count"] += int(c24h or 0)
|
||||
|
||||
# ---- distinct 活跃客户:窗内 (owner, peer) 去重(friend 行) ----
|
||||
# ---- distinct 活跃客户:账号归并到员工后按 peer 去重 ----
|
||||
af_conds = [
|
||||
MsgDailyStats.day.between(start_day, end_day),
|
||||
MsgDailyStats.peer_kind == "friend",
|
||||
@@ -274,12 +256,14 @@ async def period_metrics(
|
||||
sa.select(MsgDailyStats.owner_account_id, MsgDailyStats.peer_id)
|
||||
.distinct().where(*af_conds)
|
||||
)).all()
|
||||
active: dict[str, set[tuple[str, str]]] = {}
|
||||
active: dict[str, set[str]] = {}
|
||||
for owner, peer in pairs:
|
||||
uid = amap.get(owner or "")
|
||||
if uid is None or uid not in target:
|
||||
continue
|
||||
active.setdefault(uid, set()).add((owner or "", peer or ""))
|
||||
# (uid, peer) 粒度:同一员工多账号(wx1+wx2)的共享客户只计 1
|
||||
# ——与宽表日值口径一致(storage 侧账号归并后 add(peer))
|
||||
active.setdefault(uid, set()).add(peer or "")
|
||||
for uid, pset in active.items():
|
||||
acc[uid]["active_friends"] = len(pset)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user