Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API. Co-authored-by: Cursor <cursoragent@cursor.com>
267 lines
8.9 KiB
TypeScript
267 lines
8.9 KiB
TypeScript
"use client";
|
||
|
||
import Link from "next/link";
|
||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||
import styles from "../admin.module.css";
|
||
|
||
type Latency = { key: string; ok: boolean; latencyMs: number; status: number };
|
||
type AnalyticsPayload = {
|
||
generatedAt?: string;
|
||
charts?: {
|
||
latencies: Latency[];
|
||
modules: {
|
||
total: number;
|
||
enabled: number;
|
||
disabled: number;
|
||
items: { id: string; title: string; enabled: boolean }[];
|
||
};
|
||
flags?: Record<string, boolean | undefined>;
|
||
copy?: { vipPriceFen?: number; homeBrand?: string };
|
||
};
|
||
anchors?: Record<string, string>;
|
||
ntfy?: { configured: boolean };
|
||
};
|
||
|
||
function LatencyBars({ rows }: { rows: Latency[] }) {
|
||
const max = Math.max(1, ...rows.map((r) => r.latencyMs));
|
||
return (
|
||
<div className={styles.chartWrap} id="latency">
|
||
<svg viewBox={`0 0 640 ${Math.max(120, rows.length * 36 + 16)}`} role="img" aria-label="延迟柱状图">
|
||
{rows.map((r, i) => {
|
||
const y = 12 + i * 36;
|
||
const w = Math.max(4, (r.latencyMs / max) * 420);
|
||
return (
|
||
<g key={r.key}>
|
||
<text x="0" y={y + 14} className={styles.chartLabel}>
|
||
{r.key}
|
||
</text>
|
||
<rect
|
||
x="140"
|
||
y={y}
|
||
width={w}
|
||
height="20"
|
||
rx="6"
|
||
fill={r.ok ? "var(--admin-accent)" : "var(--admin-coral)"}
|
||
opacity={0.85}
|
||
/>
|
||
<text x={150 + w} y={y + 14} className={styles.chartValue}>
|
||
{r.latencyMs}ms · {r.ok ? "OK" : "DOWN"}
|
||
</text>
|
||
</g>
|
||
);
|
||
})}
|
||
</svg>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function ModuleDonut({ enabled, disabled }: { enabled: number; disabled: number }) {
|
||
const total = Math.max(1, enabled + disabled);
|
||
const r = 54;
|
||
const c = 2 * Math.PI * r;
|
||
const en = (enabled / total) * c;
|
||
return (
|
||
<div className={styles.donutWrap} id="modules">
|
||
<svg viewBox="0 0 140 140" width="140" height="140" aria-label="模块开关占比">
|
||
<circle cx="70" cy="70" r={r} fill="none" stroke="rgba(255,255,255,0.08)" strokeWidth="16" />
|
||
<circle
|
||
cx="70"
|
||
cy="70"
|
||
r={r}
|
||
fill="none"
|
||
stroke="var(--admin-accent)"
|
||
strokeWidth="16"
|
||
strokeDasharray={`${en} ${c - en}`}
|
||
strokeLinecap="round"
|
||
transform="rotate(-90 70 70)"
|
||
/>
|
||
<text x="70" y="66" textAnchor="middle" className={styles.donutNum}>
|
||
{enabled}/{total}
|
||
</text>
|
||
<text x="70" y="86" textAnchor="middle" className={styles.donutCap}>
|
||
模块开
|
||
</text>
|
||
</svg>
|
||
<ul className={styles.listPlain}>
|
||
<li>● 已开 {enabled}</li>
|
||
<li>○ 已关 {disabled}</li>
|
||
</ul>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default function AdminAnalyticsPage() {
|
||
const [data, setData] = useState<AnalyticsPayload | null>(null);
|
||
const [err, setErr] = useState("");
|
||
const [ntfyMsg, setNtfyMsg] = useState("后台手动测试 · 请确认手机/桌面已订阅 topic");
|
||
const [ntfyBusy, setNtfyBusy] = useState(false);
|
||
const [ntfyResult, setNtfyResult] = useState("");
|
||
|
||
const load = useCallback(() => {
|
||
setErr("");
|
||
fetch("/api/admin/analytics", { cache: "no-store" })
|
||
.then(async (r) => {
|
||
const j = await r.json();
|
||
if (!r.ok) throw new Error(j.error || r.statusText);
|
||
setData(j);
|
||
})
|
||
.catch((e) => setErr(e instanceof Error ? e.message : String(e)));
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
load();
|
||
const t = window.setInterval(load, 60_000);
|
||
return () => window.clearInterval(t);
|
||
}, [load]);
|
||
|
||
const latencies = data?.charts?.latencies || [];
|
||
const modules = data?.charts?.modules;
|
||
const okCount = useMemo(() => latencies.filter((l) => l.ok).length, [latencies]);
|
||
|
||
async function sendNtfy() {
|
||
setNtfyBusy(true);
|
||
setNtfyResult("");
|
||
try {
|
||
const res = await fetch("/api/admin/notify", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({
|
||
title: "Nomadro 后台",
|
||
message: ntfyMsg,
|
||
tags: "chart_with_upwards_trend,nomadro",
|
||
priority: "high",
|
||
}),
|
||
});
|
||
const j = await res.json().catch(() => ({}));
|
||
setNtfyResult(j.ok ? "✅ 已推送 ntfy" : `❌ ${j.error || j.skipped || res.status}`);
|
||
} finally {
|
||
setNtfyBusy(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<>
|
||
<h1 className={styles.pageTitle} id="top">
|
||
📈 统计分析
|
||
</h1>
|
||
<p className={styles.pageLead}>
|
||
探针延迟 · 模块开关 · Umami / Uptime-Kuma · 即时 ntfy。生成于{" "}
|
||
{data?.generatedAt ? new Date(data.generatedAt).toLocaleString() : "…"}
|
||
{" · "}
|
||
<button type="button" className={styles.chip} onClick={load}>
|
||
刷新
|
||
</button>
|
||
</p>
|
||
{err ? <p className={styles.error}>{err}</p> : null}
|
||
|
||
<nav className={styles.anchorNav} aria-label="页面锚点">
|
||
<a href="#latency">延迟</a>
|
||
<a href="#modules">模块</a>
|
||
<a href="#ntfy">ntfy</a>
|
||
<a href="#umami">Umami</a>
|
||
<a href="#links">外链</a>
|
||
</nav>
|
||
|
||
<div className={styles.grid2} style={{ marginBottom: 16 }}>
|
||
<div className={styles.panel}>
|
||
<h3>探针健康</h3>
|
||
<p className={styles.statBig}>
|
||
{okCount}/{latencies.length || "—"}
|
||
</p>
|
||
<p className={styles.muted}>OK / 全部探测点</p>
|
||
</div>
|
||
<div className={styles.panel}>
|
||
<h3>VIP 展示价</h3>
|
||
<p className={styles.statBig}>
|
||
¥{(((data?.charts?.copy?.vipPriceFen || 0) as number) / 100).toFixed(0)}
|
||
</p>
|
||
<p className={styles.muted}>{data?.charts?.copy?.homeBrand || "nomadro"}</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div className={styles.panel} style={{ marginBottom: 16 }}>
|
||
<h3>服务延迟(ms)</h3>
|
||
{latencies.length ? <LatencyBars rows={latencies} /> : <p className={styles.muted}>加载中…</p>}
|
||
</div>
|
||
|
||
<div className={styles.grid2} style={{ marginBottom: 16 }}>
|
||
<div className={styles.panel}>
|
||
<h3>模块开关</h3>
|
||
{modules ? (
|
||
<ModuleDonut enabled={modules.enabled} disabled={modules.disabled} />
|
||
) : (
|
||
<p className={styles.muted}>…</p>
|
||
)}
|
||
<ul className={styles.listPlain} style={{ marginTop: 12 }}>
|
||
{(modules?.items || []).map((m) => (
|
||
<li key={m.id}>
|
||
{m.enabled ? "●" : "○"} {m.title || m.id}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</div>
|
||
|
||
<div className={styles.panel} id="ntfy">
|
||
<h3>即时 ntfy</h3>
|
||
<p className={styles.muted}>
|
||
配置:{data?.ntfy?.configured ? "已就绪" : "缺 NTFY_* env(仍可试推公网 topic)"}
|
||
</p>
|
||
<textarea
|
||
className={styles.textarea}
|
||
rows={3}
|
||
value={ntfyMsg}
|
||
onChange={(e) => setNtfyMsg(e.target.value)}
|
||
/>
|
||
<p style={{ marginTop: 8 }}>
|
||
<button type="button" className={styles.primaryBtn} disabled={ntfyBusy} onClick={sendNtfy}>
|
||
{ntfyBusy ? "推送中…" : "推送到 ntfy"}
|
||
</button>
|
||
</p>
|
||
{ntfyResult ? <p className={styles.muted}>{ntfyResult}</p> : null}
|
||
<p className={styles.muted} style={{ marginTop: 8 }}>
|
||
订阅:{" "}
|
||
<a href="https://ntfy.nomadro.cn" target="_blank" rel="noreferrer">
|
||
ntfy.nomadro.cn
|
||
</a>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div className={styles.panel} id="umami" style={{ marginBottom: 16 }}>
|
||
<h3>Umami 行为分析</h3>
|
||
<p className={styles.muted}>自托管 · 完整报表在独立控制台(iframe 可能被 CSP 限制)</p>
|
||
<p style={{ marginTop: 8 }}>
|
||
<a className={styles.chip} href="https://umami.nomadro.cn" target="_blank" rel="noreferrer">
|
||
打开 Umami →
|
||
</a>{" "}
|
||
<a className={styles.chip} href="https://uptimekuma.nomadro.cn" target="_blank" rel="noreferrer">
|
||
Uptime-Kuma →
|
||
</a>
|
||
</p>
|
||
</div>
|
||
|
||
<div className={styles.panel} id="links">
|
||
<h3>运维锚点</h3>
|
||
<ul className={styles.listPlain}>
|
||
{Object.entries(data?.anchors || {}).map(([k, href]) => (
|
||
<li key={k}>
|
||
<code>{k}</code>{" "}
|
||
<a href={href} target={href.startsWith("http") ? "_blank" : undefined} rel="noreferrer">
|
||
{href}
|
||
</a>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
<p style={{ marginTop: 12 }}>
|
||
<Link href="/admin/ops" className={styles.chip}>
|
||
运维面板 →
|
||
</Link>{" "}
|
||
<Link href="/admin/config" className={styles.chip}>
|
||
远程配置 →
|
||
</Link>
|
||
</p>
|
||
</div>
|
||
</>
|
||
);
|
||
}
|