Files
live-hub-py/web/frontend/src/pages/AutomationPage.tsx
T

816 lines
26 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ReactNode } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import {
Alert,
Button,
Card,
Collapse,
Col,
Descriptions,
Form,
InputNumber,
Popconfirm,
Row,
Select,
Space,
Switch,
Table,
Tabs,
Tag,
Typography,
} from "antd";
import type { ColumnsType } from "antd/es/table";
import {
CheckCircleOutlined,
ReloadOutlined,
SaveOutlined,
ThunderboltOutlined,
} from "@ant-design/icons";
import { message } from "../utils/antdMessage";
import { getErrorMessage } from "../utils/error";
import {
automationApi,
type AutomationJobItem,
type AutomationLoginQuota,
type AutomationLoginQuotaRecommendation,
type AutomationManualGroup,
type AutomationManualSample,
type AutomationRunItem,
} from "../api/modules";
import { usePermissions } from "../hooks/usePermissions";
import { formatTime } from "../utils/time";
import AccountPlatformStatusPanel from "./douyu-tasks/AccountPlatformStatusPanel";
const { Text, Title } = Typography;
const STATUS_TAG: Record<string, { color: string; text: string }> = {
success: { color: "green", text: "成功" },
failed: { color: "red", text: "失败" },
interrupted: { color: "default", text: "被中断" },
running: { color: "blue", text: "执行中" },
skipped: { color: "default", text: "已跳过" },
};
const BUDGET_LABELS: Record<string, string> = {
daily_attempt_limit: "全局每日尝试",
hourly_attempt_limit: "全局每小时尝试",
per_account_daily_limit: "单账号每日尝试",
onboarding_daily_limit: "首次登录",
relogin_high_value_daily_limit: "高价值账号重登",
relogin_ordinary_daily_limit: "普通账号重登",
manual_daily_limit: "人工登录保留",
login_concurrency: "登录并发",
failure_pause_ratio: "失败率熔断",
};
type BudgetFieldName =
| "daily_attempt_limit"
| "hourly_attempt_limit"
| "per_account_daily_limit"
| "onboarding_daily_limit"
| "relogin_high_value_daily_limit"
| "relogin_ordinary_daily_limit"
| "manual_daily_limit"
| "login_concurrency"
| "failure_pause_ratio";
const BUDGET_FIELDS: Array<{
name: BudgetFieldName;
label: string;
min: number;
max: number;
suffix?: string;
}> = [
{ name: "daily_attempt_limit", label: "全局每日尝试", min: 1, max: 1_000_000 },
{ name: "hourly_attempt_limit", label: "全局每小时尝试", min: 1, max: 100_000 },
{ name: "per_account_daily_limit", label: "单账号每日尝试", min: 1, max: 100 },
{ name: "onboarding_daily_limit", label: "首次登录预算", min: 0, max: 100_000 },
{ name: "relogin_high_value_daily_limit", label: "高价值重登预算", min: 0, max: 100_000 },
{ name: "relogin_ordinary_daily_limit", label: "普通重登预算", min: 0, max: 100_000 },
{ name: "manual_daily_limit", label: "人工登录保留", min: 0, max: 100_000 },
{ name: "login_concurrency", label: "登录并发", min: 1, max: 10 },
{ name: "failure_pause_ratio", label: "失败率熔断阈值", min: 0, max: 100, suffix: "%" },
];
interface JobUiSpec {
description: string;
core: boolean;
showRecheck: boolean;
showConcurrency: boolean;
}
const JOB_UI: Record<string, JobUiSpec> = {
ck_health: {
description: "只读取现有 CK 判断登录状态;发现失效后交给自动重登,不替换 Cookie。",
core: true,
showRecheck: true,
showConcurrency: true,
},
ck_relogin: {
description: "对巡检标记为失效的账号执行真实登录,消耗登录预算、邮箱验证码和代理资源。",
core: true,
showRecheck: true,
showConcurrency: true,
},
account_sync_queue: {
description: "独立消费账号目录、积分、鱼翅和角色同步工作,避免阻塞订单选号。",
core: true,
showRecheck: false,
showConcurrency: true,
},
account_capability_refresh: {
description: "按待履约商品补充全量账号能力目录,只做本地筛选,外部查询交给同步队列。",
core: true,
showRecheck: false,
showConcurrency: true,
},
account_supply_replenishment: {
description: "根据高价值和普通账号的覆盖、可兑换、可分配缺口补充候选账号。",
core: true,
showRecheck: false,
showConcurrency: false,
},
platform_fulfillment_lease_reaper: {
description: "回收过期履约租约和扫码超时任务占用的账号,防止账号池被永久锁死。",
core: true,
showRecheck: false,
showConcurrency: false,
},
points_expire_check: {
description: "记录积分到期事件,属于低频事实补记,不参与账号供给调度。",
core: false,
showRecheck: false,
showConcurrency: false,
},
douyu_tasks_retention: {
description: "分批清理过期任务日志,属于低频维护作业。",
core: false,
showRecheck: false,
showConcurrency: false,
},
};
const DEFAULT_JOB_UI: JobUiSpec = {
description: "斗鱼自动化后台作业。",
core: false,
showRecheck: false,
showConcurrency: false,
};
function nextRunLabel(job: AutomationJobItem): ReactNode {
if (!job.enabled) {
return <Text type="secondary">未启用</Text>;
}
if (!job.next_run_at) {
return <Text type="secondary">等待调度</Text>;
}
const next = new Date(job.next_run_at).getTime();
if (!Number.isFinite(next)) return formatTime(job.next_run_at);
if (next > Date.now()) return formatTime(job.next_run_at);
return <Text type="warning">已到点,等待调度线程</Text>;
}
function secondsToHours(seconds: number): number {
return Math.round((seconds / 3600) * 10) / 10;
}
function formatDuration(start: string | null, end: string | null): string {
if (!start || !end) return "-";
const ms = new Date(end).getTime() - new Date(start).getTime();
if (!Number.isFinite(ms) || ms < 0) return "-";
if (ms < 1000) return `${ms}ms`;
return `${(ms / 1000).toFixed(1)}s`;
}
function jobStatusTag(status: string) {
const tag = STATUS_TAG[status] ?? { color: "default", text: status || "-" };
return <Tag color={tag.color}>{tag.text}</Tag>;
}
export default function AutomationPage() {
const [jobs, setJobs] = useState<AutomationJobItem[]>([]);
const [schedulerEnabled, setSchedulerEnabled] = useState(true);
const [schedulerRunning, setSchedulerRunning] = useState(false);
const [runs, setRuns] = useState<AutomationRunItem[]>([]);
const [manualGroups, setManualGroups] = useState<AutomationManualGroup[]>([]);
const [manualTotal, setManualTotal] = useState(0);
const [pendingRelogin, setPendingRelogin] = useState(0);
const [loginQuota, setLoginQuota] = useState<AutomationLoginQuota | null>(null);
const [budgetRecommendation, setBudgetRecommendation] =
useState<AutomationLoginQuotaRecommendation | null>(null);
const [loading, setLoading] = useState(false);
const [applyingBudget, setApplyingBudget] = useState(false);
const [savingBudget, setSavingBudget] = useState(false);
const [savingKey, setSavingKey] = useState<string | null>(null);
const [runningKey, setRunningKey] = useState<string | null>(null);
const [historyJobFilter, setHistoryJobFilter] = useState("");
const [historyStatusFilter, setHistoryStatusFilter] = useState("");
const [jobForms] = Form.useForm();
const [budgetForm] = Form.useForm();
const { can } = usePermissions();
const canManage = can("automation:manage");
const jobTitles = useMemo(
() => Object.fromEntries(jobs.map((job) => [job.key, job.title])),
[jobs],
);
const load = useCallback(async () => {
setLoading(true);
try {
const [data, recommendation, quota] = await Promise.all([
automationApi.jobs(),
automationApi.loginQuotaRecommendation(),
automationApi.loginQuota(),
]);
setJobs(data.jobs);
setBudgetRecommendation(recommendation);
setLoginQuota(quota);
setPendingRelogin(data.pending_relogin ?? 0);
setSchedulerEnabled(data.scheduler.enabled);
setSchedulerRunning(data.scheduler.running);
budgetForm.setFieldsValue(quota);
const [history, manual] = await Promise.all([
automationApi.runs({ limit: 80 }),
automationApi.manualQueue(),
]);
setRuns(history.items);
setManualGroups(manual.groups);
setManualTotal(manual.total);
jobForms.setFieldsValue(
Object.fromEntries(
data.jobs.map((job) => [
job.key,
{
enabled: job.enabled,
run_interval_minutes: Math.max(
Math.round((job.run_interval_seconds ?? 300) / 60),
1,
),
interval_hours: secondsToHours(job.interval_seconds),
batch_size: job.batch_size,
concurrency: job.concurrency,
},
]),
),
);
} catch (error: unknown) {
message.error(getErrorMessage(error));
} finally {
setLoading(false);
}
}, [budgetForm, jobForms]);
useEffect(() => {
void load();
}, [load]);
const saveJob = async (job: AutomationJobItem) => {
const values = jobForms.getFieldValue(job.key) || {};
setSavingKey(job.key);
try {
await automationApi.updateJob(job.key, {
enabled: Boolean(values.enabled),
run_interval_seconds: Math.max(
Math.round((values.run_interval_minutes ?? 5) * 60),
30,
),
interval_seconds: Math.max(
Math.round((values.interval_hours ?? 6) * 3600),
300,
),
batch_size: values.batch_size,
concurrency: values.concurrency,
});
message.success(`${job.title} 配置已保存`);
await load();
} catch (error: unknown) {
message.error(getErrorMessage(error));
} finally {
setSavingKey(null);
}
};
const runOnce = async (job: AutomationJobItem) => {
setRunningKey(job.key);
try {
const outcome = await automationApi.runJob(job.key);
message.success(
`${job.title}已执行:扫描 ${outcome.scanned},成功 ${outcome.valid},失败 ${outcome.invalid}`,
);
await load();
} catch (error: unknown) {
message.error(getErrorMessage(error));
} finally {
setRunningKey(null);
}
};
const saveBudget = async () => {
const values = await budgetForm.validateFields();
setSavingBudget(true);
try {
await automationApi.updateLoginQuota({
...(loginQuota || {}),
...values,
enabled: Boolean(values.enabled),
} as AutomationLoginQuota);
message.success("登录预算已保存");
await load();
} catch (error: unknown) {
message.error(getErrorMessage(error));
} finally {
setSavingBudget(false);
}
};
const applyBudgetRecommendation = async () => {
if (!budgetRecommendation) return;
setApplyingBudget(true);
try {
await automationApi.updateLoginQuota({
...budgetRecommendation.current,
...budgetRecommendation.recommended,
});
message.success("已应用预算建议");
await load();
} catch (error: unknown) {
message.error(getErrorMessage(error));
} finally {
setApplyingBudget(false);
}
};
const manualColumns: ColumnsType<AutomationManualSample> = [
{ title: "账号 ID", dataIndex: "account_id", width: 90 },
{ title: "账号", dataIndex: "account_username", width: 180, ellipsis: true },
{ title: "已尝试", dataIndex: "relogin_attempts", width: 90 },
{ title: "失败原因", dataIndex: "last_error", ellipsis: true },
{
title: "最近尝试",
dataIndex: "attempt_at",
width: 170,
render: (value: string | null) => formatTime(value),
},
];
const filteredRuns = useMemo(
() =>
runs.filter(
(run) =>
(!historyJobFilter || run.job_key === historyJobFilter) &&
(!historyStatusFilter || run.status === historyStatusFilter),
),
[historyJobFilter, historyStatusFilter, runs],
);
const runColumns: ColumnsType<AutomationRunItem> = [
{
title: "开始时间",
dataIndex: "started_at",
width: 170,
render: (value: string | null) => formatTime(value),
},
{
title: "作业",
dataIndex: "job_key",
width: 170,
render: (value: string) => jobTitles[value] || value,
},
{
title: "结果",
dataIndex: "status",
width: 90,
render: (value: string) => jobStatusTag(value),
},
{
title: "处理量",
width: 210,
render: (_, row) => {
const parts = [];
if (row.scanned) parts.push(`扫描 ${row.scanned}`);
if (row.valid) parts.push(`成功 ${row.valid}`);
if (row.invalid) parts.push(`失败 ${row.invalid}`);
if (row.skipped) parts.push(`跳过 ${row.skipped}`);
return parts.join(" / ") || "-";
},
},
{
title: "耗时",
width: 90,
render: (_, row) =>
row.scanned > 0 ? formatDuration(row.started_at, row.finished_at) : "-",
},
{ title: "说明", dataIndex: "message", ellipsis: true },
];
const budgetRows = budgetRecommendation
? Object.entries(BUDGET_LABELS).map(([key, label]) => ({
key,
label,
current: Number(
budgetRecommendation.current[
key as keyof typeof budgetRecommendation.current
] ?? 0,
),
recommended: Number(
budgetRecommendation.recommended[
key as keyof typeof budgetRecommendation.recommended
] ?? 0,
),
delta: Number(budgetRecommendation.delta[key] ?? 0),
}))
: [];
const renderJob = (job: AutomationJobItem) => {
const spec = JOB_UI[job.key] || DEFAULT_JOB_UI;
return (
<Card
key={job.key}
title={job.title}
extra={
<Space size={8}>
{job.key === "ck_relogin" ? (
<Tag color={pendingRelogin ? "orange" : "default"}>
待重登 {pendingRelogin}
</Tag>
) : null}
{jobStatusTag(job.last_status)}
</Space>
}
style={{ marginBottom: 16 }}
>
<Text type="secondary">{spec.description}</Text>
<Descriptions size="small" column={{ xs: 1, md: 2 }} style={{ marginTop: 12 }}>
<Descriptions.Item label="最近运行">
{formatTime(job.last_run_at)}
</Descriptions.Item>
<Descriptions.Item label="下次运行">{nextRunLabel(job)}</Descriptions.Item>
<Descriptions.Item label="最近说明" span={2}>
{job.last_message || "-"}
</Descriptions.Item>
</Descriptions>
<div
style={{
display: "flex",
flexWrap: "wrap",
alignItems: "flex-end",
gap: 16,
}}
>
<Form.Item
name={[job.key, "enabled"]}
label="启用"
valuePropName="checked"
style={{ marginBottom: 0 }}
>
<Switch disabled={!canManage} />
</Form.Item>
<Form.Item
name={[job.key, "run_interval_minutes"]}
label="运行间隔(分钟)"
tooltip="上一轮结束后,隔多久再开始下一轮。"
style={{ marginBottom: 0 }}
>
<InputNumber min={0.5} step={5} disabled={!canManage} />
</Form.Item>
{spec.showRecheck ? (
<Form.Item
name={[job.key, "interval_hours"]}
label="单账号重试间隔(小时)"
tooltip="同一账号进入下一轮之前需要等待的退避时间。"
style={{ marginBottom: 0 }}
>
<InputNumber min={0.1} step={1} disabled={!canManage} />
</Form.Item>
) : null}
<Form.Item
name={[job.key, "batch_size"]}
label="单轮数量"
style={{ marginBottom: 0 }}
>
<InputNumber min={1} max={2000} disabled={!canManage} />
</Form.Item>
{spec.showConcurrency ? (
<Form.Item
name={[job.key, "concurrency"]}
label="并发"
style={{ marginBottom: 0 }}
>
<InputNumber min={1} max={16} disabled={!canManage} />
</Form.Item>
) : null}
<Space size={8}>
<Button
type="primary"
icon={<SaveOutlined />}
disabled={!canManage}
loading={savingKey === job.key}
onClick={() => void saveJob(job)}
>
保存
</Button>
<Popconfirm
title={`立即执行「${job.title}」?`}
description="忽略启用开关,立即执行一轮。"
onConfirm={() => void runOnce(job)}
disabled={!canManage}
>
<Button
icon={<ThunderboltOutlined />}
disabled={!canManage}
loading={runningKey === job.key}
>
立即执行
</Button>
</Popconfirm>
</Space>
</div>
</Card>
);
};
const coreJobs = jobs.filter((job) => (JOB_UI[job.key] || DEFAULT_JOB_UI).core);
const maintenanceJobs = jobs.filter(
(job) => !(JOB_UI[job.key] || DEFAULT_JOB_UI).core,
);
const jobsTab = (
<Space orientation="vertical" size={16} style={{ width: "100%" }}>
{!schedulerEnabled ? (
<Alert
type="warning"
showIcon
message="服务端自动化总开关未打开"
description="需要设置 AUTOMATION_ENABLED=true 并重启服务,下面的作业开关才会真正执行。"
/>
) : null}
{schedulerEnabled && !schedulerRunning ? (
<Alert
type="error"
showIcon
message="调度线程未运行"
description="总开关已打开,但调度线程没有运行,请检查服务日志。"
/>
) : null}
<Alert
type="info"
showIcon
message="核心自动化按独立执行槽运行"
description="CK 巡检只读;自动重登会真实登录。账号同步、能力补货和租约回收互不共用订单选号执行器。"
/>
<Form form={jobForms} layout="vertical">
<Title level={5}>核心作业</Title>
{coreJobs.map(renderJob)}
{maintenanceJobs.length ? (
<Collapse
items={[
{
key: "maintenance",
label: `低频维护作业(${maintenanceJobs.length})`,
children: maintenanceJobs.map(renderJob),
},
]}
/>
) : null}
</Form>
{!canManage ? (
<Text type="secondary">当前账号只能查看配置,修改需要 automation:manage 权限。</Text>
) : null}
</Space>
);
const budgetTab = (
<Space orientation="vertical" size={16} style={{ width: "100%" }}>
<Card
title="登录预算与并发"
extra={
<Button
type="primary"
icon={<SaveOutlined />}
loading={savingBudget}
disabled={!canManage}
onClick={() => void saveBudget()}
>
保存预算
</Button>
}
>
<Form form={budgetForm} layout="vertical">
<Row gutter={16}>
<Col xs={24} md={8}>
<Form.Item name="enabled" label="启用登录预算" valuePropName="checked">
<Switch disabled={!canManage} />
</Form.Item>
</Col>
{BUDGET_FIELDS.map((field) => (
<Col xs={24} sm={12} lg={8} key={field.name}>
<Form.Item name={field.name} label={field.label}>
<InputNumber
min={field.min}
max={field.max}
precision={0}
addonAfter={field.suffix}
disabled={!canManage}
style={{ width: "100%" }}
/>
</Form.Item>
</Col>
))}
</Row>
</Form>
{loginQuota?.updated_at ? (
<Text type="secondary">最近更新:{formatTime(loginQuota.updated_at)}</Text>
) : null}
</Card>
<Card
title="建议预算"
extra={
<Button
icon={<CheckCircleOutlined />}
disabled={!canManage || !budgetRecommendation}
loading={applyingBudget}
onClick={() => void applyBudgetRecommendation()}
>
应用建议
</Button>
}
>
{budgetRecommendation ? (
<>
<Row gutter={[16, 12]} style={{ marginBottom: 16 }}>
<Col xs={12} md={8} lg={4}>
<Text type="secondary">有效账号</Text>
<div>{budgetRecommendation.basis.valid_accounts}</div>
</Col>
<Col xs={12} md={8} lg={4}>
<Text type="secondary">失效积压</Text>
<div>{budgetRecommendation.basis.invalid_backlog}</div>
</Col>
<Col xs={12} md={8} lg={4}>
<Text type="secondary">未登录积压</Text>
<div>{budgetRecommendation.basis.imported_backlog}</div>
</Col>
<Col xs={12} md={8} lg={4}>
<Text type="secondary">近期成功率</Text>
<div>{budgetRecommendation.basis.recent_success_rate.toFixed(1)}%</div>
</Col>
<Col xs={12} md={8} lg={4}>
<Text type="secondary">每日成功需求</Text>
<div>{budgetRecommendation.basis.required_daily_success}</div>
</Col>
</Row>
<Table
rowKey="key"
size="small"
pagination={false}
dataSource={budgetRows}
columns={[
{ title: "预算项", dataIndex: "label" },
{ title: "当前", dataIndex: "current", width: 100 },
{ title: "建议", dataIndex: "recommended", width: 100 },
{
title: "差额",
dataIndex: "delta",
width: 90,
render: (value: number) => (
<Text type={value > 0 ? "success" : value < 0 ? "danger" : "secondary"}>
{value > 0 ? `+${value}` : value}
</Text>
),
},
]}
/>
</>
) : (
<Text type="secondary">暂无预算建议</Text>
)}
</Card>
</Space>
);
const manualTab = (
<Space orientation="vertical" size={12} style={{ width: "100%" }}>
<div>
<Title level={5} style={{ margin: 0 }}>
待人工处理({manualTotal})
</Title>
<Text type="secondary">自动重登无法处理的账号,按失败类型分组。</Text>
</div>
{manualGroups.length === 0 ? (
<Text type="secondary">暂无需要人工处理的账号。</Text>
) : (
manualGroups.map((group) => (
<Card
key={group.kind || "exhausted"}
type="inner"
title={`${group.label}(${group.count})`}
style={{ marginBottom: 12 }}
>
<Table
rowKey="account_id"
size="small"
pagination={false}
dataSource={group.samples}
columns={manualColumns}
/>
</Card>
))
)}
</Space>
);
const historyTab = (
<Card
title="自动化运行记录"
extra={
<Space wrap>
<Select
allowClear
placeholder="全部作业"
style={{ width: 190 }}
value={historyJobFilter || undefined}
onChange={(value) => setHistoryJobFilter(value || "")}
options={jobs.map((job) => ({ value: job.key, label: job.title }))}
/>
<Select
allowClear
placeholder="全部结果"
style={{ width: 120 }}
value={historyStatusFilter || undefined}
onChange={(value) => setHistoryStatusFilter(value || "")}
options={Object.entries(STATUS_TAG).map(([value, item]) => ({
value,
label: item.text,
}))}
/>
</Space>
}
>
<Table
rowKey="id"
size="small"
loading={loading}
columns={runColumns}
dataSource={filteredRuns}
pagination={{ pageSize: 20, showSizeChanger: false }}
/>
</Card>
);
return (
<Space orientation="vertical" size={16} style={{ width: "100%" }}>
<div
style={{
display: "flex",
alignItems: "flex-start",
justifyContent: "space-between",
gap: 16,
flexWrap: "wrap",
}}
>
<div>
<Title level={3} style={{ margin: 0 }}>
斗鱼自动化中心
</Title>
<Text type="secondary">
统一管理 CK 巡检、自动重登、账号目录、供给补货、履约租约、登录预算和运行趋势。
</Text>
</div>
<Space wrap>
<Tag color={schedulerEnabled ? (schedulerRunning ? "green" : "red") : "default"}>
{schedulerEnabled
? schedulerRunning
? "调度运行中"
: "调度线程未运行"
: "自动化总开关关闭"}
</Tag>
<Button icon={<ReloadOutlined />} loading={loading} onClick={() => void load()}>
刷新配置
</Button>
</Space>
</div>
<Tabs
destroyOnHidden
items={[
{
key: "overview",
label: "运行总览",
children: <AccountPlatformStatusPanel />,
},
{ key: "jobs", label: "作业配置", children: jobsTab },
{ key: "budget", label: "登录预算", children: budgetTab },
{
key: "manual",
label: `待人工${manualTotal ? ` (${manualTotal})` : ""}`,
children: manualTab,
},
{ key: "history", label: "运行记录", children: historyTab },
]}
/>
</Space>
);
}