perf(automation): 持久队列驱动并优化履约热路径

This commit is contained in:
yml
2026-11-23 15:07:31 +08:00
parent 11c2a0dba7
commit 2549511598
31 changed files with 1658 additions and 235 deletions
+15 -3
View File
@@ -12,6 +12,8 @@ from ..models import (
Account,
AccountCommodityState,
AccountEvent,
AccountSupplySnapshot,
AccountSupplyTarget,
AccountSyncRun,
AccountSyncState,
AccountWorkItem,
@@ -19,7 +21,7 @@ from ..models import (
)
from .account_capability_service import AVAILABILITY_AVAILABLE, AVAILABILITY_LIMITED
from .account_protection_service import evaluate_protection_targets
from .account_supply_service import evaluate_supply_targets
from .account_supply_service import load_supply_snapshot_inventories
TREND_DAYS = 7
@@ -265,7 +267,7 @@ def _supply_snapshot(
db: Session,
moment: datetime,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
inventories = evaluate_supply_targets(db, now=moment)
inventories = load_supply_snapshot_inventories(db, now=moment)
supply_targets: list[dict[str, Any]] = []
alerts: list[dict[str, Any]] = []
for item in inventories:
@@ -331,8 +333,18 @@ def account_supply_snapshot(
"""返回可延迟加载的账号供给库存,避免阻塞运行总览首屏。"""
moment = now or datetime.now(UTC)
supply_targets, alerts = _supply_snapshot(db, moment)
checked_at = (
db.query(func.max(AccountSupplySnapshot.checked_at))
.join(
AccountSupplyTarget,
AccountSupplyTarget.id == AccountSupplySnapshot.target_id,
)
.filter(AccountSupplyTarget.enabled.is_(True))
.scalar()
or moment
)
return {
"checked_at": moment,
"checked_at": checked_at,
"supply_targets": supply_targets,
"alerts": alerts,
}