import { useState, useEffect } from 'react'; import { Row, Col, Card, Descriptions, Spin, message, Typography } from 'antd'; import ReactEChartsCore from 'echarts-for-react/lib/core'; import { echarts } from '@/utils/echarts'; import { macarons } from '../../themes/macarons'; import { getCache } from '../../api/monitor/cache'; import type { CacheMonitorResponse } from '@/types/api'; import './cache-monitor.css'; const { Text } = Typography; echarts.registerTheme('macarons', macarons); const defaultCacheData: CacheMonitorResponse = { info: {}, dbSize: 0, commandStats: [], }; const toDisplayText = (value: string | number | undefined): string => { if (value === undefined || value === null) { return ''; } return String(value); }; const CacheMonitorPage = () => { const [loading, setLoading] = useState(true); const [cacheData, setCacheData] = useState(defaultCacheData); const [error, setError] = useState(null); useEffect(() => { getCache() .then((response) => { setCacheData(response); }) .catch(() => { const errorMsg = '加载缓存数据失败,请确认后端服务是否正常。'; setError(errorMsg); message.error(errorMsg); }) .finally(() => { setLoading(false); }); }, []); if (loading) { return ; } if (error) { return
{error}
; } const commandStatsOptions = { tooltip: { trigger: 'item', formatter: '{a}
{b} : {c} ({d}%)', }, series: [ { name: '命令', type: 'pie', roseType: 'radius', radius: [15, 95], center: ['50%', '38%'], data: cacheData.commandStats || [], animationEasing: 'cubicInOut', animationDuration: 1000, }, ], }; const usedMemoryOptions = { tooltip: { formatter: `{b}
{a} : ${toDisplayText(cacheData.info?.used_memory_human)}`, }, series: [ { name: '峰值', type: 'gauge', min: 0, max: 1000, detail: { formatter: toDisplayText(cacheData.info?.used_memory_human), }, data: [ { value: Number.parseFloat(toDisplayText(cacheData.info?.used_memory_human)) || 0, name: '内存消耗', }, ], }, ], }; return (
{toDisplayText(cacheData.info?.redis_version)} {cacheData.info?.redis_mode === 'standalone' ? '单机' : toDisplayText(cacheData.info?.redis_mode)} {toDisplayText(cacheData.info?.tcp_port)} {toDisplayText(cacheData.info?.connected_clients)} {toDisplayText(cacheData.info?.uptime_in_days)} {toDisplayText(cacheData.info?.used_memory_human)} {cacheData.info?.used_cpu_user_children ? Number.parseFloat(toDisplayText(cacheData.info.used_cpu_user_children)).toFixed(2) : ''} {toDisplayText(cacheData.info?.maxmemory_human)} {cacheData.info?.aof_enabled === '0' ? '否' : (cacheData.info?.aof_enabled ? '是' : '')} {toDisplayText(cacheData.info?.rdb_last_bgsave_status)} {cacheData.dbSize || ''} {cacheData.info?.instantaneous_input_kbps ? `${cacheData.info.instantaneous_input_kbps}kps/${cacheData.info.instantaneous_output_kbps}kps` : ''}
); }; export default CacheMonitorPage;