fix(ad): detach late impressions from abandoned coupons
This commit is contained in:
@@ -21,16 +21,17 @@ def attributable_trace_id(
|
||||
) -> str | None:
|
||||
"""返回广告展示允许归属的业务 trace。
|
||||
|
||||
领券任务可能在 Draw 广告异步加载完成前已经失败。失败终态先落库、广告回调后到时,
|
||||
收益仍需保留在总广告报表中,但不能再挂到该失败领券明细,因此清空关联 trace。
|
||||
其它场景、找不到 session、非 failed 状态保持原值,由客户端生命周期修复负责主防线。
|
||||
领券任务可能在 Draw 广告异步加载完成前已经失败或被用户放弃。非完成终态先落库、
|
||||
广告回调后到时,收益仍需保留在总广告报表中,但不能再挂到该死亡领券明细,
|
||||
因此清空关联 trace。其它场景、找不到 session、进行中或已完成状态保持原值,
|
||||
由客户端生命周期修复负责主防线。
|
||||
"""
|
||||
if feed_scene != "coupon" or not trace_id:
|
||||
return trace_id
|
||||
session_status = db.execute(
|
||||
select(CouponSession.status).where(CouponSession.trace_id == trace_id)
|
||||
).scalar_one_or_none()
|
||||
return None if session_status == "failed" else trace_id
|
||||
return None if session_status in {"failed", "abandoned"} else trace_id
|
||||
|
||||
|
||||
def create_ecpm_record(
|
||||
|
||||
@@ -58,19 +58,28 @@ def test_revenue_yuan_by_trace_empty() -> None:
|
||||
db.close()
|
||||
|
||||
|
||||
def test_failed_coupon_trace_is_not_attributable_to_late_impression() -> None:
|
||||
"""领券已失败后才到达的广告展示保留收益记录,但不再关联失败 trace。"""
|
||||
def test_terminal_coupon_trace_is_not_attributable_to_late_impression() -> None:
|
||||
"""领券失败或被放弃后才到达的广告展示保留收益记录,但不再关联死亡 trace。"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
db.add(CouponSession(
|
||||
trace_id="failed-before-ad", device_id="d-late-ad", status="failed", app_env="prod",
|
||||
started_at=datetime(2020, 1, 2, tzinfo=UTC), started_date=date(2020, 1, 2),
|
||||
))
|
||||
db.add_all([
|
||||
CouponSession(
|
||||
trace_id="failed-before-ad", device_id="d-late-ad", status="failed", app_env="prod",
|
||||
started_at=datetime(2020, 1, 2, tzinfo=UTC), started_date=date(2020, 1, 2),
|
||||
),
|
||||
CouponSession(
|
||||
trace_id="abandoned-before-ad", device_id="d-late-ad", status="abandoned", app_env="prod",
|
||||
started_at=datetime(2020, 1, 2, tzinfo=UTC), started_date=date(2020, 1, 2),
|
||||
),
|
||||
])
|
||||
db.flush()
|
||||
|
||||
assert crud_ecpm.attributable_trace_id(
|
||||
db, feed_scene="coupon", trace_id="failed-before-ad"
|
||||
) is None
|
||||
assert crud_ecpm.attributable_trace_id(
|
||||
db, feed_scene="coupon", trace_id="abandoned-before-ad"
|
||||
) is None
|
||||
assert crud_ecpm.attributable_trace_id(
|
||||
db, feed_scene="comparison", trace_id="failed-before-ad"
|
||||
) == "failed-before-ad"
|
||||
|
||||
Reference in New Issue
Block a user