diff --git a/app/alerts/page.tsx b/app/alerts/page.tsx index 945dec6..c1d1aaa 100644 --- a/app/alerts/page.tsx +++ b/app/alerts/page.tsx @@ -37,7 +37,12 @@ export default function AlertsPage() { const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [saved, setSaved] = useState(false); + const [checking, setChecking] = useState(false); + const [checkResults, setCheckResults] = useState([]); + const [lastCheckTime, setLastCheckTime] = useState(""); + const [checkInterval, setCheckInterval] = useState(5); // 默认 5 分钟检查一次 + // 加载配置 useEffect(() => { Promise.all([ fetch("/api/alerts").then((r) => r.json()), @@ -51,6 +56,46 @@ export default function AlertsPage() { .finally(() => setLoading(false)); }, []); + // 定时检查告警 + useEffect(() => { + if (!config?.enabled) return; + + const checkAlerts = () => { + setChecking(true); + fetch("/api/alerts/check", { method: "POST" }) + .then((r) => r.json()) + .then((data) => { + if (data.results && data.results.length > 0) { + setCheckResults(data.results); + setLastCheckTime(new Date().toLocaleTimeString("zh-CN")); + } + }) + .catch(console.error) + .finally(() => setChecking(false)); + }; + + // 立即检查一次 + checkAlerts(); + + // 设置定时器 + const timer = setInterval(checkAlerts, checkInterval * 60 * 1000); + return () => clearInterval(timer); + }, [config?.enabled, checkInterval]); + + const handleManualCheck = () => { + setChecking(true); + fetch("/api/alerts/check", { method: "POST" }) + .then((r) => r.json()) + .then((data) => { + if (data.results && data.results.length > 0) { + setCheckResults(data.results); + setLastCheckTime(new Date().toLocaleTimeString("zh-CN")); + } + }) + .catch(console.error) + .finally(() => setChecking(false)); + }; + const handleToggle = () => { if (!config) return; setSaving(true); @@ -152,14 +197,63 @@ export default function AlertsPage() { {t("alerts.subtitle") || "Configure system alerts and notifications"}

- - {t("common.backHome") || "Back"} - +
+ {/* 检查间隔设置 */} + {config.enabled && ( +
+ 检查间隔: + +
+ )} + {/* 手动检查按钮 */} + {config.enabled && ( + + )} + + {t("common.backHome") || "Back"} + +
+ {/* 检查结果展示 */} + {config.enabled && checkResults.length > 0 && ( +
+
+

⚠️ 告警触发 ({checkResults.length})

+ {lastCheckTime && {lastCheckTime}} +
+
    + {checkResults.map((result, i) => ( +
  • • {result}
  • + ))} +
+
+ )} + + {config.enabled && checking && ( +
+ ⏳ 正在检查告警... +
+ )} + {/* 告警总开关 */}