- Automation section gets storage budget, retention and cache TTL cards bound to settings.storage - Metadata section drops deprecated cache.tmdbTtlHours / cache.emptyResultTtlSec / identity.poolArchiveDays inputs - cache.tmdbMaxSize default 256 -> 5000; migration 0051 bumps stored values still at the old default Co-Authored-By: Claude Opus 5.5 <[email protected]>
275 lines
11 KiB
TypeScript
275 lines
11 KiB
TypeScript
import { Pause, Play, ShieldAlert } from "lucide-react";
|
||
import type { ReactNode } from "react";
|
||
import { toast } from "sonner";
|
||
import { useUpdateSettings } from "~/api/settings.ts";
|
||
import { Switch } from "~/components/ui/switch.tsx";
|
||
import { errorMessage } from "~/lib/api.ts";
|
||
import { cn } from "~/lib/utils.ts";
|
||
import { type FormValues, num } from "./resource-section.tsx";
|
||
import { SettingsBlock } from "./settings-block.tsx";
|
||
import { type CardSpec, n, SettingsCard, SettingsGate } from "./settings-card.tsx";
|
||
|
||
// 自动化分区:总开关、只读对账、缓存水位、每日下载预算、保留天数、存储预算与保留(CACHE-2)
|
||
|
||
const GiB = 1024 ** 3;
|
||
|
||
const CACHE_CARD: CardSpec = {
|
||
anchor: "cache",
|
||
title: "缓存水位",
|
||
description: "缓存卷使用率到硬阈值时停止新增下载并暂停纳管任务;降到恢复水位以下只恢复因阈值暂停的任务。",
|
||
fields: [
|
||
{ name: "cache.hardThresholdPercent", label: "硬阈值(%)", kind: "number" },
|
||
{ name: "cache.resumePercent", label: "恢复水位(%)", kind: "number", hint: "低于硬阈值" },
|
||
{ name: "cache.cleanupTargetPercent", label: "清理目标(%)", kind: "number", hint: "不高于恢复水位" },
|
||
{ name: "cache.checkIntervalSec", label: "检查间隔(秒)", kind: "number", hint: "10–3600" },
|
||
],
|
||
fromSettings: (s) => ({
|
||
"cache.hardThresholdPercent": n(s.cache.hardThresholdPercent),
|
||
"cache.resumePercent": n(s.cache.resumePercent),
|
||
"cache.cleanupTargetPercent": n(s.cache.cleanupTargetPercent),
|
||
"cache.checkIntervalSec": n(s.cache.checkIntervalSec),
|
||
}),
|
||
toPatch: (v) => ({
|
||
cache: {
|
||
hardThresholdPercent: num(v["cache.hardThresholdPercent"]),
|
||
resumePercent: num(v["cache.resumePercent"]),
|
||
cleanupTargetPercent: num(v["cache.cleanupTargetPercent"]),
|
||
checkIntervalSec: num(v["cache.checkIntervalSec"]),
|
||
},
|
||
}),
|
||
};
|
||
|
||
const BUDGET_CARD: CardSpec = {
|
||
anchor: "budget",
|
||
title: "每日预算",
|
||
description: "任一项到上限后不再建新的下载计划;前两项按自然日计。",
|
||
fields: [
|
||
{ name: "budget.maxNewTitlesPerDay", label: "每日新作品数", kind: "number" },
|
||
{ name: "budget.maxDownloadGiBPerDay", label: "每日下载量(GiB)", kind: "number" },
|
||
{ name: "budget.maxConcurrentDownloads", label: "最大并发下载", kind: "number", hint: "1–100" },
|
||
],
|
||
fromSettings: (s) => ({
|
||
"budget.maxNewTitlesPerDay": n(s.budget.maxNewTitlesPerDay),
|
||
"budget.maxDownloadGiBPerDay": n(Math.round((s.budget.maxDownloadBytesPerDay / GiB) * 100) / 100),
|
||
"budget.maxConcurrentDownloads": n(s.budget.maxConcurrentDownloads),
|
||
}),
|
||
toPatch: (v) => {
|
||
const gib = num(v["budget.maxDownloadGiBPerDay"]);
|
||
return {
|
||
budget: {
|
||
maxNewTitlesPerDay: num(v["budget.maxNewTitlesPerDay"]),
|
||
maxDownloadBytesPerDay: gib === undefined ? undefined : Math.round(gib * GiB),
|
||
maxConcurrentDownloads: num(v["budget.maxConcurrentDownloads"]),
|
||
},
|
||
};
|
||
},
|
||
};
|
||
|
||
const RETENTION_CARD: CardSpec = {
|
||
anchor: "retention",
|
||
title: "保留",
|
||
fields: [
|
||
{
|
||
name: "retention.seedRetentionDays",
|
||
label: "源文件保留天数",
|
||
kind: "number",
|
||
hint: "复制入库后下载源在缓存里至少留这么久;0–365",
|
||
},
|
||
{ name: "retention.logRetentionDays", label: "日志保留天数", kind: "number", hint: "观察与日志类数据" },
|
||
],
|
||
fromSettings: (s) => ({
|
||
"retention.seedRetentionDays": n(s.retention.seedRetentionDays),
|
||
"retention.logRetentionDays": n(s.retention.logRetentionDays),
|
||
}),
|
||
toPatch: (v) => ({
|
||
retention: {
|
||
seedRetentionDays: num(v["retention.seedRetentionDays"]),
|
||
logRetentionDays: num(v["retention.logRetentionDays"]),
|
||
},
|
||
}),
|
||
};
|
||
|
||
// CACHE-2:存储预算、流水保留期、缓存有效期(settings.storage,docs/runbook/storage.md)
|
||
const gbField = (k: string, label: string, hint?: string) => ({
|
||
name: `storage.budgets.${k}`,
|
||
label,
|
||
kind: "number" as const,
|
||
hint,
|
||
});
|
||
const dayField = (group: "retentionDays" | "cacheTtl", k: string, label: string, hint?: string) => ({
|
||
name: `storage.${group}.${k}`,
|
||
label,
|
||
kind: "number" as const,
|
||
hint,
|
||
});
|
||
const pick = (v: FormValues, prefix: string, keys: readonly string[]) =>
|
||
Object.fromEntries(keys.map((k) => [k, num(v[`${prefix}.${k}`])]));
|
||
|
||
const BUDGET_KEYS = ["pgDataGb", "pgDataWarnGb", "imagesGb", "backupsGb", "sessionsEvidenceGb"] as const;
|
||
const STORAGE_BUDGET_CARD: CardSpec = {
|
||
anchor: "storage",
|
||
title: "存储预算",
|
||
description: "超过预算时健康页告警;图片缓存超出容量按最久未访问删除。单位 GB。",
|
||
fields: [
|
||
gbField("pgDataGb", "数据库预算", "超过为严重"),
|
||
gbField("pgDataWarnGb", "数据库告警线", "不高于数据库预算"),
|
||
gbField("imagesGb", "图片缓存容量"),
|
||
gbField("backupsGb", "备份目录预算"),
|
||
gbField("sessionsEvidenceGb", "Agent 会话与截图证据预算"),
|
||
],
|
||
fromSettings: (s) => Object.fromEntries(BUDGET_KEYS.map((k) => [`storage.budgets.${k}`, n(s.storage.budgets[k])])),
|
||
toPatch: (v) => ({ storage: { budgets: pick(v, "storage.budgets", BUDGET_KEYS) } }),
|
||
};
|
||
|
||
const RETENTION_KEYS = [
|
||
"observation",
|
||
"sseEvent",
|
||
"heartbeatHours",
|
||
"taskRun",
|
||
"agentEvent",
|
||
"audit",
|
||
"capabilityStats",
|
||
"identityPoolArchive",
|
||
] as const;
|
||
const STORAGE_RETENTION_CARD: CardSpec = {
|
||
anchor: "storage-retention",
|
||
title: "流水数据保留",
|
||
description: "到期由定时清理任务分批删除。单位天,心跳为小时。",
|
||
fields: [
|
||
dayField("retentionDays", "observation", "Feed 观察"),
|
||
dayField("retentionDays", "sseEvent", "实时事件"),
|
||
dayField("retentionDays", "heartbeatHours", "进程心跳(小时)"),
|
||
dayField("retentionDays", "taskRun", "任务运行记录"),
|
||
dayField("retentionDays", "agentEvent", "Agent 会话事件", "只删已结束的会话"),
|
||
dayField("retentionDays", "audit", "审计日志", "能力调用审计除外"),
|
||
dayField("retentionDays", "capabilityStats", "能力调用统计"),
|
||
dayField(
|
||
"retentionDays",
|
||
"identityPoolArchive",
|
||
"待识别池归档",
|
||
"未命中的条目这么久没有新证据就归档,仍可手动指定",
|
||
),
|
||
],
|
||
fromSettings: (s) =>
|
||
Object.fromEntries(RETENTION_KEYS.map((k) => [`storage.retentionDays.${k}`, n(s.storage.retentionDays[k])])),
|
||
toPatch: (v) => ({ storage: { retentionDays: pick(v, "storage.retentionDays", RETENTION_KEYS) } }),
|
||
};
|
||
|
||
const TTL_KEYS = ["tmdbSearchDays", "tmdbDetailDays", "tmdbMissDays", "siteDetailDays", "webSearchDays"] as const;
|
||
const STORAGE_TTL_CARD: CardSpec = {
|
||
anchor: "storage-ttl",
|
||
title: "缓存有效期",
|
||
description: "单位天。追踪中的作品命中 TMDB 详情缓存时自动续期。",
|
||
fields: [
|
||
dayField("cacheTtl", "tmdbSearchDays", "TMDB 搜索与识别结果"),
|
||
dayField("cacheTtl", "tmdbDetailDays", "TMDB 详情"),
|
||
dayField("cacheTtl", "tmdbMissDays", "TMDB 空结果", "搜索无结果、未识别时的负缓存"),
|
||
dayField("cacheTtl", "siteDetailDays", "站点详情页内容"),
|
||
dayField("cacheTtl", "webSearchDays", "网页搜索结果"),
|
||
],
|
||
fromSettings: (s) => Object.fromEntries(TTL_KEYS.map((k) => [`storage.cacheTtl.${k}`, n(s.storage.cacheTtl[k])])),
|
||
toPatch: (v) => ({ storage: { cacheTtl: pick(v, "storage.cacheTtl", TTL_KEYS) } }),
|
||
};
|
||
|
||
function ToggleRow({
|
||
id,
|
||
icon,
|
||
title,
|
||
description,
|
||
checked,
|
||
onChange,
|
||
disabled,
|
||
tone,
|
||
}: {
|
||
id: string;
|
||
icon: ReactNode;
|
||
title: string;
|
||
description: string;
|
||
checked: boolean;
|
||
onChange: (v: boolean) => void;
|
||
disabled: boolean;
|
||
tone: "success" | "warning";
|
||
}) {
|
||
return (
|
||
<div className="flex items-start gap-4 py-4 first:pt-0 last:pb-0">
|
||
<span
|
||
className={cn(
|
||
"grid h-9 w-9 shrink-0 place-items-center rounded-full",
|
||
tone === "success" ? "bg-success/10 text-success" : "bg-warning/10 text-warning",
|
||
)}
|
||
>
|
||
{icon}
|
||
</span>
|
||
<span className="min-w-0 flex-1">
|
||
<label htmlFor={id} className="block cursor-pointer text-sm font-medium text-foreground">
|
||
{title}
|
||
</label>
|
||
<span className="block text-caption text-muted-foreground">{description}</span>
|
||
</span>
|
||
<Switch id={id} checked={checked} onCheckedChange={onChange} disabled={disabled} aria-label={title} />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function Switches() {
|
||
const update = useUpdateSettings();
|
||
const set = async (patch: { enabled?: boolean; readOnlyReconcile?: boolean }, msg: string) => {
|
||
try {
|
||
await update.mutateAsync({ automation: patch });
|
||
toast.success(msg);
|
||
} catch (err) {
|
||
toast.error(errorMessage(err, "切换失败"));
|
||
}
|
||
};
|
||
return (
|
||
<SettingsGate>
|
||
{(s) => (
|
||
<SettingsBlock section="automation" anchor="switches" title="开关" description="切换后立即生效。">
|
||
<div className="divide-y divide-border/60">
|
||
<ToggleRow
|
||
id="tg-automation"
|
||
icon={s.automation.enabled ? <Play className="h-4 w-4" /> : <Pause className="h-4 w-4" />}
|
||
tone={s.automation.enabled ? "success" : "warning"}
|
||
title={`自动化总开关 · ${s.automation.enabled ? "运行中" : "已暂停"}`}
|
||
description="关闭后自动收录、下载、整理全部停止;查询与手动操作不受影响。"
|
||
checked={s.automation.enabled}
|
||
disabled={update.isPending}
|
||
onChange={(v) => set({ enabled: v }, v ? "自动化已恢复" : "自动化已暂停")}
|
||
/>
|
||
<ToggleRow
|
||
id="tg-readonly"
|
||
icon={<ShieldAlert className="h-4 w-4" />}
|
||
tone={s.automation.readOnlyReconcile ? "warning" : "success"}
|
||
title={`只读对账 · ${s.automation.readOnlyReconcile ? "开启" : "关闭"}`}
|
||
description="从备份恢复后先开启:只核对库存与路径,不做自动写入。核对完再关闭。"
|
||
checked={s.automation.readOnlyReconcile}
|
||
disabled={update.isPending}
|
||
onChange={(v) => set({ readOnlyReconcile: v }, v ? "已开启只读对账" : "已关闭只读对账")}
|
||
/>
|
||
</div>
|
||
</SettingsBlock>
|
||
)}
|
||
</SettingsGate>
|
||
);
|
||
}
|
||
|
||
export function AutomationSection() {
|
||
return (
|
||
<div className="grid grid-cols-1 gap-8">
|
||
<Switches />
|
||
<SettingsGate>
|
||
{(s) => (
|
||
<>
|
||
<SettingsCard section="automation" spec={CACHE_CARD} settings={s} />
|
||
<SettingsCard section="automation" spec={BUDGET_CARD} settings={s} />
|
||
<SettingsCard section="automation" spec={RETENTION_CARD} settings={s} />
|
||
<SettingsCard section="automation" spec={STORAGE_BUDGET_CARD} settings={s} />
|
||
<SettingsCard section="automation" spec={STORAGE_RETENTION_CARD} settings={s} />
|
||
<SettingsCard section="automation" spec={STORAGE_TTL_CARD} settings={s} />
|
||
</>
|
||
)}
|
||
</SettingsGate>
|
||
</div>
|
||
);
|
||
}
|