/** * AI 运行总览 Dashboard 页面。 * * - 顶部:门店筛选 + 刷新 * - 第一行:4 个统计卡片(今日调用、成功率、Token 消耗、平均延迟) * - 第二行:7 天趋势表格 + App 调用占比表格 * - 第三行:Token 预算进度条 + App 健康状态 * - 第四行:告警列表 */ import React, { useEffect, useRef, useState, useCallback } from "react"; import { Card, Row, Col, Statistic, Table, Tag, Badge, Progress, Select, Button, message, Typography, Space, DatePicker, } from "antd"; import { ReloadOutlined, WifiOutlined } from "@ant-design/icons"; import type { Dayjs } from "dayjs"; const { RangePicker } = DatePicker; const RANGE_OPTIONS = [ { label: "今日", value: 1 }, { label: "近 3 天", value: 3 }, { label: "近 7 天", value: 7 }, { label: "近 10 天", value: 10 }, { label: "指定日期", value: 0 }, // 0 = 启用 RangePicker ]; const RANGE_LABEL: Record = { 1: "今日", 3: "近 3 天", 7: "近 7 天", 10: "近 10 天", }; import type { ColumnsType } from "antd/es/table"; import { getDashboard, type DashboardResponse, type DailyTrend, type AppDistItem, type AlertItem, type AppHealthItem, } from "../api/adminAI"; const { Title } = Typography; const ALERT_STATUS_COLOR: Record = { failed: "red", timeout: "orange", circuit_open: "volcano", }; const ALERT_MGMT_COLOR: Record = { pending: "warning", acknowledged: "success", ignored: "default", }; const HEALTH_STATUS: Record = { success: "success", failed: "error", timeout: "warning", circuit_open: "error", }; function fmtTime(raw: string | null): string { if (!raw) return "—"; const d = new Date(raw); return Number.isNaN(d.getTime()) ? raw : d.toLocaleString("zh-CN"); } // ---- 表格列定义 ---- const trendColumns: ColumnsType = [ { title: "日期", dataIndex: "date", key: "date", width: 120 }, { title: "调用量", dataIndex: "calls", key: "calls", align: "right" }, { title: "成功率", dataIndex: "success_rate", key: "success_rate", align: "right", render: (v: number) => `${(v * 100).toFixed(1)}%`, }, ]; const distColumns: ColumnsType = [ { title: "App 类型", dataIndex: "app_type", key: "app_type" }, { title: "调用次数", dataIndex: "count", key: "count", align: "right" }, { title: "占比", dataIndex: "percentage", key: "percentage", align: "right", render: (v: number) => `${(v * 100).toFixed(1)}%`, }, ]; const alertColumns: ColumnsType = [ { title: "ID", dataIndex: "id", key: "id", width: 80 }, { title: "App", dataIndex: "app_type", key: "app_type", width: 160 }, { title: "状态", dataIndex: "status", key: "status", width: 110, render: (v: string) => {v}, }, { title: "告警状态", dataIndex: "alert_status", key: "alert_status", width: 110, render: (v: string | null) => v ? {v} : "—", }, { title: "错误信息", dataIndex: "error_message", key: "error_message", ellipsis: true, render: (v: string | null) => v ?? "—", }, { title: "时间", dataIndex: "created_at", key: "created_at", width: 170, render: fmtTime }, ]; // ---- 页面组件 ---- const AIDashboard: React.FC = () => { const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [siteId, setSiteId] = useState(undefined); const [rangeDays, setRangeDays] = useState(1); // 0=自定义日期 / 1/3/7/10 const [customRange, setCustomRange] = useState<[Dayjs, Dayjs] | null>(null); const [wsStatus, setWsStatus] = useState<"connecting" | "connected" | "disconnected">("disconnected"); const [realtimeAlerts, setRealtimeAlerts] = useState([]); const wsRef = useRef(null); const load = useCallback(async () => { setLoading(true); try { const query: { site_id?: number; range_days?: number; date_from?: string; date_to?: string } = {}; if (siteId != null) query.site_id = siteId; if (rangeDays === 0 && customRange) { query.date_from = customRange[0].format("YYYY-MM-DD"); query.date_to = customRange[1].format("YYYY-MM-DD"); } else if (rangeDays > 0) { query.range_days = rangeDays; } const res = await getDashboard(query); setData(res); } catch { message.error("加载 Dashboard 失败"); } finally { setLoading(false); } }, [siteId, rangeDays, customRange]); useEffect(() => { load(); }, [load]); const statLabel = rangeDays === 0 ? (customRange ? `${customRange[0].format("MM-DD")} ~ ${customRange[1].format("MM-DD")}` : "指定日期") : (RANGE_LABEL[rangeDays] || "今日"); // WebSocket 实时告警订阅 useEffect(() => { const wsKey = siteId ?? -1; const proto = window.location.protocol === "https:" ? "wss" : "ws"; const url = `${proto}://${window.location.host}/ws/ai-alerts/${wsKey}`; setWsStatus("connecting"); const ws = new WebSocket(url); wsRef.current = ws; ws.onopen = () => setWsStatus("connected"); ws.onclose = () => setWsStatus("disconnected"); ws.onerror = () => setWsStatus("disconnected"); ws.onmessage = (evt) => { try { const msg = JSON.parse(evt.data as string) as { type: string; payload: AlertItem; }; if (msg.type === "alert_created" && msg.payload) { setRealtimeAlerts((prev) => [msg.payload, ...prev].slice(0, 20)); message.warning(`[实时] ${msg.payload.app_type} ${msg.payload.status}`); } } catch { // 忽略非 JSON 消息 } }; return () => { ws.close(); wsRef.current = null; setWsStatus("disconnected"); }; }, [siteId]); return (
{/* 顶部:门店筛选 + 刷新 */} AI 运行总览 {rangeDays === 0 && ( setCustomRange(v as [Dayjs, Dayjs] | null)} /> )} 实时 {wsStatus === "connected" ? "已连接" : wsStatus === "connecting" ? "连接中" : "断开"}} /> {/* 第一行:4 个统计卡片 */} {/* 第二行:7 天趋势 + App 调用占比 */} columns={trendColumns} dataSource={data?.trend_7d ?? []} rowKey="date" size="small" pagination={false} loading={loading} /> columns={distColumns} dataSource={data?.app_distribution ?? []} rowKey="app_type" size="small" pagination={false} loading={loading} /> {/* 第三行:Token 预算 + App 健康状态 */}
日预算:{data?.budget.daily_used ?? 0} / {data?.budget.daily_limit ?? 0} 0.9 ? "exception" : "active"} />
月预算:{data?.budget.monthly_used ?? 0} / {data?.budget.monthly_limit ?? 0} 0.9 ? "exception" : "active"} />
{(data?.app_health ?? []).map((item: AppHealthItem) => (
{item.app_type} {fmtTime(item.last_call_at)}
))} {(data?.app_health ?? []).length === 0 && 暂无数据}
{/* 第四行:告警列表(实时 + 历史合并) */} 0 && ( {realtimeAlerts.length} 条实时 )} > columns={alertColumns} dataSource={[ ...realtimeAlerts, ...(data?.recent_alerts ?? []).filter( (a) => !realtimeAlerts.some((r) => r.id === a.id) ), ]} rowKey="id" size="small" pagination={{ pageSize: 10 }} loading={loading} />
); }; export default AIDashboard;