feat(douyu): 增加账号供给看板与阈值告警

This commit is contained in:
yml
2026-11-21 22:55:42 +08:00
parent 1e2089431d
commit f5d81a65ce
11 changed files with 413 additions and 18 deletions
+86 -16
View File
@@ -145,6 +145,90 @@ def account_platform_status(
fresh_limited = int(commodities.get(AVAILABILITY_LIMITED, {}).get("fresh", 0))
inventories = evaluate_supply_targets(db, now=moment)
supply_targets: list[dict[str, Any]] = []
alerts: list[dict[str, Any]] = []
for item in inventories:
shortage = max(
item.covered_shortage,
item.available_shortage,
item.allocatable_shortage,
)
supply_targets.append(
{
"target_id": item.target_id,
"scope": item.scope,
"commodity_id": item.commodity_id,
"value_class": item.value_class,
"target_covered": item.target_covered,
"target_available": item.target_available,
"target_allocatable": item.target_allocatable,
"safety_buffer": item.safety_buffer,
"covered": item.covered,
"available": item.available,
"allocatable": item.allocatable,
"covered_shortage": item.covered_shortage,
"available_shortage": item.available_shortage,
"allocatable_shortage": item.allocatable_shortage,
"needs_replenishment": item.needs_replenishment,
}
)
if not item.needs_replenishment:
continue
critical = (
(item.target_available > 0 and item.available <= 0)
or (item.target_allocatable > 0 and item.allocatable <= 0)
)
alerts.append(
{
"code": "supply_shortage",
"severity": "critical" if critical else "warning",
"scope": item.scope,
"commodity_id": item.commodity_id,
"value_class": item.value_class,
"shortage": shortage,
"message": (
f"{item.scope}/{item.commodity_id or 'all'}/"
f"{item.value_class} 库存短缺 {shortage}"
),
}
)
if not inventories:
alerts.append(
{
"code": "supply_targets_missing",
"severity": "warning",
"message": "尚未配置启用的账号供给目标",
}
)
if expiring_24h:
alerts.append(
{
"code": "ck_expiring_24h",
"severity": "warning",
"count": expiring_24h,
"message": f"未来 24 小时预计有 {expiring_24h} 个 CK 到期",
}
)
overdue_work = int(
db.query(func.count(AccountWorkItem.id))
.filter(
AccountWorkItem.status == "pending",
AccountWorkItem.due_at < moment,
)
.scalar()
or 0
)
if overdue_work:
alerts.append(
{
"code": "account_work_overdue",
"severity": "warning",
"count": overdue_work,
"message": f"账号内部工作队列有 {overdue_work} 项待处理已过期",
}
)
return {
"checked_at": moment,
"accounts": {
@@ -165,22 +249,8 @@ def account_platform_status(
"fresh_available": fresh_available,
"fresh_limited": fresh_limited,
},
"supply_targets": [
{
"target_id": item.target_id,
"scope": item.scope,
"commodity_id": item.commodity_id,
"value_class": item.value_class,
"covered": item.covered,
"available": item.available,
"allocatable": item.allocatable,
"covered_shortage": item.covered_shortage,
"available_shortage": item.available_shortage,
"allocatable_shortage": item.allocatable_shortage,
"needs_replenishment": item.needs_replenishment,
}
for item in inventories
],
"supply_targets": supply_targets,
"alerts": alerts,
}