feat(ops): auto-poll server for live analytics/monitor updates
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
d8bf5658e6
commit
993433d380
@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import {
|
import {
|
||||||
Activity,
|
Activity,
|
||||||
Bell,
|
Bell,
|
||||||
@ -15,11 +15,13 @@ import {
|
|||||||
Server,
|
Server,
|
||||||
ShieldCheck,
|
ShieldCheck,
|
||||||
Users,
|
Users,
|
||||||
|
Wifi,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
|
|
||||||
import { Shell } from "@/components/Shell";
|
import { Shell } from "@/components/Shell";
|
||||||
import { useToast } from "@/components/Toast";
|
import { useToast } from "@/components/Toast";
|
||||||
import { fetchJson, fmtTs } from "@/lib/api";
|
import { fetchJson, fmtTs } from "@/lib/api";
|
||||||
|
import { useAutoRefresh, useNow, type RefreshOpts } from "@/lib/realtime";
|
||||||
|
|
||||||
type Probe = {
|
type Probe = {
|
||||||
ok?: boolean | null;
|
ok?: boolean | null;
|
||||||
@ -196,6 +198,7 @@ function MetricList({ title, rows }: { title: string; rows?: { name: string; val
|
|||||||
|
|
||||||
export default function OpsPage() {
|
export default function OpsPage() {
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
|
const now = useNow(1000);
|
||||||
const [tab, setTab] = useState<"analytics" | "monitor">("analytics");
|
const [tab, setTab] = useState<"analytics" | "monitor">("analytics");
|
||||||
const [data, setData] = useState<OpsSnapshot | null>(null);
|
const [data, setData] = useState<OpsSnapshot | null>(null);
|
||||||
const [overview, setOverview] = useState<OverviewSite[]>([]);
|
const [overview, setOverview] = useState<OverviewSite[]>([]);
|
||||||
@ -203,39 +206,83 @@ export default function OpsPage() {
|
|||||||
const [domain, setDomain] = useState("dylive.nomadro.com");
|
const [domain, setDomain] = useState("dylive.nomadro.com");
|
||||||
const [range, setRange] = useState("7d");
|
const [range, setRange] = useState("7d");
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [syncing, setSyncing] = useState(false);
|
||||||
|
const [live, setLive] = useState(true);
|
||||||
|
const [lastOkAt, setLastOkAt] = useState(0);
|
||||||
|
const [lastErr, setLastErr] = useState("");
|
||||||
const [busy, setBusy] = useState("");
|
const [busy, setBusy] = useState("");
|
||||||
const [title, setTitle] = useState("DouyinWEB 测试");
|
const [title, setTitle] = useState("DouyinWEB 测试");
|
||||||
const [message, setMessage] = useState("来自 /ops 控制台的 ntfy 测试消息");
|
const [message, setMessage] = useState("来自 /ops 控制台的 ntfy 测试消息");
|
||||||
const [priority, setPriority] = useState("default");
|
const [priority, setPriority] = useState("default");
|
||||||
|
const [pageVisible, setPageVisible] = useState(true);
|
||||||
const loadMonitor = useCallback(async () => {
|
const inFlight = useRef(false);
|
||||||
const r = await fetchJson("/api/ops");
|
const domainRef = useRef(domain);
|
||||||
setData(r);
|
const rangeRef = useRef(range);
|
||||||
}, []);
|
const tabRef = useRef(tab);
|
||||||
|
domainRef.current = domain;
|
||||||
const loadAnalytics = useCallback(async () => {
|
rangeRef.current = range;
|
||||||
const ov = await fetchJson(`/api/ops/analytics?range=${encodeURIComponent(range)}`);
|
tabRef.current = tab;
|
||||||
setOverview(ov?.sites || []);
|
|
||||||
const d = await fetchJson(
|
|
||||||
`/api/ops/analytics?range=${encodeURIComponent(range)}&domain=${encodeURIComponent(domain)}`
|
|
||||||
);
|
|
||||||
setDetail(d);
|
|
||||||
}, [domain, range]);
|
|
||||||
|
|
||||||
const loadAll = useCallback(async () => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
await Promise.all([loadMonitor(), loadAnalytics()]);
|
|
||||||
} catch (e) {
|
|
||||||
toast.push((e as Error).message, "err");
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [loadAnalytics, loadMonitor, toast]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void loadAll();
|
const onVis = () => setPageVisible(document.visibilityState === "visible");
|
||||||
}, [loadAll]);
|
onVis();
|
||||||
|
document.addEventListener("visibilitychange", onVis);
|
||||||
|
return () => document.removeEventListener("visibilitychange", onVis);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const loadSilent = useCallback(async (opts?: RefreshOpts) => {
|
||||||
|
const silent = Boolean(opts?.silent);
|
||||||
|
if (inFlight.current) return;
|
||||||
|
inFlight.current = true;
|
||||||
|
if (!silent) setLoading(true);
|
||||||
|
else setSyncing(true);
|
||||||
|
try {
|
||||||
|
const tasks: Promise<void>[] = [];
|
||||||
|
// Always keep analytics fresh on analytics tab; monitor on monitor tab;
|
||||||
|
// still refresh both lightly so switching tabs feels live.
|
||||||
|
tasks.push(
|
||||||
|
(async () => {
|
||||||
|
const ov = await fetchJson(`/api/ops/analytics?range=${encodeURIComponent(rangeRef.current)}`);
|
||||||
|
setOverview(ov?.sites || []);
|
||||||
|
const d = await fetchJson(
|
||||||
|
`/api/ops/analytics?range=${encodeURIComponent(rangeRef.current)}&domain=${encodeURIComponent(domainRef.current)}`
|
||||||
|
);
|
||||||
|
setDetail(d);
|
||||||
|
})()
|
||||||
|
);
|
||||||
|
tasks.push(
|
||||||
|
(async () => {
|
||||||
|
const r = await fetchJson("/api/ops");
|
||||||
|
setData(r);
|
||||||
|
})()
|
||||||
|
);
|
||||||
|
await Promise.all(tasks);
|
||||||
|
setLastOkAt(Date.now());
|
||||||
|
setLastErr("");
|
||||||
|
setLive(true);
|
||||||
|
} catch (e) {
|
||||||
|
setLive(false);
|
||||||
|
setLastErr((e as Error).message || "同步失败");
|
||||||
|
if (!silent) toast.push((e as Error).message, "err");
|
||||||
|
} finally {
|
||||||
|
inFlight.current = false;
|
||||||
|
setLoading(false);
|
||||||
|
setSyncing(false);
|
||||||
|
}
|
||||||
|
}, [toast]);
|
||||||
|
|
||||||
|
// Poll server continuously while page is visible (no manual refresh required).
|
||||||
|
useAutoRefresh(loadSilent, {
|
||||||
|
intervalMs: tab === "analytics" ? 10000 : 12000,
|
||||||
|
eventKinds: false,
|
||||||
|
enabled: pageVisible,
|
||||||
|
});
|
||||||
|
|
||||||
|
// When domain/range/tab changes, pull immediately (silent).
|
||||||
|
useEffect(() => {
|
||||||
|
if (!pageVisible) return;
|
||||||
|
void loadSilent({ silent: true });
|
||||||
|
}, [domain, range, tab, pageVisible, loadSilent]);
|
||||||
|
|
||||||
const dnsOkCount = useMemo(() => (data?.sites || []).filter((s) => s.dns_ok).length, [data]);
|
const dnsOkCount = useMemo(() => (data?.sites || []).filter((s) => s.dns_ok).length, [data]);
|
||||||
const svcOkCount = useMemo(() => (data?.services || []).filter((s) => s.ok).length, [data]);
|
const svcOkCount = useMemo(() => (data?.services || []).filter((s) => s.ok).length, [data]);
|
||||||
@ -244,6 +291,8 @@ export default function OpsPage() {
|
|||||||
[data]
|
[data]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const agoSec = lastOkAt ? Math.max(0, Math.floor((now - lastOkAt) / 1000)) : null;
|
||||||
|
|
||||||
async function sendNtfy() {
|
async function sendNtfy() {
|
||||||
setBusy("ntfy");
|
setBusy("ntfy");
|
||||||
try {
|
try {
|
||||||
@ -264,7 +313,7 @@ export default function OpsPage() {
|
|||||||
try {
|
try {
|
||||||
const r = await fetchJson("/api/ops/healthcheck", { method: "POST" });
|
const r = await fetchJson("/api/ops/healthcheck", { method: "POST" });
|
||||||
toast.push(r?.ok ? "健康检查已执行" : `健康检查结束 code=${r?.returncode}`, r?.ok ? "ok" : "err");
|
toast.push(r?.ok ? "健康检查已执行" : `健康检查结束 code=${r?.returncode}`, r?.ok ? "ok" : "err");
|
||||||
await loadMonitor();
|
await loadSilent({ silent: true });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.push((e as Error).message, "err");
|
toast.push((e as Error).message, "err");
|
||||||
} finally {
|
} finally {
|
||||||
@ -283,18 +332,33 @@ export default function OpsPage() {
|
|||||||
<div className="text-[11px] uppercase tracking-[0.18em] text-teal-200/70">Analytics + Ops</div>
|
<div className="text-[11px] uppercase tracking-[0.18em] text-teal-200/70">Analytics + Ops</div>
|
||||||
<h1 className="font-display text-2xl font-semibold text-white">访问数据与运维</h1>
|
<h1 className="font-display text-2xl font-semibold text-white">访问数据与运维</h1>
|
||||||
<p className="mt-1 max-w-2xl text-sm text-slate-400">
|
<p className="mt-1 max-w-2xl text-sm text-slate-400">
|
||||||
Umami 提供类似 Google Analytics 的 PV / 访客 / 来源 / 设备统计(偏隐私、无广告画像)。下方可切换「访问数据」与「监控」。
|
页面打开时自动与服务器同步(访问数据约 10s、监控约 12s),无需手动刷新。切换标签页离开时暂停轮询。
|
||||||
</p>
|
</p>
|
||||||
|
<div className="mt-2 flex flex-wrap items-center gap-2 text-[11px]">
|
||||||
|
<span
|
||||||
|
className={`inline-flex items-center gap-1.5 rounded-full px-2 py-0.5 ${
|
||||||
|
live ? "bg-teal-400/15 text-teal-200" : "bg-rose-400/15 text-rose-200"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<Wifi className={`h-3 w-3 ${syncing ? "animate-pulse" : ""}`} />
|
||||||
|
{pageVisible ? (live ? "实时同步中" : "同步异常") : "已暂停(标签页不可见)"}
|
||||||
|
</span>
|
||||||
|
<span className="text-slate-500">
|
||||||
|
{syncing ? "拉取中…" : agoSec == null ? "等待首次同步" : `上次更新 ${agoSec}s 前`}
|
||||||
|
</span>
|
||||||
|
{lastErr ? <span className="truncate text-rose-300">{lastErr}</span> : null}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
disabled={!!busy || loading}
|
disabled={!!busy || syncing}
|
||||||
onClick={() => void loadAll()}
|
onClick={() => void loadSilent({ silent: false })}
|
||||||
className="inline-flex items-center gap-1.5 rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-xs text-slate-200"
|
className="inline-flex items-center gap-1.5 rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-xs text-slate-200"
|
||||||
|
title="立即同步一次"
|
||||||
>
|
>
|
||||||
<RefreshCw className={`h-3.5 w-3.5 ${loading ? "animate-spin" : ""}`} />
|
<RefreshCw className={`h-3.5 w-3.5 ${loading || syncing ? "animate-spin" : ""}`} />
|
||||||
刷新
|
立即同步
|
||||||
</button>
|
</button>
|
||||||
<a
|
<a
|
||||||
href={data?.umami?.url || "https://umami.nomadro.com"}
|
href={data?.umami?.url || "https://umami.nomadro.com"}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user