perf(automation): 持久队列驱动并优化履约热路径
This commit is contained in:
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy import and_, case, func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
@@ -13,6 +13,7 @@ from sqlalchemy.orm import Session
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountSupplySnapshot,
|
||||
AccountSupplyTarget,
|
||||
AccountSyncState,
|
||||
)
|
||||
@@ -58,6 +59,7 @@ DEFAULT_SUPPLY_TARGETS = (
|
||||
"safety_buffer": 60,
|
||||
},
|
||||
)
|
||||
DEFAULT_SUPPLY_SNAPSHOT_MAX_AGE_SECONDS = 15 * 60
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -364,6 +366,123 @@ def evaluate_supply_targets(
|
||||
return inventories
|
||||
|
||||
|
||||
def _enabled_supply_targets(db: Session) -> list[AccountSupplyTarget]:
|
||||
return (
|
||||
db.query(AccountSupplyTarget)
|
||||
.filter(AccountSupplyTarget.enabled.is_(True))
|
||||
.order_by(
|
||||
AccountSupplyTarget.platform.asc(),
|
||||
AccountSupplyTarget.scope.asc(),
|
||||
AccountSupplyTarget.value_class.asc(),
|
||||
AccountSupplyTarget.commodity_id.asc(),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
def supply_snapshot_max_age_seconds() -> int:
|
||||
raw = os.getenv(
|
||||
"ACCOUNT_SUPPLY_SNAPSHOT_MAX_AGE_SECONDS",
|
||||
str(DEFAULT_SUPPLY_SNAPSHOT_MAX_AGE_SECONDS),
|
||||
)
|
||||
try:
|
||||
return max(30, int(raw))
|
||||
except ValueError:
|
||||
return DEFAULT_SUPPLY_SNAPSHOT_MAX_AGE_SECONDS
|
||||
|
||||
|
||||
def refresh_supply_snapshots(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> list[SupplyInventory]:
|
||||
"""重算并持久化供给快照;只由后台作业或明确的回源路径调用。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
inventories = evaluate_supply_targets(db, now=moment)
|
||||
existing = {
|
||||
int(row.target_id): row
|
||||
for row in db.query(AccountSupplySnapshot)
|
||||
.filter(
|
||||
AccountSupplySnapshot.target_id.in_(
|
||||
[item.target_id for item in inventories]
|
||||
)
|
||||
)
|
||||
.all()
|
||||
} if inventories else {}
|
||||
for inventory in inventories:
|
||||
row = existing.get(inventory.target_id)
|
||||
if row is None:
|
||||
row = AccountSupplySnapshot(
|
||||
target_id=inventory.target_id,
|
||||
covered=inventory.covered,
|
||||
available=inventory.available,
|
||||
allocatable=inventory.allocatable,
|
||||
checked_at=moment,
|
||||
)
|
||||
try:
|
||||
with db.begin_nested():
|
||||
db.add(row)
|
||||
db.flush()
|
||||
except IntegrityError:
|
||||
row = (
|
||||
db.query(AccountSupplySnapshot)
|
||||
.filter(
|
||||
AccountSupplySnapshot.target_id
|
||||
== inventory.target_id
|
||||
)
|
||||
.one()
|
||||
)
|
||||
row.covered = inventory.covered
|
||||
row.available = inventory.available
|
||||
row.allocatable = inventory.allocatable
|
||||
row.checked_at = moment
|
||||
row.updated_at = moment
|
||||
if inventories:
|
||||
db.commit()
|
||||
return inventories
|
||||
|
||||
|
||||
def load_supply_snapshot_inventories(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
refresh_if_stale: bool = True,
|
||||
) -> list[SupplyInventory]:
|
||||
"""优先读快照;缺失或超过有效期时回源一次并刷新快照。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
targets = _enabled_supply_targets(db)
|
||||
if not targets:
|
||||
return []
|
||||
snapshots = {
|
||||
int(row.target_id): row
|
||||
for row in db.query(AccountSupplySnapshot)
|
||||
.filter(
|
||||
AccountSupplySnapshot.target_id.in_(
|
||||
[int(target.id) for target in targets]
|
||||
)
|
||||
)
|
||||
.all()
|
||||
}
|
||||
fresh_after = moment - timedelta(seconds=supply_snapshot_max_age_seconds())
|
||||
if all(
|
||||
int(target.id) in snapshots
|
||||
and snapshots[int(target.id)].checked_at >= fresh_after
|
||||
for target in targets
|
||||
):
|
||||
return [
|
||||
_inventory_from_counts(
|
||||
target,
|
||||
covered=int(snapshots[int(target.id)].covered or 0),
|
||||
available=int(snapshots[int(target.id)].available or 0),
|
||||
allocatable=int(snapshots[int(target.id)].allocatable or 0),
|
||||
)
|
||||
for target in targets
|
||||
]
|
||||
if refresh_if_stale:
|
||||
return refresh_supply_snapshots(db, now=moment)
|
||||
return evaluate_supply_targets(db, now=moment)
|
||||
|
||||
|
||||
def upsert_supply_target(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -569,7 +688,7 @@ def enqueue_supply_replenishment(
|
||||
) -> SupplyReplenishmentResult:
|
||||
"""按本地目标缺口定向入队 storehome 同步,不发起外部请求。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
inventories = evaluate_supply_targets(db, now=moment)
|
||||
inventories = load_supply_snapshot_inventories(db, now=moment)
|
||||
queued = 0
|
||||
shortages = 0
|
||||
pending = active_account_work_count(
|
||||
@@ -633,6 +752,8 @@ __all__ = [
|
||||
"ensure_default_supply_targets",
|
||||
"evaluate_supply_target",
|
||||
"evaluate_supply_targets",
|
||||
"load_supply_snapshot_inventories",
|
||||
"refresh_supply_snapshots",
|
||||
"update_supply_target",
|
||||
"upsert_supply_target",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user