From f5d81a65cea946de19deb7fed59b6fb08448c830 Mon Sep 17 00:00:00 2001 From: yml Date: Sat, 21 Nov 2026 22:55:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(douyu):=20=E5=A2=9E=E5=8A=A0=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E4=BE=9B=E7=BB=99=E7=9C=8B=E6=9D=BF=E4=B8=8E=E9=98=88?= =?UTF-8?q?=E5=80=BC=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 2 + docker-compose.yml | 1 + docs/斗鱼/斗鱼账号平台最终实施设计.md | 15 +- tests/test_account_supply_service.py | 28 +++ web/backend/main.py | 5 + web/backend/services/account_observability.py | 102 +++++++++-- .../services/account_supply_service.py | 51 ++++++ web/frontend/src/api/douyu.ts | 3 + web/frontend/src/api/types.ts | 53 ++++++ web/frontend/src/pages/AccountCheckPage.tsx | 3 + .../AccountPlatformStatusPanel.tsx | 168 ++++++++++++++++++ 11 files changed, 413 insertions(+), 18 deletions(-) create mode 100644 web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx diff --git a/.env.example b/.env.example index e2c57d6..e4d30b4 100644 --- a/.env.example +++ b/.env.example @@ -211,6 +211,8 @@ PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5 # 客户绑定二维码有效期 DOUYU_BIND_ROLE_POLL_SECONDS=1200 PLATFORM_BINDING_QR_TTL_SECONDS=1260 +# 首次启动且尚未配置供给目标时,建立保守的高价值/普通号库存目标。 +DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED=true # Uvicorn reload(开发模式设为 true,生产环境保持 false) UVICORN_RELOAD=false diff --git a/docker-compose.yml b/docker-compose.yml index d132eaf..4aa1d26 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -123,6 +123,7 @@ services: - PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=${PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS:-5} - DOUYU_BIND_ROLE_POLL_SECONDS=${DOUYU_BIND_ROLE_POLL_SECONDS:-1200} - PLATFORM_BINDING_QR_TTL_SECONDS=${PLATFORM_BINDING_QR_TTL_SECONDS:-1260} + - DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED=${DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED:-true} # 用户上传图片的桶(在线协作 / P2 资料收集共用;生产用内网 endpoint)。 # OSS_MODE=oss 才走对象存储,否则落容器内 ./data/file_storage。 - OSS_MODE=${OSS_MODE:-local} diff --git a/docs/斗鱼/斗鱼账号平台最终实施设计.md b/docs/斗鱼/斗鱼账号平台最终实施设计.md index 9e7206e..7b2acb7 100644 --- a/docs/斗鱼/斗鱼账号平台最终实施设计.md +++ b/docs/斗鱼/斗鱼账号平台最终实施设计.md @@ -11,14 +11,14 @@ 更新时间:2026-11-21 -整体完成度约 **66%**。 +整体完成度约 **72%**。 | 阶段 | 完成度 | 已落地 | 主要缺口 | | --- | ---: | --- | --- | | 阶段 0:止乱 | 100% | 登录批次上限、任务槽隔离、连接及时释放 | 无 | | 阶段 1:同步基础 | 95% | 新表、迁移、storehome 网关、全量快照、同步运行记录、内部工作队列执行器、session/points/gold/elite/esports/role/exchange 同步状态 | 少量旁路查询仍需继续收口 | | 阶段 2:账号与 CK | 90% | CK 周期字段、invalid 分流、登录流水、日/小时/分类/单账号预算、login_relogin 队列真实消费与退避回队、预算配置 API | 更精细的动态预算计算 | -| 阶段 3:商品与供给 | 60% | 全量商品能力、限时窗口、原始响应、供给库存和目标缺口、定向补货入队、供给目标配置 API、统一平台状态快照 | 默认供给目标、供给看板、阈值告警 | +| 阶段 3:商品与供给 | 90% | 全量商品能力、限时窗口、原始响应、供给库存和目标缺口、定向补货入队、供给目标配置 API、默认目标、统一平台状态快照、供给看板、阈值告警 | 供给目标管理 UI、趋势统计 | | 阶段 4:安全恢复自动化 | 50% | account_sync_queue 与供给补货作业默认小批量开启,自动重登消费工作队列,硬失败率熔断,登录预算在线配置且熔断阈值实时生效 | 灰度批次自动扩缩和长期趋势告警 | | 阶段 5:清理旧逻辑 | 20% | 履约补货不再直接发官网请求 | 旧资料刷新、旁路查询和旧候选兜底仍需逐步下线 | @@ -505,6 +505,16 @@ Runner 启动前必须有预算 token;没有 token 时留在工作队列。 - 价值分档与库存目标 - 补货 planner +默认目标只在 `account_supply_targets` 完全为空时建立,使用保守库存: + +| 分类 | 覆盖 | 可兑换 | 可分配 | 安全缓冲 | +| --- | ---: | ---: | ---: | ---: | +| 高价值 | 30 | 20 | 10 | 3 | +| 普通 | 60 | 30 | 15 | 5 | + +可通过 `DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED=false` 关闭初始化。人工新增过任意 +供给目标后,启动过程不会再注入或覆盖默认值。 + ### 阶段 4:安全恢复自动化 - 自动重登小批量开启 @@ -599,3 +609,4 @@ Runner 启动前必须有预算 token;没有 token 时留在工作队列。 | 日期 | 变更 | | --- | --- | | 2026-11-21 | 建立斗鱼账号平台最终实施蓝图 | +| 2026-11-21 | 增加保守默认供给目标、账号平台供给看板和库存/CK/工作队列阈值告警 | diff --git a/tests/test_account_supply_service.py b/tests/test_account_supply_service.py index e9b58f3..c5a14ca 100644 --- a/tests/test_account_supply_service.py +++ b/tests/test_account_supply_service.py @@ -19,6 +19,7 @@ from web.backend.services.account_observability import account_platform_status from web.backend.services.account_supply_service import ( VALUE_HIGH, enqueue_supply_replenishment, + ensure_default_supply_targets, evaluate_supply_target, upsert_supply_target, ) @@ -165,6 +166,29 @@ def test_supply_target_configuration_is_idempotent(db): assert second.enabled is False +def test_default_supply_targets_are_seeded_once(db, monkeypatch): + monkeypatch.setenv("DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED", "true") + + assert ensure_default_supply_targets(db) == 2 + assert ensure_default_supply_targets(db) == 0 + + targets = ( + db.query(AccountSupplyTarget) + .order_by(AccountSupplyTarget.value_class.asc()) + .all() + ) + assert [row.value_class for row in targets] == ["high_value", "ordinary"] + assert all(row.scope == "elite" for row in targets) + assert all(row.enabled for row in targets) + + +def test_default_supply_targets_can_be_disabled(db, monkeypatch): + monkeypatch.setenv("DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED", "false") + + assert ensure_default_supply_targets(db) == 0 + assert db.query(AccountSupplyTarget).count() == 0 + + def test_supply_target_configuration_route_returns_inventory(db): user = User( username="supply-config-admin", @@ -238,3 +262,7 @@ def test_account_platform_status_aggregates_queue_sync_and_supply(db): assert status["sync_domains"]["storehome"]["fresh"] == 1 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"} diff --git a/web/backend/main.py b/web/backend/main.py index 29ad397..345d45c 100644 --- a/web/backend/main.py +++ b/web/backend/main.py @@ -92,6 +92,11 @@ async def lifespan(app: FastAPI): recovered_work = recover_running_account_work(db) if recovered_work: logger.info(f"启动恢复中断的账号内部工作项: {recovered_work} 条") + from .services.account_supply_service import ensure_default_supply_targets + + seeded_targets = ensure_default_supply_targets(db) + if seeded_targets: + logger.info(f"已建立默认账号供给目标: {seeded_targets} 条") from .services.yyb_service import cleanup_orphan_yyb_tasks cleaned_yyb = cleanup_orphan_yyb_tasks(db, message="任务已中断(服务重启)") diff --git a/web/backend/services/account_observability.py b/web/backend/services/account_observability.py index 23db6e6..f509d61 100644 --- a/web/backend/services/account_observability.py +++ b/web/backend/services/account_observability.py @@ -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, } diff --git a/web/backend/services/account_supply_service.py b/web/backend/services/account_supply_service.py index 4faad44..12be260 100644 --- a/web/backend/services/account_supply_service.py +++ b/web/backend/services/account_supply_service.py @@ -2,6 +2,7 @@ from __future__ import annotations +import os from dataclasses import dataclass from datetime import UTC, datetime, timedelta @@ -36,6 +37,24 @@ ACTIVE_COLLAB_STATUSES = ( "role_error", "failed", ) +DEFAULT_SUPPLY_TARGETS = ( + { + "scope": "elite", + "value_class": VALUE_HIGH, + "target_covered": 30, + "target_available": 20, + "target_allocatable": 10, + "safety_buffer": 3, + }, + { + "scope": "elite", + "value_class": VALUE_ORDINARY, + "target_covered": 60, + "target_available": 30, + "target_allocatable": 15, + "safety_buffer": 5, + }, +) @dataclass(frozen=True) @@ -356,6 +375,37 @@ def upsert_supply_target( 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, @@ -483,6 +533,7 @@ __all__ = [ "SupplyInventory", "SupplyReplenishmentResult", "enqueue_supply_replenishment", + "ensure_default_supply_targets", "evaluate_supply_target", "evaluate_supply_targets", "update_supply_target", diff --git a/web/frontend/src/api/douyu.ts b/web/frontend/src/api/douyu.ts index 59f827f..2537d8b 100644 --- a/web/frontend/src/api/douyu.ts +++ b/web/frontend/src/api/douyu.ts @@ -1,5 +1,6 @@ import api from './client'; import type { + DouyuAccountPlatformStatus, DouyuConfig, DouyuExchangeResultImage, DouyuGoodsItem, @@ -16,6 +17,8 @@ import type { export const douyuApi = { taskTypes: () => api.get, Record>('/douyu/task-types'), + getAccountPlatformStatus: () => + api.get('/douyu/account-platform-status'), listAccounts: (params?: { search?: string; tag?: string; ids?: string }) => api.get('/douyu/accounts', { params }), listAccountsPaged: (params: PageParams & { ids?: string; tag?: string }) => diff --git a/web/frontend/src/api/types.ts b/web/frontend/src/api/types.ts index 55d634d..ab1ec8d 100644 --- a/web/frontend/src/api/types.ts +++ b/web/frontend/src/api/types.ts @@ -727,6 +727,59 @@ export interface DouyuTaskAccountItem { assigned_username: string | null; } +export interface DouyuSupplyTargetStatus { + target_id: number; + scope: string; + commodity_id: string; + value_class: 'high_value' | 'ordinary'; + target_covered: number; + target_available: number; + target_allocatable: number; + safety_buffer: number; + covered: number; + available: number; + allocatable: number; + covered_shortage: number; + available_shortage: number; + allocatable_shortage: number; + needs_replenishment: boolean; +} + +export interface DouyuPlatformAlert { + code: string; + severity: 'critical' | 'warning' | 'info'; + message: string; + scope?: string; + commodity_id?: string; + value_class?: string; + shortage?: number; + count?: number; +} + +export interface DouyuAccountPlatformStatus { + checked_at: string; + accounts: { + total: number; + with_cookie: number; + ck_valid: number; + ck_invalid: number; + ck_error: number; + ck_unchecked: number; + ck_expiring_24h: number; + ck_expiring_72h: number; + ck_predicted_expired: number; + }; + work_queue: Record>; + sync_domains: Record>; + commodities: { + by_availability: Record; + fresh_available: number; + fresh_limited: number; + }; + supply_targets: DouyuSupplyTargetStatus[]; + alerts: DouyuPlatformAlert[]; +} + export interface DouyuConfig { manual_id: string; rid: string; diff --git a/web/frontend/src/pages/AccountCheckPage.tsx b/web/frontend/src/pages/AccountCheckPage.tsx index 70616fb..2a0112e 100644 --- a/web/frontend/src/pages/AccountCheckPage.tsx +++ b/web/frontend/src/pages/AccountCheckPage.tsx @@ -9,6 +9,7 @@ import { DownloadOutlined, PlayCircleOutlined, ReloadOutlined, StopOutlined } fr import { accountApi, accountCheckApi, type AccountCheckBatch, type AccountCheckItem } from '../api/modules'; import { formatTime } from '../utils/time'; import { getErrorMessage } from '../utils/error'; +import AccountPlatformStatusPanel from './douyu-tasks/AccountPlatformStatusPanel'; const { TextArea } = Input; const { Text, Title } = Typography; @@ -340,6 +341,8 @@ export default function AccountCheckPage() { 账号检测 + + {/* 两种来源并存:手动粘贴是旧逻辑,一条没动; diff --git a/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx b/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx new file mode 100644 index 0000000..c11dc28 --- /dev/null +++ b/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx @@ -0,0 +1,168 @@ +import { useCallback, useEffect, useMemo, useState } from 'react'; +import { + Alert, + Button, + Card, + Col, + Empty, + Row, + Space, + Statistic, + Table, + Tag, + Typography, +} from 'antd'; +import type { TableProps } from 'antd'; +import { ReloadOutlined } from '@ant-design/icons'; +import { + douyuApi, + type DouyuAccountPlatformStatus, + type DouyuSupplyTargetStatus, +} from '../../api/modules'; +import { getErrorMessage } from '../../utils/error'; +import { formatTime } from '../../utils/time'; + +const { Text } = Typography; + +const VALUE_LABELS: Record = { + high_value: '高价值', + ordinary: '普通', +}; + +function targetTotal(actual: number, target: number, buffer: number) { + const effectiveTarget = target + buffer; + return `${actual}/${effectiveTarget || '-'}`; +} + +export default function AccountPlatformStatusPanel() { + const [status, setStatus] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(''); + + const load = useCallback(async () => { + setLoading(true); + setError(''); + try { + setStatus(await douyuApi.getAccountPlatformStatus()); + } catch (loadError: unknown) { + setError(getErrorMessage(loadError)); + } finally { + setLoading(false); + } + }, []); + + useEffect(() => { + void load(); + }, [load]); + + const pendingWork = useMemo( + () => Object.values(status?.work_queue || {}).reduce( + (total, bucket) => total + Number(bucket.pending || 0), + 0, + ), + [status?.work_queue], + ); + + const columns: TableProps['columns'] = [ + { + title: '分类', + width: 130, + render: (_, row) => ( + + + {VALUE_LABELS[row.value_class] || row.value_class} + + {row.scope} + + ), + }, + { + title: '覆盖', + width: 110, + render: (_, row) => targetTotal(row.covered, row.target_covered, row.safety_buffer), + }, + { + title: '可兑换', + width: 110, + render: (_, row) => targetTotal(row.available, row.target_available, row.safety_buffer), + }, + { + title: '可分配', + width: 110, + render: (_, row) => targetTotal(row.allocatable, row.target_allocatable, row.safety_buffer), + }, + { + title: '缺口', + width: 90, + render: (_, row) => { + const shortage = Math.max( + row.covered_shortage, + row.available_shortage, + row.allocatable_shortage, + ); + return shortage > 0 ? {shortage} : 0; + }, + }, + { + title: '状态', + width: 100, + render: (_, row) => ( + row.needs_replenishment + ? 需补货 + : 达标 + ), + }, + ]; + + return ( + + + {status?.checked_at ? formatTime(status.checked_at) : '-'} + + + + )} + > + {error ? : null} + + + + + + + + {status?.alerts.length ? ( + + {status.alerts.map((alert) => ( + + ))} + + ) : null} + + + ), + }} + /> + + ); +}