import { useCallback, useEffect, useMemo, useState } from 'react'; import type { ReactNode } from 'react'; import { useTranslation } from 'react-i18next'; import { Modal, Select, Tabs } from 'antd'; import { DashboardOutlined, DatabaseOutlined, DeploymentUnitOutlined, GlobalOutlined, HddOutlined, LineChartOutlined, TeamOutlined, } from '@ant-design/icons'; import { HttpUtil, SizeFormatter } from '@/utils'; import { Sparkline } from '@/components/viz'; import { useMediaQuery } from '@/hooks/useMediaQuery'; import type { Status } from '@/models/status'; import './SystemHistoryModal.css'; interface SystemHistoryModalProps { open: boolean; status: Status; onClose: () => void; } interface MetricDef { key: string; tab: string; tabKey?: string; title: string; icon: ReactNode; valueMax: number | null; unit: string; stroke: string; key2?: string; stroke2?: string; name1?: string; name2?: string; key3?: string; stroke3?: string; name3?: string; } const METRICS: MetricDef[] = [ { key: 'cpu', tab: 'CPU', title: 'pages.index.historyTitleCpu', icon: , valueMax: 100, unit: '%', stroke: '' }, { key: 'mem', tab: 'RAM', title: 'pages.index.historyTitleMem', icon: , valueMax: 100, unit: '%', stroke: '#7c4dff' }, { key: 'netUp', tab: 'Bandwidth', tabKey: 'pages.index.historyTabBandwidth', title: 'pages.index.historyTitleNetwork', icon: , valueMax: null, unit: 'B/s', stroke: '#1890ff', key2: 'netDown', stroke2: '#13c2c2', name1: 'Up', name2: 'Down' }, { key: 'pktUp', tab: 'Packets', tabKey: 'pages.index.historyTabPackets', title: 'pages.index.historyTitlePackets', icon: , valueMax: null, unit: 'pkt/s', stroke: '#2f54eb', key2: 'pktDown', stroke2: '#36cfc9', name1: 'Up', name2: 'Down' }, { key: 'diskRead', tab: 'Disk I/O', tabKey: 'pages.index.historyTabDisk', title: 'pages.index.historyTitleDisk', icon: , valueMax: null, unit: 'B/s', stroke: '#eb2f96', key2: 'diskWrite', stroke2: '#722ed1', name1: 'Read', name2: 'Write' }, { key: 'online', tab: 'Online', tabKey: 'pages.index.historyTabOnline', title: 'pages.index.historyTitleOnline', icon: , valueMax: null, unit: '', stroke: '#52c41a' }, { key: 'load1', tab: 'Load', tabKey: 'pages.index.historyTabLoad', title: 'pages.index.historyTitleLoad', icon: , valueMax: null, unit: '', stroke: '#fa8c16', key2: 'load5', stroke2: '#f5222d', name1: '1m', name2: '5m', key3: 'load15', stroke3: '#a0d911', name3: '15m' }, ]; function unitFormatter(unit: string, activeKey: string): (v: number) => string { if (unit === 'B/s') { return (v) => `${SizeFormatter.sizeFormat(Math.max(0, Number(v) || 0)).replace(/\.\d+/, '')}/s`; } if (unit === 'pkt/s') { return (v) => `${Math.round(Math.max(0, Number(v) || 0)).toLocaleString()}/s`; } if (unit === '%') { return (v) => `${Number(v).toFixed(1)}%`; } return (v) => { const n = Number(v) || 0; if (activeKey === 'online') return String(Math.round(n)); return n.toFixed(2); }; } function formatFullTimestamp(unixSec: number): string { const d = new Date(unixSec * 1000); const today = new Date(); const sameDay = d.getFullYear() === today.getFullYear() && d.getMonth() === today.getMonth() && d.getDate() === today.getDate(); const hh = String(d.getHours()).padStart(2, '0'); const mm = String(d.getMinutes()).padStart(2, '0'); const ss = String(d.getSeconds()).padStart(2, '0'); const time = `${hh}:${mm}:${ss}`; if (sameDay) return time; const MM = String(d.getMonth() + 1).padStart(2, '0'); const DD = String(d.getDate()).padStart(2, '0'); return `${MM}-${DD} ${time}`; } export default function SystemHistoryModal({ open, status, onClose }: SystemHistoryModalProps) { const { t } = useTranslation(); const { isMobile } = useMediaQuery(); const [activeKey, setActiveKey] = useState('cpu'); const [bucket, setBucket] = useState(2); const [points, setPoints] = useState([]); const [points2, setPoints2] = useState([]); const [points3, setPoints3] = useState([]); const [labels, setLabels] = useState([]); const [timestamps, setTimestamps] = useState([]); const activeMetric = useMemo(() => METRICS.find((m) => m.key === activeKey), [activeKey]); const strokeColor = activeMetric?.stroke || status?.cpu?.color || '#008771'; const yFormatter = useMemo( () => unitFormatter(activeMetric?.unit ?? '', activeKey), [activeMetric, activeKey], ); const tsLookup = useMemo(() => { const m = new Map(); for (let i = 0; i < labels.length; i++) { m.set(labels[i], timestamps[i]); } return m; }, [labels, timestamps]); const tooltipLabelFormatter = useCallback( (label: string) => { const ts = tsLookup.get(label); return ts ? formatFullTimestamp(ts) : label; }, [tsLookup], ); const fetchBucket = useCallback(async () => { if (!activeMetric) return; try { const url = `/panel/api/server/history/${activeMetric.key}/${bucket}`; const msg = await HttpUtil.get(url); if (msg?.success && Array.isArray(msg.obj)) { const vals: number[] = []; const labs: string[] = []; const tss: number[] = []; for (const p of msg.obj) { const d = new Date(p.t * 1000); const hh = String(d.getHours()).padStart(2, '0'); const mm = String(d.getMinutes()).padStart(2, '0'); const ss = String(d.getSeconds()).padStart(2, '0'); labs.push(bucket >= 60 ? `${hh}:${mm}` : `${hh}:${mm}:${ss}`); vals.push(Number(p.v) || 0); tss.push(Number(p.t) || 0); } setLabels(labs); setPoints(vals); setTimestamps(tss); const fetchAligned = async (key?: string): Promise => { if (!key) return []; const m = await HttpUtil.get(`/panel/api/server/history/${key}/${bucket}`); if (m?.success && Array.isArray(m.obj)) { const byTs = new Map(); for (const p of m.obj) byTs.set(Number(p.t) || 0, Number(p.v) || 0); return tss.map((ts) => byTs.get(ts) ?? 0); } return []; }; setPoints2(await fetchAligned(activeMetric.key2)); setPoints3(await fetchAligned(activeMetric.key3)); } else { setLabels([]); setPoints([]); setPoints2([]); setPoints3([]); setTimestamps([]); } } catch (e) { console.error('Failed to fetch history bucket', e); setLabels([]); setPoints([]); setPoints2([]); setPoints3([]); setTimestamps([]); } }, [activeMetric, bucket]); useEffect(() => { if (open) setActiveKey('cpu'); }, [open]); useEffect(() => { if (open) fetchBucket(); }, [open, activeKey, bucket, fetchBucket]); return ( {t('pages.index.systemHistoryTitle')}