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 = { success: { color: "green", text: "成功" }, failed: { color: "red", text: "失败" }, interrupted: { color: "default", text: "被中断" }, running: { color: "blue", text: "执行中" }, skipped: { color: "default", text: "已跳过" }, }; const BUDGET_LABELS: Record = { 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 = { 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 未启用; } if (!job.next_run_at) { return 等待调度; } 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 已到点,等待调度线程; } 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.text}; } export default function AutomationPage() { const [jobs, setJobs] = useState([]); const [schedulerEnabled, setSchedulerEnabled] = useState(true); const [schedulerRunning, setSchedulerRunning] = useState(false); const [runs, setRuns] = useState([]); const [manualGroups, setManualGroups] = useState([]); const [manualTotal, setManualTotal] = useState(0); const [pendingRelogin, setPendingRelogin] = useState(0); const [loginQuota, setLoginQuota] = useState(null); const [budgetRecommendation, setBudgetRecommendation] = useState(null); const [loading, setLoading] = useState(false); const [applyingBudget, setApplyingBudget] = useState(false); const [savingBudget, setSavingBudget] = useState(false); const [savingKey, setSavingKey] = useState(null); const [runningKey, setRunningKey] = useState(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 = [ { 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 = [ { 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 ( {job.key === "ck_relogin" ? ( 待重登 {pendingRelogin} ) : null} {jobStatusTag(job.last_status)} } style={{ marginBottom: 16 }} > {spec.description} {formatTime(job.last_run_at)} {nextRunLabel(job)} {job.last_message || "-"}
{spec.showRecheck ? ( ) : null} {spec.showConcurrency ? ( ) : null} void runOnce(job)} disabled={!canManage} >
); }; 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 = ( {!schedulerEnabled ? ( ) : null} {schedulerEnabled && !schedulerRunning ? ( ) : null}
核心作业 {coreJobs.map(renderJob)} {maintenanceJobs.length ? ( ) : null} {!canManage ? ( 当前账号只能查看配置,修改需要 automation:manage 权限。 ) : null}
); const budgetTab = ( } loading={savingBudget} disabled={!canManage} onClick={() => void saveBudget()} > 保存预算 } >
{BUDGET_FIELDS.map((field) => ( ))}
{loginQuota?.updated_at ? ( 最近更新:{formatTime(loginQuota.updated_at)} ) : null}
} disabled={!canManage || !budgetRecommendation} loading={applyingBudget} onClick={() => void applyBudgetRecommendation()} > 应用建议 } > {budgetRecommendation ? ( <> 有效账号
{budgetRecommendation.basis.valid_accounts}
失效积压
{budgetRecommendation.basis.invalid_backlog}
未登录积压
{budgetRecommendation.basis.imported_backlog}
近期成功率
{budgetRecommendation.basis.recent_success_rate.toFixed(1)}%
每日成功需求
{budgetRecommendation.basis.required_daily_success}
( 0 ? "success" : value < 0 ? "danger" : "secondary"}> {value > 0 ? `+${value}` : value} ), }, ]} /> ) : ( 暂无预算建议 )} ); const manualTab = (
待人工处理({manualTotal}) 自动重登无法处理的账号,按失败类型分组。
{manualGroups.length === 0 ? ( 暂无需要人工处理的账号。 ) : ( manualGroups.map((group) => (
)) )} ); const historyTab = ( setHistoryStatusFilter(value || "")} options={Object.entries(STATUS_TAG).map(([value, item]) => ({ value, label: item.text, }))} /> } >
); return (
斗鱼自动化中心 统一管理 CK 巡检、自动重登、账号目录、供给补货、履约租约、登录预算和运行趋势。
{schedulerEnabled ? schedulerRunning ? "调度运行中" : "调度线程未运行" : "自动化总开关关闭"}
, }, { key: "jobs", label: "作业配置", children: jobsTab }, { key: "budget", label: "登录预算", children: budgetTab }, { key: "manual", label: `待人工${manualTotal ? ` (${manualTotal})` : ""}`, children: manualTab, }, { key: "history", label: "运行记录", children: historyTab }, ]} />
); }