feat(douyu): 增加账号供给看板与阈值告警
This commit is contained in:
@@ -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="任务已中断(服务重启)")
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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<string, string>, Record<string, string>>('/douyu/task-types'),
|
||||
getAccountPlatformStatus: () =>
|
||||
api.get<DouyuAccountPlatformStatus, DouyuAccountPlatformStatus>('/douyu/account-platform-status'),
|
||||
listAccounts: (params?: { search?: string; tag?: string; ids?: string }) =>
|
||||
api.get<DouyuTaskAccountItem[], DouyuTaskAccountItem[]>('/douyu/accounts', { params }),
|
||||
listAccountsPaged: (params: PageParams & { ids?: string; tag?: string }) =>
|
||||
|
||||
@@ -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<string, Record<string, number | string | null>>;
|
||||
sync_domains: Record<string, Record<string, number>>;
|
||||
commodities: {
|
||||
by_availability: Record<string, { total: number; fresh: number }>;
|
||||
fresh_available: number;
|
||||
fresh_limited: number;
|
||||
};
|
||||
supply_targets: DouyuSupplyTargetStatus[];
|
||||
alerts: DouyuPlatformAlert[];
|
||||
}
|
||||
|
||||
export interface DouyuConfig {
|
||||
manual_id: string;
|
||||
rid: string;
|
||||
|
||||
@@ -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() {
|
||||
<Space orientation="vertical" size={16} style={{ width: '100%' }}>
|
||||
<Title level={3} style={{ margin: 0 }}>账号检测</Title>
|
||||
|
||||
<AccountPlatformStatusPanel />
|
||||
|
||||
<Card title="选择账号来源">
|
||||
<Space orientation="vertical" size={12} style={{ width: '100%' }}>
|
||||
{/* 两种来源并存:手动粘贴是旧逻辑,一条没动;
|
||||
|
||||
@@ -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<string, string> = {
|
||||
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<DouyuAccountPlatformStatus | null>(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<DouyuSupplyTargetStatus>['columns'] = [
|
||||
{
|
||||
title: '分类',
|
||||
width: 130,
|
||||
render: (_, row) => (
|
||||
<Space size={4}>
|
||||
<Tag color={row.value_class === 'high_value' ? 'gold' : 'blue'}>
|
||||
{VALUE_LABELS[row.value_class] || row.value_class}
|
||||
</Tag>
|
||||
<Text type="secondary">{row.scope}</Text>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
{
|
||||
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 ? <Text type="danger">{shortage}</Text> : <Text type="success">0</Text>;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
width: 100,
|
||||
render: (_, row) => (
|
||||
row.needs_replenishment
|
||||
? <Tag color="warning">需补货</Tag>
|
||||
: <Tag color="success">达标</Tag>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<Card
|
||||
title="账号平台状态"
|
||||
extra={(
|
||||
<Space size={8}>
|
||||
<Text type="secondary">
|
||||
{status?.checked_at ? formatTime(status.checked_at) : '-'}
|
||||
</Text>
|
||||
<Button icon={<ReloadOutlined />} loading={loading} onClick={() => void load()}>
|
||||
刷新
|
||||
</Button>
|
||||
</Space>
|
||||
)}
|
||||
>
|
||||
{error ? <Alert type="error" showIcon message={error} style={{ marginBottom: 16 }} /> : null}
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col xs={12} md={6}><Statistic title="CK 有效" value={status?.accounts.ck_valid || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="24h 预计到期" value={status?.accounts.ck_expiring_24h || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="新鲜可兑换商品" value={status?.commodities.fresh_available || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={pendingWork} /></Col>
|
||||
</Row>
|
||||
|
||||
{status?.alerts.length ? (
|
||||
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 16 }}>
|
||||
{status.alerts.map((alert) => (
|
||||
<Alert
|
||||
key={`${alert.code}-${alert.scope || ''}-${alert.value_class || ''}-${alert.message}`}
|
||||
type={alert.severity === 'critical' ? 'error' : alert.severity === 'warning' ? 'warning' : 'info'}
|
||||
showIcon
|
||||
message={alert.message}
|
||||
/>
|
||||
))}
|
||||
</Space>
|
||||
) : null}
|
||||
|
||||
<Table
|
||||
rowKey="target_id"
|
||||
size="small"
|
||||
style={{ marginTop: 16 }}
|
||||
columns={columns}
|
||||
dataSource={status?.supply_targets || []}
|
||||
pagination={false}
|
||||
scroll={{ x: 650 }}
|
||||
locale={{
|
||||
emptyText: (
|
||||
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无供给目标" />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user