diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 7fd3653..1436572 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent PROJECT_ROOT = TESTS_DIR.parent VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" -HEAD_REVISION = "20261123_0122" +HEAD_REVISION = "20261123_0123" # 迁移新增、但不声明在模型里的复合查询索引。 EXTRA_INDEXES = ( diff --git a/tests/test_platform_fulfillment.py b/tests/test_platform_fulfillment.py index 5b858f8..01b8b5d 100644 --- a/tests/test_platform_fulfillment.py +++ b/tests/test_platform_fulfillment.py @@ -1153,6 +1153,35 @@ class TestSnapshotSync(_Base): class TestAccountSelection(_Base): + def test_capability_candidate_query_selects_only_selection_columns(self): + query = account_selector._capability_candidate_query( + self.session, + commodity_id=COMMODITY, + required_points=1280, + now=NOW, + ) + + assert query is not None + selected = { + str(description["name"]) + for description in query.column_descriptions + } + assert { + "account_id", + "account_username", + "account_points", + "account_elite_opened", + "state_offer_state", + "state_observed_at", + } <= selected + assert { + "account_cookie", + "account_overview_snapshot", + "account_ck_check_result", + "state_raw_json", + "state_schedule_json", + }.isdisjoint(selected) + def test_manual_allocation_request_only_marks_task_due(self): task = self.task() old_retry_at = task.next_allocate_at diff --git a/web/backend/migrations/migration_manifest.json b/web/backend/migrations/migration_manifest.json index 0b05c44..f09b264 100644 --- a/web/backend/migrations/migration_manifest.json +++ b/web/backend/migrations/migration_manifest.json @@ -489,6 +489,10 @@ "20261123_0122_execution_jobs_recharge_guard.py": { "revision": "20261123_0122", "sha256": "e3549ea76a9da00d1e6e01e11d25fbb6fca3cce64b26223a8f0352a6fbec4c7d" + }, + "20261123_0123_capability_candidate_index.py": { + "revision": "20261123_0123", + "sha256": "4fc8ed0659f92a141ff383b5a5925e8b3e73aff7540b523198742971dc9b628b" } } } diff --git a/web/backend/migrations/versions/20261123_0123_capability_candidate_index.py b/web/backend/migrations/versions/20261123_0123_capability_candidate_index.py new file mode 100644 index 0000000..867385e --- /dev/null +++ b/web/backend/migrations/versions/20261123_0123_capability_candidate_index.py @@ -0,0 +1,73 @@ +"""add covering index for fulfillment capability candidates. + +Revision ID: 20261123_0123 +Revises: 20261123_0122 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + +revision: str = "20261123_0123" +down_revision: str | None = "20261123_0122" +branch_labels: str | None = None +depends_on: str | None = None + +INDEX_NAME = "ix_account_commodity_candidate" +REPLACED_INDEX_NAME = "ix_account_commodity_eligibility" + + +def _indexes() -> set[str]: + return { + str(index["name"]) + for index in sa.inspect(op.get_bind()).get_indexes( + "account_commodity_states" + ) + if index.get("name") + } + + +def upgrade() -> None: + indexes = _indexes() + if INDEX_NAME not in indexes: + op.create_index( + INDEX_NAME, + "account_commodity_states", + [ + "platform", + "scope", + "commodity_id", + "account_eligibility", + "offer_state", + "valid_until", + "account_id", + "last_exchange_at", + "observed_at", + ], + ) + if REPLACED_INDEX_NAME in indexes: + op.drop_index( + REPLACED_INDEX_NAME, + table_name="account_commodity_states", + ) + + +def downgrade() -> None: + indexes = _indexes() + if REPLACED_INDEX_NAME not in indexes: + op.create_index( + REPLACED_INDEX_NAME, + "account_commodity_states", + [ + "platform", + "scope", + "commodity_id", + "account_eligibility", + "offer_state", + "valid_until", + "account_id", + ], + ) + if INDEX_NAME in indexes: + op.drop_index(INDEX_NAME, table_name="account_commodity_states") diff --git a/web/backend/models.py b/web/backend/models.py index 0fdaed4..e7a1278 100644 --- a/web/backend/models.py +++ b/web/backend/models.py @@ -2118,7 +2118,7 @@ class AccountCommodityState(Base): "account_id", ), Index( - "ix_account_commodity_eligibility", + "ix_account_commodity_candidate", "platform", "scope", "commodity_id", @@ -2126,6 +2126,8 @@ class AccountCommodityState(Base): "offer_state", "valid_until", "account_id", + "last_exchange_at", + "observed_at", ), Index( "ix_account_commodity_account", diff --git a/web/backend/services/douyu_collab_service.py b/web/backend/services/douyu_collab_service.py index aef9f3f..958efd9 100644 --- a/web/backend/services/douyu_collab_service.py +++ b/web/backend/services/douyu_collab_service.py @@ -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, diff --git a/web/backend/services/platform_fulfillment/account_selector.py b/web/backend/services/platform_fulfillment/account_selector.py index 5c85caf..4b2b629 100644 --- a/web/backend/services/platform_fulfillment/account_selector.py +++ b/web/backend/services/platform_fulfillment/account_selector.py @@ -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",