perf(fulfillment): 优化能力刷新候选查询并新增精英能力投影

This commit is contained in:
yml
2026-11-29 13:06:54 +08:00
parent 2ea105a924
commit be7146a9ef
12 changed files with 548 additions and 54 deletions
@@ -671,3 +671,64 @@ def test_multi_commodity_refresh_handles_missing_and_due_states(db):
now=NOW,
)
assert {row.id for row in selected} == {missing_one.id, due_one.id}
def test_capability_refresh_tier0_fast_path_and_fallback_order(db):
from web.backend.services.platform_fulfillment.account_selector import (
capability_refresh_candidate_accounts,
)
user = User(
username="cap-tier-order",
password_hash="h",
role="super_admin",
is_active=True,
)
db.add(user)
db.flush()
t0_never_queried = _account(db, "cap-t0-null")
t0_never_queried.overview_queried_at = None
t0_older = _account(db, "cap-t0-older")
t0_older.overview_queried_at = NOW - timedelta(days=3)
t1_bound = _account(db, "cap-t1-bound")
t1_bound.role_bind_status = "bound"
t2_leased = _account(db, "cap-t2-leased")
db.flush()
db.add(
AccountLease(
account_id=t2_leased.id,
holder_user_id=user.id,
scope="elite",
purpose="platform_fulfillment",
status="active",
)
)
for acc in (t0_never_queried, t0_older, t1_bound, t2_leased):
_state(db, acc, commodity_id="C1", valid_until=NOW - timedelta(minutes=1))
db.commit()
# limit=2 时直接由 Tier-0 命中返回,顺序严格为 never_queried -> older
top2 = capability_refresh_candidate_accounts(
db,
commodity_ids=["C1"],
scope="elite",
limit=2,
now=NOW,
)
assert [row.id for row in top2] == [t0_never_queried.id, t0_older.id]
# limit=4 时自动回退补齐 bound 与 leased 档,全局顺序不变
all4 = capability_refresh_candidate_accounts(
db,
commodity_ids=["C1"],
scope="elite",
limit=4,
now=NOW,
)
assert [row.id for row in all4] == [
t0_never_queried.id,
t0_older.id,
t1_bound.id,
t2_leased.id,
]
+77
View File
@@ -938,3 +938,80 @@ def test_valid_ck_older_than_24h_is_still_a_sync_candidate(db):
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
assert result.queued == 1
def test_elite_capability_projection_lifecycle_and_global_totals(db):
from web.backend.services.account_capability_service import (
invalidate_account_commodity_states,
mark_commodity_exchanged,
record_account_commodity_state,
sync_account_goods_states,
)
acc_sync = _account("proj-sync", points=400)
acc_limited = _account("proj-limited", points=0)
db.add_all([acc_sync, acc_limited])
db.commit()
sync_account_goods_states(
db,
acc_sync,
[
{"commodityId": "C1", "status": 0, "storeStatus": 1, "count": 5},
{"commodityId": "C2", "status": 1, "storeStatus": 1, "count": 5},
],
scope="elite",
observed_at=NOW,
)
record_account_commodity_state(
db,
acc_limited,
commodity_id="C1",
availability="available",
observed_at=NOW,
scope="elite",
)
db.commit()
db.refresh(acc_sync)
db.refresh(acc_limited)
assert acc_sync.elite_covered is True
assert acc_sync.elite_eligible_until is not None
assert acc_sync.elite_eligible_until > NOW
assert acc_limited.elite_covered is True
assert acc_limited.elite_eligible_until is not None
# 兑换唯一可兑换商品后,acc_limited 变为 covered 但不再 eligible
mark_commodity_exchanged(
db,
account_id=acc_limited.id,
commodity_id="C1",
occurred_at=NOW,
scope="elite",
)
db.commit()
db.refresh(acc_limited)
assert acc_limited.elite_covered is True
assert acc_limited.elite_eligible_until is None
totals = _global_inventory_totals(db, platform="douyu", scope="elite", now=NOW)
assert totals == {
"high_value": (1, 1, 1),
"ordinary": (1, 0, 0),
}
# 作废能力后清除可兑换有效期,覆盖仍保留
invalidate_account_commodity_states(
db,
account_id=acc_sync.id,
reason="ck invalid",
observed_at=NOW,
)
db.commit()
db.refresh(acc_sync)
assert acc_sync.elite_covered is True
assert acc_sync.elite_eligible_until is None
assert _global_inventory_totals(db, platform="douyu", scope="elite", now=NOW) == {
"high_value": (1, 0, 0),
"ordinary": (1, 0, 0),
}
+3 -1
View File
@@ -45,7 +45,7 @@ TESTS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = TESTS_DIR.parent
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
HEAD_REVISION = "20261128_0139"
HEAD_REVISION = "20261129_0140"
# 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = (
@@ -68,6 +68,8 @@ _ACCOUNT_PLATFORM_COLUMNS = {
"ck_observed_lifetime_seconds": "INTEGER NULL",
"ck_expiry_source": "VARCHAR(24) NOT NULL DEFAULT ''",
"value_tier": "INTEGER NULL",
"elite_covered": "BOOLEAN NULL",
"elite_eligible_until": "DATETIME NULL",
"role_change_official_at": "DATETIME NULL",
"last_bind_ok_at": "DATETIME NULL",
"role_bind_lock_until": "DATETIME NULL",