perf(fulfillment): 优化本地能力选号查询与索引

This commit is contained in:
yml
2026-11-23 17:18:39 +08:00
parent d5422b0ecd
commit f7a5979f9b
7 changed files with 225 additions and 11 deletions
+2 -1
View File
@@ -80,6 +80,7 @@ from .douyu_goods_service import (
)
from .file_service import build_file_url
from .platform_fulfillment.account_selector import (
CapabilityAccount,
acquire_account_lease,
active_lease_for_account,
capability_candidate_accounts,
@@ -651,7 +652,7 @@ def recommend_accounts(
),
)
now = _now()
candidates_by_id: dict[int, Account] = {}
candidates_by_id: dict[int, Account | CapabilityAccount] = {}
if cid:
for account in capability_candidate_accounts(
db,
@@ -64,8 +64,38 @@ class AccountCandidate:
class CapabilityCandidate:
"""一个新鲜能力快照及其对应账号,供本地零外部请求选号。"""
account: Account
state: AccountCommodityState
account: CapabilityAccount
state: CapabilityState
@dataclass(frozen=True)
class CapabilityAccount:
"""能力选号实际需要的账号字段,不加载 CK / JSON 等大列。"""
id: int
username: str
nickname: str
uid: str
points: int | None
gold_balance: int | None
points_expire_at: datetime | None
role_change_available_at: datetime | None
elite_opened: bool | None
role_bind_status: str
ck_check_status: str
game_name: str
@dataclass(frozen=True)
class CapabilityState:
"""选号排序之后才需要的能力快照详情。"""
account_eligibility: str
offer_state: str
observed_at: datetime | None
available_from: datetime | None
valid_until: datetime | None
blocking_reason: str
def _now(now: datetime | None = None) -> datetime:
@@ -251,7 +281,24 @@ def _capability_candidate_query(
allow_before_open=allow_before_open,
)
return (
db.query(Account, AccountCommodityState)
db.query(
Account.id.label("account_id"),
Account.username.label("account_username"),
Account.nickname.label("account_nickname"),
Account.uid.label("account_uid"),
Account.points.label("account_points"),
Account.gold_balance.label("account_gold_balance"),
Account.points_expire_at.label("account_points_expire_at"),
Account.role_change_available_at.label(
"account_role_change_available_at"
),
Account.elite_opened.label("account_elite_opened"),
Account.role_bind_status.label("account_role_bind_status"),
Account.ck_check_status.label("account_ck_check_status"),
Account.game_name.label("account_game_name"),
AccountCommodityState.offer_state.label("state_offer_state"),
AccountCommodityState.observed_at.label("state_observed_at"),
)
.join(
AccountCommodityState,
AccountCommodityState.account_id == Account.id,
@@ -315,10 +362,67 @@ def capability_candidate_rows(
)
if query is None:
return []
return [
CapabilityCandidate(account=account, state=state)
for account, state in query.all()
]
rows = query.all()
if not rows:
return []
account_ids = [int(row.account_id) for row in rows]
state_rows = (
db.query(
AccountCommodityState.account_id,
AccountCommodityState.account_eligibility,
AccountCommodityState.offer_state,
AccountCommodityState.observed_at,
AccountCommodityState.available_from,
AccountCommodityState.valid_until,
AccountCommodityState.blocking_reason,
)
.filter(
AccountCommodityState.account_id.in_(account_ids),
AccountCommodityState.platform == "douyu",
AccountCommodityState.scope == scope,
AccountCommodityState.commodity_id == commodity_id.strip(),
)
.all()
)
states = {
int(row.account_id): CapabilityState(
account_eligibility=str(row.account_eligibility or ""),
offer_state=str(row.offer_state or ""),
observed_at=row.observed_at,
available_from=row.available_from,
valid_until=row.valid_until,
blocking_reason=str(row.blocking_reason or ""),
)
for row in state_rows
}
candidates: list[CapabilityCandidate] = []
for row in rows:
account_id = int(row.account_id)
state = states.get(account_id)
if state is None:
continue
candidates.append(
CapabilityCandidate(
account=CapabilityAccount(
id=account_id,
username=str(row.account_username or ""),
nickname=str(row.account_nickname or ""),
uid=str(row.account_uid or ""),
points=row.account_points,
gold_balance=row.account_gold_balance,
points_expire_at=row.account_points_expire_at,
role_change_available_at=(
row.account_role_change_available_at
),
elite_opened=row.account_elite_opened,
role_bind_status=str(row.account_role_bind_status or ""),
ck_check_status=str(row.account_ck_check_status or ""),
game_name=str(row.account_game_name or ""),
),
state=state,
)
)
return candidates
def capability_candidate_accounts(
@@ -330,7 +434,7 @@ def capability_candidate_accounts(
limit: int = 20,
now: datetime | None = None,
allow_before_open: bool = False,
) -> list[Account]:
) -> list[CapabilityAccount]:
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
return [
candidate.account
@@ -680,6 +784,7 @@ __all__ = [
"EXCHANGE_COOLDOWN_DAYS",
"VALIDATION_LEASE_MINUTES",
"AccountCandidate",
"CapabilityAccount",
"acquire_account_lease",
"active_lease_for_account",
"capability_candidate_accounts",