138 lines
4.5 KiB
TypeScript
138 lines
4.5 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useState } from "react";
|
||
import Link from "next/link";
|
||
import { Activity, HardDrive, Loader2, RefreshCw } from "lucide-react";
|
||
|
||
import { Shell } from "@/components/Shell";
|
||
import { useToast } from "@/components/Toast";
|
||
import { fetchJson } from "@/lib/api";
|
||
|
||
type Health = {
|
||
monitor?: { running?: boolean; live_count?: number };
|
||
shorts?: { enabled?: boolean; last_error?: string };
|
||
tiktok?: { enabled?: boolean };
|
||
yt_long?: { enabled?: boolean };
|
||
};
|
||
|
||
type Metrics = {
|
||
disk_free_gb?: number;
|
||
shorts_quota_gap?: number;
|
||
yt_long_upload_gap?: number;
|
||
tiktok_daily_gap?: number;
|
||
failed_jobs?: number;
|
||
};
|
||
|
||
export default function OpsMobilePage() {
|
||
const toast = useToast();
|
||
const [health, setHealth] = useState<Health>({});
|
||
const [metrics, setMetrics] = useState<Metrics>({});
|
||
const [loading, setLoading] = useState(true);
|
||
|
||
const load = useCallback(async () => {
|
||
setLoading(true);
|
||
try {
|
||
const [h, m] = await Promise.all([
|
||
fetchJson("/api/health?detail=1"),
|
||
fetchJson("/api/ops/alerts/metrics"),
|
||
]);
|
||
setHealth(h as Health);
|
||
setMetrics(m as Metrics);
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, [toast]);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
const t = setInterval(() => {
|
||
if (document.hidden) return;
|
||
void load();
|
||
}, 30000);
|
||
const onVis = () => {
|
||
if (!document.hidden) void load();
|
||
};
|
||
document.addEventListener("visibilitychange", onVis);
|
||
return () => {
|
||
clearInterval(t);
|
||
document.removeEventListener("visibilitychange", onVis);
|
||
};
|
||
}, [load]);
|
||
|
||
return (
|
||
<Shell>
|
||
<div className="mx-auto max-w-lg space-y-4 p-4 pb-24">
|
||
<div className="flex items-center justify-between">
|
||
<h1 className="text-base font-semibold text-slate-100">运维速览</h1>
|
||
<button type="button" onClick={() => void load()} className="rounded-lg border border-white/10 p-2">
|
||
<RefreshCw className={`h-4 w-4 ${loading ? "animate-spin" : ""}`} />
|
||
</button>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-2 gap-3">
|
||
<Card icon={<Activity className="h-4 w-4 text-teal-400" />} label="抖音在播" value={String(health.monitor?.live_count ?? "—")} />
|
||
<Card icon={<HardDrive className="h-4 w-4 text-sky-400" />} label="磁盘 GB" value={metrics.disk_free_gb?.toFixed(1) ?? "—"} />
|
||
<Card label="Shorts 配额" value={String(metrics.shorts_quota_gap ?? "—")} />
|
||
<Card label="失败任务" value={String(metrics.failed_jobs ?? "—")} />
|
||
</div>
|
||
|
||
<div className="rounded-xl border border-white/10 bg-black/25 p-4 text-xs text-slate-400 space-y-2">
|
||
<div>监控:{health.monitor?.running ? "运行中" : "已停"}</div>
|
||
<div>Shorts:{health.shorts?.enabled ? "开" : "关"}</div>
|
||
<div>TikTok:{health.tiktok?.enabled ? "开" : "关"}</div>
|
||
<div>长视频:{health.yt_long?.enabled ? "开" : "关"}</div>
|
||
</div>
|
||
|
||
<div className="grid gap-2">
|
||
<Link href="/dashboard" className="rounded-xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-center">
|
||
任务看板
|
||
</Link>
|
||
<Link href="/youtube/health" className="rounded-xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-center">
|
||
YouTube 健康
|
||
</Link>
|
||
<Link href="/ops" className="rounded-xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-center">
|
||
完整运维页
|
||
</Link>
|
||
</div>
|
||
|
||
<button
|
||
type="button"
|
||
onClick={async () => {
|
||
try {
|
||
await fetchJson("/api/notify/test", { method: "POST" });
|
||
toast.push("ntfy 测试已发送", "ok");
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "err");
|
||
}
|
||
}}
|
||
className="w-full rounded-xl border border-sky-500/30 bg-sky-500/10 py-3 text-sm text-sky-100"
|
||
>
|
||
测试通知
|
||
</button>
|
||
</div>
|
||
</Shell>
|
||
);
|
||
}
|
||
|
||
function Card({
|
||
icon,
|
||
label,
|
||
value,
|
||
}: {
|
||
icon?: React.ReactNode;
|
||
label: string;
|
||
value: string;
|
||
}) {
|
||
return (
|
||
<div className="rounded-xl border border-white/10 bg-black/30 p-3">
|
||
<div className="mb-1 flex items-center gap-1 text-[10px] text-slate-500">
|
||
{icon}
|
||
{label}
|
||
</div>
|
||
<div className="text-xl font-semibold text-slate-100">{value}</div>
|
||
</div>
|
||
);
|
||
}
|