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,
]