perf(account-supply): 合并全局库存统计并增加查询索引
This commit is contained in:
@@ -4,6 +4,8 @@ from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy import event
|
||||
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
@@ -13,6 +15,7 @@ from web.backend.models import (
|
||||
AccountSyncRun,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
DouyuExternalOrder,
|
||||
LoginAttempt,
|
||||
User,
|
||||
)
|
||||
@@ -21,9 +24,11 @@ from web.backend.schemas import DouyuSupplyTargetUpsert
|
||||
from web.backend.services.account_observability import account_platform_status
|
||||
from web.backend.services.account_supply_service import (
|
||||
VALUE_HIGH,
|
||||
_global_inventory_totals,
|
||||
enqueue_supply_replenishment,
|
||||
ensure_default_supply_targets,
|
||||
evaluate_supply_target,
|
||||
evaluate_supply_targets,
|
||||
upsert_supply_target,
|
||||
)
|
||||
from web.backend.services.account_work_queue import enqueue_account_work
|
||||
@@ -106,6 +111,115 @@ def test_supply_inventory_separates_available_and_allocatable(db):
|
||||
assert inventory.needs_replenishment is True
|
||||
|
||||
|
||||
def test_global_supply_inventory_batches_value_classes_in_two_queries(db):
|
||||
user = User(
|
||||
username="global-supply-owner",
|
||||
password_hash="h",
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
high_free = _account("global-high-free", points=400)
|
||||
high_leased = _account("global-high-leased", points=400)
|
||||
high_limited = _account("global-high-limited", points=400)
|
||||
ordinary_free = _account("global-ordinary-free")
|
||||
ordinary_collab = _account("global-ordinary-collab")
|
||||
db.add_all(
|
||||
[user, high_free, high_leased, high_limited, ordinary_free, ordinary_collab]
|
||||
)
|
||||
db.flush()
|
||||
limited_state = _state(high_limited)
|
||||
limited_state.availability = "limited"
|
||||
db.add_all(
|
||||
[
|
||||
_state(high_free),
|
||||
_state(high_leased),
|
||||
limited_state,
|
||||
_state(ordinary_free),
|
||||
_state(ordinary_collab),
|
||||
AccountLease(
|
||||
account_id=high_leased.id,
|
||||
holder_user_id=user.id,
|
||||
scope="elite",
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
),
|
||||
DouyuExternalOrder(
|
||||
order_no="global-supply-collab",
|
||||
account_id=ordinary_collab.id,
|
||||
account_username=ordinary_collab.username,
|
||||
status="incomplete",
|
||||
),
|
||||
AccountSupplyTarget(
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=3,
|
||||
target_available=2,
|
||||
target_allocatable=1,
|
||||
),
|
||||
AccountSupplyTarget(
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="",
|
||||
value_class="ordinary",
|
||||
target_covered=2,
|
||||
target_available=2,
|
||||
target_allocatable=1,
|
||||
),
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
|
||||
statements: list[str] = []
|
||||
|
||||
def capture(_conn, _cursor, statement, _parameters, _context, _executemany):
|
||||
if statement.lstrip().upper().startswith("SELECT"):
|
||||
statements.append(statement)
|
||||
|
||||
engine = db.get_bind()
|
||||
event.listen(engine, "before_cursor_execute", capture)
|
||||
try:
|
||||
totals = _global_inventory_totals(
|
||||
db,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
now=NOW,
|
||||
)
|
||||
finally:
|
||||
event.remove(engine, "before_cursor_execute", capture)
|
||||
|
||||
assert totals == {
|
||||
"high_value": (3, 2, 1),
|
||||
"ordinary": (2, 2, 1),
|
||||
}
|
||||
assert len(statements) == 2
|
||||
|
||||
inventories = {
|
||||
item.value_class: item for item in evaluate_supply_targets(db, now=NOW)
|
||||
}
|
||||
assert (
|
||||
inventories["high_value"].covered,
|
||||
inventories["high_value"].available,
|
||||
inventories["high_value"].allocatable,
|
||||
) == (3, 2, 1)
|
||||
assert (
|
||||
inventories["ordinary"].covered,
|
||||
inventories["ordinary"].available,
|
||||
inventories["ordinary"].allocatable,
|
||||
) == (2, 2, 1)
|
||||
|
||||
high_target = next(
|
||||
item for item in inventories.values() if item.value_class == VALUE_HIGH
|
||||
)
|
||||
single = evaluate_supply_target(
|
||||
db,
|
||||
db.get(AccountSupplyTarget, high_target.target_id),
|
||||
now=NOW,
|
||||
)
|
||||
assert (single.covered, single.available, single.allocatable) == (3, 2, 1)
|
||||
|
||||
|
||||
def test_supply_shortage_enqueues_missing_coverage(db):
|
||||
account = _account("supply-missing", points=400)
|
||||
db.add(account)
|
||||
@@ -280,9 +394,10 @@ def test_account_platform_status_aggregates_queue_sync_and_supply(db):
|
||||
assert status["commodities"]["fresh_available"] == 1
|
||||
assert status["supply_targets"][0]["covered_shortage"] == 1
|
||||
assert status["supply_targets"][0]["target_available"] == 2
|
||||
assert {
|
||||
alert["code"] for alert in status["alerts"]
|
||||
} >= {"supply_shortage", "ck_expiring_24h"}
|
||||
assert {alert["code"] for alert in status["alerts"]} >= {
|
||||
"supply_shortage",
|
||||
"ck_expiring_24h",
|
||||
}
|
||||
|
||||
|
||||
def test_account_platform_status_builds_seven_day_trends(db):
|
||||
|
||||
@@ -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 = "20261122_0113"
|
||||
HEAD_REVISION = "20261122_0114"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
Reference in New Issue
Block a user