639 lines
18 KiB
Python
639 lines
18 KiB
Python
"""斗鱼账号供给库存、目标缺口和定向补货入队。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from dataclasses import dataclass
|
||
from datetime import UTC, datetime
|
||
|
||
from sqlalchemy import and_, case, func, select
|
||
from sqlalchemy.exc import IntegrityError
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..models import (
|
||
Account,
|
||
AccountCommodityState,
|
||
AccountSupplyTarget,
|
||
AccountSyncState,
|
||
)
|
||
from .account_query_filters import (
|
||
active_collab_order_exists,
|
||
active_lease_exists,
|
||
assigned_to_pool_filter,
|
||
automatable_account_filter,
|
||
cookie_present_filter,
|
||
fresh_ck_cutoff,
|
||
fresh_eligible_state_filter,
|
||
role_change_ready_filter,
|
||
valid_ck_filter,
|
||
)
|
||
from .account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
||
from .account_value import is_high_value_expr
|
||
from .account_work_queue import (
|
||
active_account_work_count,
|
||
active_account_work_exists,
|
||
enqueue_account_work,
|
||
)
|
||
|
||
VALUE_HIGH = "high_value"
|
||
VALUE_ORDINARY = "ordinary"
|
||
VALID_VALUES = frozenset({VALUE_HIGH, VALUE_ORDINARY})
|
||
DEFAULT_SUPPLY_TARGETS = (
|
||
{
|
||
"scope": "elite",
|
||
"value_class": VALUE_HIGH,
|
||
# 2026-11-22:旧 30/20/10/3 只适合功能验证,无法覆盖当前十余客服、
|
||
# 每小时 200+ 次领取峰值和 24 小时约 220 个 CK 流失。默认改为生产基线。
|
||
"target_covered": 2000,
|
||
"target_available": 300,
|
||
"target_allocatable": 200,
|
||
"safety_buffer": 60,
|
||
},
|
||
{
|
||
"scope": "elite",
|
||
"value_class": VALUE_ORDINARY,
|
||
"target_covered": 1800,
|
||
"target_available": 200,
|
||
"target_allocatable": 150,
|
||
"safety_buffer": 60,
|
||
},
|
||
)
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class SupplyInventory:
|
||
target_id: int
|
||
scope: str
|
||
commodity_id: str
|
||
value_class: str
|
||
covered: int
|
||
available: int
|
||
allocatable: int
|
||
target_covered: int
|
||
target_available: int
|
||
target_allocatable: int
|
||
safety_buffer: int
|
||
covered_shortage: int
|
||
available_shortage: int
|
||
allocatable_shortage: int
|
||
|
||
@property
|
||
def needs_replenishment(self) -> bool:
|
||
return bool(
|
||
self.covered_shortage
|
||
or self.available_shortage
|
||
or self.allocatable_shortage
|
||
)
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class SupplyReplenishmentResult:
|
||
targets: int
|
||
queued: int
|
||
shortages: int
|
||
|
||
|
||
def _value_filter(value_class: str, *, now: datetime):
|
||
high_value = is_high_value_expr(now=now)
|
||
if value_class == VALUE_HIGH:
|
||
return high_value
|
||
if value_class == VALUE_ORDINARY:
|
||
return ~high_value
|
||
raise ValueError(f"不支持的账号价值分类: {value_class}")
|
||
|
||
|
||
def _base_account_filter(*, now: datetime):
|
||
return and_(
|
||
cookie_present_filter(),
|
||
valid_ck_filter(checked_since=fresh_ck_cutoff(now)),
|
||
automatable_account_filter(),
|
||
)
|
||
|
||
|
||
def _base_account_query(
|
||
db: Session,
|
||
*,
|
||
now: datetime,
|
||
value_class: str,
|
||
):
|
||
return (
|
||
db.query(Account)
|
||
.filter(_base_account_filter(now=now))
|
||
.filter(_value_filter(value_class, now=now))
|
||
)
|
||
|
||
|
||
def _allocatable_filter(now: datetime):
|
||
return and_(
|
||
~active_lease_exists(),
|
||
~active_collab_order_exists(),
|
||
assigned_to_pool_filter(),
|
||
role_change_ready_filter(now),
|
||
)
|
||
|
||
|
||
def _fresh_eligible_filter(now: datetime):
|
||
return fresh_eligible_state_filter(now, allow_before_open=True)
|
||
|
||
|
||
def _commodity_query(
|
||
db: Session,
|
||
*,
|
||
now: datetime,
|
||
target: AccountSupplyTarget,
|
||
):
|
||
query = (
|
||
_base_account_query(
|
||
db,
|
||
now=now,
|
||
value_class=target.value_class,
|
||
)
|
||
.join(
|
||
AccountCommodityState,
|
||
AccountCommodityState.account_id == Account.id,
|
||
)
|
||
.filter(
|
||
AccountCommodityState.platform == target.platform,
|
||
AccountCommodityState.scope == target.scope,
|
||
AccountCommodityState.commodity_id == target.commodity_id,
|
||
AccountCommodityState.observed_at.isnot(None),
|
||
)
|
||
)
|
||
return query
|
||
|
||
|
||
def _global_state_exists_for(*, platform: str, scope: str):
|
||
return (
|
||
select(AccountCommodityState.id)
|
||
.where(
|
||
AccountCommodityState.account_id == Account.id,
|
||
AccountCommodityState.platform == platform,
|
||
AccountCommodityState.scope == scope,
|
||
AccountCommodityState.observed_at.isnot(None),
|
||
)
|
||
.exists()
|
||
)
|
||
|
||
|
||
def _global_inventory_totals(
|
||
db: Session,
|
||
*,
|
||
platform: str,
|
||
scope: str,
|
||
now: datetime,
|
||
) -> dict[str, tuple[int, int, int]]:
|
||
"""一次算出某作用域下高价值/普通的覆盖、可兑换和可分配数量。
|
||
|
||
全局目标不指定 commodity_id,因此不能用
|
||
``ix_account_commodity_match`` 的 commodity_id 前缀。固定拆成两条 SQL:
|
||
|
||
1. accounts + EXISTS:计算 covered;
|
||
2. account_commodity_states JOIN accounts:同时计算 available/allocatable。
|
||
|
||
第二条走 ``ix_account_commodity_global_available``,避免先物化全部商品状态。
|
||
"""
|
||
value_class = case(
|
||
(is_high_value_expr(now=now), VALUE_HIGH),
|
||
else_=VALUE_ORDINARY,
|
||
)
|
||
base_filter = _base_account_filter(now=now)
|
||
|
||
covered_rows = (
|
||
db.query(value_class, func.count(Account.id))
|
||
.select_from(Account)
|
||
.filter(
|
||
base_filter,
|
||
_global_state_exists_for(platform=platform, scope=scope),
|
||
)
|
||
.group_by(value_class)
|
||
.all()
|
||
)
|
||
totals = {
|
||
VALUE_HIGH: [0, 0, 0],
|
||
VALUE_ORDINARY: [0, 0, 0],
|
||
}
|
||
for class_name, covered in covered_rows:
|
||
totals[str(class_name)][0] = int(covered or 0)
|
||
|
||
allocatable_account = case(
|
||
(_allocatable_filter(now), Account.id),
|
||
else_=None,
|
||
)
|
||
available_rows = (
|
||
db.query(
|
||
value_class,
|
||
func.count(func.distinct(Account.id)),
|
||
func.count(func.distinct(allocatable_account)),
|
||
)
|
||
.select_from(Account)
|
||
.join(
|
||
AccountCommodityState,
|
||
AccountCommodityState.account_id == Account.id,
|
||
)
|
||
.filter(
|
||
base_filter,
|
||
AccountCommodityState.platform == platform,
|
||
AccountCommodityState.scope == scope,
|
||
_fresh_eligible_filter(now),
|
||
)
|
||
.group_by(value_class)
|
||
.all()
|
||
)
|
||
for class_name, available, allocatable in available_rows:
|
||
bucket = totals[str(class_name)]
|
||
bucket[1] = int(available or 0)
|
||
bucket[2] = int(allocatable or 0)
|
||
return {
|
||
class_name: (values[0], values[1], values[2])
|
||
for class_name, values in totals.items()
|
||
}
|
||
|
||
|
||
def _inventory_from_counts(
|
||
target: AccountSupplyTarget,
|
||
*,
|
||
covered: int,
|
||
available: int,
|
||
allocatable: int,
|
||
) -> SupplyInventory:
|
||
return SupplyInventory(
|
||
target_id=int(target.id),
|
||
scope=target.scope,
|
||
commodity_id=target.commodity_id,
|
||
value_class=target.value_class,
|
||
covered=covered,
|
||
available=available,
|
||
allocatable=allocatable,
|
||
target_covered=int(target.target_covered or 0),
|
||
target_available=int(target.target_available or 0),
|
||
target_allocatable=int(target.target_allocatable or 0),
|
||
safety_buffer=int(target.safety_buffer or 0),
|
||
covered_shortage=max(
|
||
0,
|
||
int(target.target_covered or 0) + int(target.safety_buffer or 0) - covered,
|
||
),
|
||
available_shortage=max(
|
||
0,
|
||
int(target.target_available or 0)
|
||
+ int(target.safety_buffer or 0)
|
||
- available,
|
||
),
|
||
allocatable_shortage=max(
|
||
0,
|
||
int(target.target_allocatable or 0)
|
||
+ int(target.safety_buffer or 0)
|
||
- allocatable,
|
||
),
|
||
)
|
||
|
||
|
||
def evaluate_supply_target(
|
||
db: Session,
|
||
target: AccountSupplyTarget,
|
||
*,
|
||
now: datetime | None = None,
|
||
) -> SupplyInventory:
|
||
"""计算一条供给目标的 covered / available / allocatable 库存。"""
|
||
moment = now or datetime.now(UTC)
|
||
if target.value_class not in VALID_VALUES:
|
||
raise ValueError(f"不支持的账号价值分类: {target.value_class}")
|
||
|
||
if target.commodity_id:
|
||
base = _commodity_query(db, now=moment, target=target)
|
||
covered = base.count()
|
||
available_query = base.filter(_fresh_eligible_filter(moment))
|
||
available = available_query.count()
|
||
allocatable = available_query.filter(_allocatable_filter(moment)).count()
|
||
else:
|
||
totals = _global_inventory_totals(
|
||
db,
|
||
platform=target.platform,
|
||
scope=target.scope,
|
||
now=moment,
|
||
)
|
||
covered, available, allocatable = totals[target.value_class]
|
||
|
||
return _inventory_from_counts(
|
||
target,
|
||
covered=covered,
|
||
available=available,
|
||
allocatable=allocatable,
|
||
)
|
||
|
||
|
||
def evaluate_supply_targets(
|
||
db: Session,
|
||
*,
|
||
now: datetime | None = None,
|
||
) -> list[SupplyInventory]:
|
||
moment = now or datetime.now(UTC)
|
||
targets = (
|
||
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()
|
||
)
|
||
global_totals: dict[tuple[str, str], dict[str, tuple[int, int, int]]] = {}
|
||
inventories: list[SupplyInventory] = []
|
||
for target in targets:
|
||
if target.commodity_id:
|
||
inventories.append(evaluate_supply_target(db, target, now=moment))
|
||
continue
|
||
key = (target.platform, target.scope)
|
||
if key not in global_totals:
|
||
global_totals[key] = _global_inventory_totals(
|
||
db,
|
||
platform=target.platform,
|
||
scope=target.scope,
|
||
now=moment,
|
||
)
|
||
covered, available, allocatable = global_totals[key][target.value_class]
|
||
inventories.append(
|
||
_inventory_from_counts(
|
||
target,
|
||
covered=covered,
|
||
available=available,
|
||
allocatable=allocatable,
|
||
)
|
||
)
|
||
return inventories
|
||
|
||
|
||
def upsert_supply_target(
|
||
db: Session,
|
||
*,
|
||
platform: str = "douyu",
|
||
scope: str,
|
||
commodity_id: str,
|
||
value_class: str,
|
||
target_covered: int,
|
||
target_available: int,
|
||
target_allocatable: int,
|
||
safety_buffer: int,
|
||
enabled: bool,
|
||
updated_by: int | None,
|
||
) -> AccountSupplyTarget:
|
||
"""新增或更新一条供给目标。"""
|
||
if value_class not in VALID_VALUES:
|
||
raise ValueError(f"不支持的账号价值分类: {value_class}")
|
||
for name, value in (
|
||
("target_covered", target_covered),
|
||
("target_available", target_available),
|
||
("target_allocatable", target_allocatable),
|
||
("safety_buffer", safety_buffer),
|
||
):
|
||
if int(value) < 0:
|
||
raise ValueError(f"{name} 不能为负数")
|
||
|
||
target = (
|
||
db.query(AccountSupplyTarget)
|
||
.filter(
|
||
AccountSupplyTarget.platform == platform,
|
||
AccountSupplyTarget.scope == scope,
|
||
AccountSupplyTarget.commodity_id == commodity_id,
|
||
AccountSupplyTarget.value_class == value_class,
|
||
)
|
||
.first()
|
||
)
|
||
if target is None:
|
||
target = AccountSupplyTarget(
|
||
platform=platform,
|
||
scope=scope,
|
||
commodity_id=commodity_id,
|
||
value_class=value_class,
|
||
)
|
||
db.add(target)
|
||
target.target_covered = int(target_covered)
|
||
target.target_available = int(target_available)
|
||
target.target_allocatable = int(target_allocatable)
|
||
target.safety_buffer = int(safety_buffer)
|
||
target.enabled = bool(enabled)
|
||
target.updated_by = updated_by
|
||
target.updated_at = datetime.now(UTC)
|
||
try:
|
||
with db.begin_nested():
|
||
db.flush()
|
||
except IntegrityError:
|
||
target = (
|
||
db.query(AccountSupplyTarget)
|
||
.filter(
|
||
AccountSupplyTarget.platform == platform,
|
||
AccountSupplyTarget.scope == scope,
|
||
AccountSupplyTarget.commodity_id == commodity_id,
|
||
AccountSupplyTarget.value_class == value_class,
|
||
)
|
||
.one()
|
||
)
|
||
db.commit()
|
||
return target
|
||
|
||
|
||
def ensure_default_supply_targets(db: Session) -> int:
|
||
"""Bootstrap conservative supply targets when none have ever been configured."""
|
||
enabled = os.getenv("DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED", "true").strip().lower()
|
||
if enabled in {"0", "false", "no", "off"}:
|
||
return 0
|
||
if db.query(AccountSupplyTarget.id).first() is not None:
|
||
return 0
|
||
|
||
created = 0
|
||
for spec in DEFAULT_SUPPLY_TARGETS:
|
||
target = AccountSupplyTarget(
|
||
platform="douyu",
|
||
scope=str(spec["scope"]),
|
||
commodity_id="",
|
||
value_class=str(spec["value_class"]),
|
||
target_covered=int(spec["target_covered"]),
|
||
target_available=int(spec["target_available"]),
|
||
target_allocatable=int(spec["target_allocatable"]),
|
||
safety_buffer=int(spec["safety_buffer"]),
|
||
enabled=True,
|
||
)
|
||
db.add(target)
|
||
created += 1
|
||
try:
|
||
db.commit()
|
||
except IntegrityError:
|
||
db.rollback()
|
||
return 0
|
||
return created
|
||
|
||
|
||
def update_supply_target(
|
||
db: Session,
|
||
target: AccountSupplyTarget,
|
||
*,
|
||
target_covered: int,
|
||
target_available: int,
|
||
target_allocatable: int,
|
||
safety_buffer: int,
|
||
enabled: bool,
|
||
updated_by: int | None,
|
||
) -> AccountSupplyTarget:
|
||
return upsert_supply_target(
|
||
db,
|
||
platform=target.platform,
|
||
scope=target.scope,
|
||
commodity_id=target.commodity_id,
|
||
value_class=target.value_class,
|
||
target_covered=target_covered,
|
||
target_available=target_available,
|
||
target_allocatable=target_allocatable,
|
||
safety_buffer=safety_buffer,
|
||
enabled=enabled,
|
||
updated_by=updated_by,
|
||
)
|
||
|
||
|
||
def _sync_candidate_query(
|
||
db: Session,
|
||
*,
|
||
target: AccountSupplyTarget,
|
||
now: datetime,
|
||
):
|
||
query = _base_account_query(
|
||
db,
|
||
now=now,
|
||
value_class=target.value_class,
|
||
)
|
||
if target.commodity_id:
|
||
state_exists = (
|
||
select(AccountCommodityState.id)
|
||
.where(
|
||
AccountCommodityState.account_id == Account.id,
|
||
AccountCommodityState.platform == target.platform,
|
||
AccountCommodityState.scope == target.scope,
|
||
AccountCommodityState.commodity_id == target.commodity_id,
|
||
AccountCommodityState.valid_until.isnot(None),
|
||
AccountCommodityState.valid_until > now,
|
||
)
|
||
.exists()
|
||
)
|
||
else:
|
||
state_exists = (
|
||
select(AccountSyncState.id)
|
||
.where(
|
||
AccountSyncState.account_id == Account.id,
|
||
AccountSyncState.platform == target.platform,
|
||
AccountSyncState.domain == "storehome",
|
||
AccountSyncState.status == "success",
|
||
AccountSyncState.valid_until.isnot(None),
|
||
AccountSyncState.valid_until > now,
|
||
)
|
||
.exists()
|
||
)
|
||
return (
|
||
query.filter(
|
||
~state_exists,
|
||
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
|
||
assigned_to_pool_filter(),
|
||
)
|
||
.order_by(
|
||
case((active_lease_exists(), 1), else_=0),
|
||
case((active_collab_order_exists(), 1), else_=0),
|
||
case((role_change_ready_filter(now), 0), else_=1),
|
||
case((Account.role_bind_status == "unbound", 0), else_=1),
|
||
Account.overview_queried_at.is_(None).desc(),
|
||
Account.overview_queried_at.asc(),
|
||
Account.id.asc(),
|
||
)
|
||
)
|
||
|
||
|
||
def _account_sync_queue_target_pending() -> int:
|
||
raw = os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")
|
||
try:
|
||
total = max(1, int(raw))
|
||
except ValueError:
|
||
total = 60
|
||
# 通用供给补货只占低水位;剩余水位留给有明确订单的履约商品,
|
||
# 防止注册顺序靠前的普通补货把待分配订单的紧急刷新挤到下一轮。
|
||
raw_supply = os.getenv("ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING", "20")
|
||
try:
|
||
supply = max(1, int(raw_supply))
|
||
except ValueError:
|
||
supply = 20
|
||
return min(total, supply)
|
||
|
||
|
||
def enqueue_supply_replenishment(
|
||
db: Session,
|
||
*,
|
||
now: datetime | None = None,
|
||
max_per_target: int = 50,
|
||
) -> SupplyReplenishmentResult:
|
||
"""按本地目标缺口定向入队 storehome 同步,不发起外部请求。"""
|
||
moment = now or datetime.now(UTC)
|
||
inventories = evaluate_supply_targets(db, now=moment)
|
||
queued = 0
|
||
shortages = 0
|
||
pending = active_account_work_count(
|
||
db,
|
||
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||
)
|
||
headroom = max(0, _account_sync_queue_target_pending() - pending)
|
||
for inventory in inventories:
|
||
if not inventory.needs_replenishment:
|
||
continue
|
||
shortages += 1
|
||
if headroom <= 0:
|
||
continue
|
||
target = db.get(AccountSupplyTarget, inventory.target_id)
|
||
if target is None:
|
||
continue
|
||
missing = max(
|
||
inventory.covered_shortage,
|
||
inventory.available_shortage,
|
||
inventory.allocatable_shortage,
|
||
)
|
||
limit = max(1, min(int(max_per_target), missing, headroom))
|
||
accounts = (
|
||
_sync_candidate_query(
|
||
db,
|
||
target=target,
|
||
now=moment,
|
||
)
|
||
.limit(limit)
|
||
.all()
|
||
)
|
||
for account in accounts:
|
||
enqueue_account_work(
|
||
db,
|
||
account_id=int(account.id),
|
||
work_type=WORK_STOREHOME_SYNC,
|
||
priority=35,
|
||
due_at=moment,
|
||
reason=(
|
||
f"供给短缺: {target.scope}/{target.commodity_id or 'all'}"
|
||
f"/{target.value_class}"
|
||
),
|
||
)
|
||
queued += 1
|
||
headroom -= 1
|
||
if queued:
|
||
db.commit()
|
||
return SupplyReplenishmentResult(
|
||
targets=len(inventories),
|
||
queued=queued,
|
||
shortages=shortages,
|
||
)
|
||
|
||
|
||
__all__ = [
|
||
"VALUE_HIGH",
|
||
"VALUE_ORDINARY",
|
||
"SupplyInventory",
|
||
"SupplyReplenishmentResult",
|
||
"enqueue_supply_replenishment",
|
||
"ensure_default_supply_targets",
|
||
"evaluate_supply_target",
|
||
"evaluate_supply_targets",
|
||
"update_supply_target",
|
||
"upsert_supply_target",
|
||
]
|