feat(backend): F1-6 sprint1 sandbox_replay 模块脚手架 + get_last_visit_days 迁移试点 (W1)
F1-5b 完成后启动 F1-6 沙箱时光机阶段 B,Sprint 1 范围: 1. 建立 sandbox_replay 模块脚手架 2. 实现 @runtime_aware decorator(自动注入 RuntimeContext) 3. 试点迁移 1 个指标:get_last_visit_days(P1-4 距上次到店天数) 4. MCP 端到端 4a/4b 双口径走查 新增文件: - apps/backend/app/services/sandbox_replay/__init__.py(模块入口 + re-export) - apps/backend/app/services/sandbox_replay/_decorator.py(@runtime_aware 实现) - apps/backend/app/services/sandbox_replay/consumption_replay.py(get_last_visit_days 试点) - docs/_overview/wave1-findings/F1-6-tasks.md(F1-6 任务清单 4 个 sprint) - docs/audit/changes/2026-05-05__f1_6_sprint1_sandbox_replay_kickoff.md(Sprint 1 审计) 修改: - apps/backend/app/services/fdw_queries.py:218-238 get_last_visit_days 改 thin wrapper,委托 sandbox_replay.consumption_replay 保持 75+ 现有调用点无感兼容 关键 bug 修复:@trace_service + @runtime_aware 嵌套 sig.bind 失败 - 现象:仅 FastAPI 请求 + 有活跃 TraceContext 时复现 (直接 Python 脚本 get_current_trace 返回 None,跳过 _build_params_dict) - 根因:functools.wraps 设置 __wrapped__ 让 inspect.signature 追溯原函数 → bind 时缺 keyword-only 必传 ctx → TypeError - 修复:_decorator.py 不用 functools.wraps,手动复制元信息但不设 __wrapped__,inspect.signature 看到 wrapper 自身 (*args, **kwargs) 测试: - unit test 10/10 PASS(本地 .gitignore:71 不入仓) - 直接 customer_service.get_customer_detail PASS(独立诊断脚本) - etl_conn 复用模式 PASS MCP 双口径(member=2799207087163141 黄先生): - 4a live (today=2026-05-05): daysSinceVisit=32(05-05 - 04-03) - 4b sandbox=2026-04-20: daysSinceVisit=31(04-20 - 03-20) 通过插 walkthrough 测试快照 stat_date=2026-04-15 演示 (测试库 dws_member_consumption_summary 仅 stat_date=2026-05-01 一行, sandbox=4-20 时被视图层 stat_date <= business_date_now() 过滤) - 测试快照已清理,sandbox 已切回 live Sprint 2-4 待启动(见 F1-6-tasks.md): - Sprint 2:5 个会员 P1 指标(余额/60d 消费/累计交易/GMV/累计服务客户数) - Sprint 3:5 个助教/门店 P1 + MP-2 完整(含 ETL 改造) - Sprint 4:5 个 P2 算法重算指标 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
42
apps/backend/app/services/sandbox_replay/__init__.py
Normal file
42
apps/backend/app/services/sandbox_replay/__init__.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""沙箱时光机重算引擎 (Sandbox Replay Engine)
|
||||
|
||||
F1-6 沙箱时光机阶段 B 启动模块。
|
||||
|
||||
设计意图:
|
||||
P20 沙箱(`runtime_mode='sandbox' + sandbox_business_date`)的"时光机"语义
|
||||
要求所有业务指标按 sandbox_business_date 重新累计/截断,而非展示 ETL
|
||||
跑批的最新累计。F1-5a/F1-5b 已建立基础设施(RuntimeContext + app 视图
|
||||
业务日上界),本模块统一所有"runtime-aware"业务读取路径。
|
||||
|
||||
完整 spec:
|
||||
docs/_overview/sandbox-replay-engine-spec.md
|
||||
|
||||
模块结构(随 sprint 推进逐步填充):
|
||||
sandbox_replay/
|
||||
├── __init__.py # 本文件,re-export 主要 API
|
||||
├── _decorator.py # @runtime_aware decorator
|
||||
├── consumption_replay.py # 消费/到店/累计交易(P1-2/3/4/12/13)
|
||||
├── balance_replay.py # 会员余额(P1-1)— sprint 2
|
||||
├── assistant_metrics_replay.py # 助教课时/收入/客户(P1-5/6/7/8)— sprint 3
|
||||
├── salary_replay.py # MP-2 完整 daily salary(P2-17)— sprint 3
|
||||
├── adjustments_replay.py # Excel 修正截断(P2-19)— sprint 3
|
||||
├── tasks_replay.py # 任务完成率(P2-18)— sprint 4
|
||||
├── rs_replay.py # RS 算法重算(P2-15)— sprint 4
|
||||
└── intimacy_replay.py # 客户黏性(P2-16)— sprint 4
|
||||
|
||||
接口契约(sprint 1):
|
||||
@runtime_aware(metric='last_visit_days')
|
||||
def get_last_visit_days(conn, site_id: int, member_ids: list[int]) -> dict[int, int | None]:
|
||||
'''函数体内自动接收 RuntimeContext,可读 ctx.is_sandbox / ctx.business_date。'''
|
||||
...
|
||||
"""
|
||||
|
||||
from app.services.sandbox_replay._decorator import runtime_aware
|
||||
from app.services.sandbox_replay.consumption_replay import (
|
||||
get_last_visit_days as get_last_visit_days_replay,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"runtime_aware",
|
||||
"get_last_visit_days_replay",
|
||||
]
|
||||
Reference in New Issue
Block a user