"use client";
import { useEffect, useState } from "react";
import dynamic from "next/dynamic";
import { RefreshCcw, CheckCircle2, XCircle, AlertTriangle } from "lucide-react";
import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
const HealthLatencyChart = dynamic(
() => import("./HealthLatencyChart").then((mod) => mod.HealthLatencyChart),
{
ssr: false,
loading: () =>
,
},
);
type ServiceResult = {
ok: boolean;
status?: number;
latency_ms?: number;
error?: string;
credential_configured?: boolean;
points?: number;
observation_time_local?: string;
sample_city?: string;
};
type HealthPayload = { ok: boolean; checked_at: string; services: Record };
const LABELS: Record = {
supabase: "Supabase",
open_meteo: "Open-Meteo",
metar: "METAR (AviationWeather)",
knmi: "KNMI (Amsterdam)",
madis: "MADIS (NOAA)",
telegram: "Telegram Bot",
jma: "JMA (日本)",
mgm: "MGM (土耳其)",
fmi: "FMI (芬兰)",
kma: "KMA (韩国)",
hko: "HKO (香港)",
singapore_mss: "Singapore MSS",
cwa: "CWA (台湾)",
amos: "AMOS (韩国跑道)",
amsc_awos: "AMSC AWOS (中国)",
noaa_wrh: "NOAA WRH (美国结算)",
};
function StatusIcon({ svc }: { svc: ServiceResult }) {
if (svc.ok) return ;
if (svc.error && svc.error.includes("not configured")) return ;
return ;
}
function StatusText({ svc }: { svc: ServiceResult }) {
if (svc.ok) return {svc.latency_ms}ms;
if (svc.error && svc.error.includes("not configured")) return 未配置;
return {svc.error ?? "连接失败"};
}
export function HealthPageClient() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
const load = async () => {
setLoading(true);
setError("");
try {
const res = await fetch("/api/ops/health-check", { cache: "no-store" });
if (!res.ok) { setError(`HTTP ${res.status}`); setLoading(false); return; }
setData(await res.json());
} catch (e) { setError(String(e).slice(0, 200)); }
setLoading(false);
};
useEffect(() => { void load(); }, []);
if (loading) return 检测中...
;
if (error) return 加载失败: {error}
;
if (!data) return 无数据
;
const services = Object.entries(data.services);
const okCount = services.filter(([, v]) => v.ok).length;
const latencyData = services
.filter(([, svc]) => svc.ok && svc.latency_ms != null)
.map(([key, svc]) => ({
name: LABELS[key] || key,
latency: svc.latency_ms ?? 0,
}))
.sort((a, b) => a.latency - b.latency);
return (
API 状态{" "}
{data.ok ? "全部正常" : `${okCount}/${services.length} 正常`}
{data.checked_at?.slice(11, 19) ?? ""}
{latencyData.length > 0 && (
服务响应延迟对比 (ms)
)}
{services.map(([key, svc]) => (
{LABELS[key] ?? key}
{svc.status ? HTTP {svc.status} : null}
{(svc.credential_configured != null || svc.points != null || svc.observation_time_local) && (
{svc.credential_configured != null && (
{svc.credential_configured ? "凭证已配置" : "凭证未配置"}
)}
{svc.points != null && 跑道点 {svc.points}}
{svc.observation_time_local && {svc.observation_time_local}}
)}
))}
);
}