perf(fulfillment): 优化能力刷新候选查询并新增精英能力投影
This commit is contained in:
@@ -623,18 +623,103 @@ def capability_candidate_accounts(
|
||||
]
|
||||
|
||||
|
||||
def _refresh_tiebreaker_order_by() -> tuple[Any, ...]:
|
||||
return (
|
||||
Account.overview_queried_at.is_(None).desc(),
|
||||
Account.overview_queried_at.asc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
|
||||
|
||||
def _refresh_priority_tier0_filter(now: datetime):
|
||||
return and_(
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
role_change_ready_filter(now),
|
||||
Account.role_bind_status == "unbound",
|
||||
)
|
||||
|
||||
|
||||
def _refresh_candidate_order_by(now: datetime) -> tuple[Any, ...]:
|
||||
return (
|
||||
case((active_lease_exists(), 1), else_=0),
|
||||
case((active_collab_order_exists(), 1), else_=0),
|
||||
case((role_change_ready_filter(now), 0), else_=1),
|
||||
case((Account.role_bind_status == "unbound", 0), else_=1),
|
||||
Account.overview_queried_at.is_(None).desc(),
|
||||
Account.overview_queried_at.asc(),
|
||||
Account.id.asc(),
|
||||
*_refresh_tiebreaker_order_by(),
|
||||
)
|
||||
|
||||
|
||||
def _pool_refresh_base_query(
|
||||
db: Session,
|
||||
*,
|
||||
excluded_ids: tuple[int, ...] = (),
|
||||
):
|
||||
query = db.query(Account).filter(
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
)
|
||||
active_work_ids = active_account_work_ids(db, work_type=WORK_STOREHOME_SYNC)
|
||||
if active_work_ids is None:
|
||||
query = query.filter(~active_account_work_exists(work_type=WORK_STOREHOME_SYNC))
|
||||
elif active_work_ids:
|
||||
query = query.filter(~Account.id.in_(active_work_ids))
|
||||
if excluded_ids:
|
||||
query = query.filter(~Account.id.in_(excluded_ids))
|
||||
return query
|
||||
|
||||
|
||||
def _select_refresh_candidates_tier0_first(
|
||||
db: Session,
|
||||
*,
|
||||
base_pool,
|
||||
condition,
|
||||
moment: datetime,
|
||||
limit: int,
|
||||
) -> list[Account]:
|
||||
"""优先只取最高优先级窗口账号再校验商品明细,避免全池 1.1 万账号逐行扫 45 条商品。"""
|
||||
effective_limit = max(1, min(int(limit), 100))
|
||||
window_limit = max(effective_limit * 4, 200)
|
||||
tier0_ids = [
|
||||
int(account_id)
|
||||
for (account_id,) in (
|
||||
base_pool.with_entities(Account.id)
|
||||
.filter(_refresh_priority_tier0_filter(moment))
|
||||
.order_by(*_refresh_tiebreaker_order_by())
|
||||
.limit(window_limit)
|
||||
.all()
|
||||
)
|
||||
]
|
||||
if not tier0_ids:
|
||||
return (
|
||||
base_pool.filter(condition)
|
||||
.order_by(*_refresh_candidate_order_by(moment))
|
||||
.limit(effective_limit)
|
||||
.all()
|
||||
)
|
||||
|
||||
collected = (
|
||||
db.query(Account)
|
||||
.filter(Account.id.in_(tier0_ids), condition)
|
||||
.order_by(*_refresh_tiebreaker_order_by())
|
||||
.limit(effective_limit)
|
||||
.all()
|
||||
)
|
||||
if len(collected) >= effective_limit:
|
||||
return collected[:effective_limit]
|
||||
|
||||
remaining = effective_limit - len(collected)
|
||||
rest = (
|
||||
base_pool.filter(~Account.id.in_(tier0_ids), condition)
|
||||
.order_by(*_refresh_candidate_order_by(moment))
|
||||
.limit(remaining)
|
||||
.all()
|
||||
)
|
||||
return [*collected, *rest]
|
||||
|
||||
|
||||
def capability_refresh_candidate_accounts(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -708,25 +793,17 @@ def capability_refresh_candidate_accounts(
|
||||
< len(commodities)
|
||||
)
|
||||
|
||||
query = db.query(Account).filter(
|
||||
or_(
|
||||
state_due,
|
||||
and_(~fresh_storehome, missing_state),
|
||||
),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
base_pool = _pool_refresh_base_query(db)
|
||||
needs_refresh = or_(
|
||||
state_due,
|
||||
and_(~fresh_storehome, missing_state),
|
||||
)
|
||||
active_work_ids = active_account_work_ids(db, work_type=WORK_STOREHOME_SYNC)
|
||||
if active_work_ids is None:
|
||||
query = query.filter(~active_account_work_exists(work_type=WORK_STOREHOME_SYNC))
|
||||
elif active_work_ids:
|
||||
query = query.filter(~Account.id.in_(active_work_ids))
|
||||
return (
|
||||
query.order_by(*_refresh_candidate_order_by(moment))
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
.all()
|
||||
return _select_refresh_candidates_tier0_first(
|
||||
db,
|
||||
base_pool=base_pool,
|
||||
condition=needs_refresh,
|
||||
moment=moment,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
|
||||
@@ -798,23 +875,17 @@ def storehome_refresh_candidate_accounts(
|
||||
)
|
||||
for group in preferred_groups
|
||||
]
|
||||
query = db.query(Account).filter(
|
||||
~fresh_storehome,
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
)
|
||||
active_work_ids = active_account_work_ids(db, work_type=WORK_STOREHOME_SYNC)
|
||||
if active_work_ids is None:
|
||||
query = query.filter(~active_account_work_exists(work_type=WORK_STOREHOME_SYNC))
|
||||
elif active_work_ids:
|
||||
query = query.filter(~Account.id.in_(active_work_ids))
|
||||
if excluded_ids:
|
||||
query = query.filter(~Account.id.in_(excluded_ids))
|
||||
order_by: list[Any] = []
|
||||
if preferred_exprs:
|
||||
order_by.append(case((or_(*preferred_exprs), 0), else_=1))
|
||||
base_pool = _pool_refresh_base_query(db, excluded_ids=excluded_ids)
|
||||
if not preferred_exprs:
|
||||
return _select_refresh_candidates_tier0_first(
|
||||
db,
|
||||
base_pool=base_pool,
|
||||
condition=~fresh_storehome,
|
||||
moment=moment,
|
||||
limit=limit,
|
||||
)
|
||||
query = base_pool.filter(~fresh_storehome)
|
||||
order_by: list[Any] = [case((or_(*preferred_exprs), 0), else_=1)]
|
||||
order_by.extend(_refresh_candidate_order_by(moment))
|
||||
return query.order_by(*order_by).limit(max(1, min(int(limit), 100))).all()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user