Jiarenxiang | 38dcb05 | 2025-03-13 16:40:09 +0800 | [diff] [blame] | 1 | import React, { useEffect, useState } from 'react'; |
| 2 | import { Card, Col, Row, Table } from 'antd'; |
| 3 | import { DataItem } from '@antv/g2plot/esm/interface/config'; |
| 4 | import { Gauge, Pie } from '@ant-design/plots'; |
| 5 | import styles from './index.less'; |
| 6 | import { getCacheInfo } from '@/services/monitor/cache'; |
| 7 | |
| 8 | |
| 9 | /* * |
| 10 | * |
| 11 | * @author whiteshader@163.com |
| 12 | * @datetime 2021/09/16 |
| 13 | * |
| 14 | * */ |
| 15 | |
| 16 | |
| 17 | const columns = [ |
| 18 | { |
| 19 | title: 'col1', |
| 20 | dataIndex: 'col1', |
| 21 | key: 'col1', |
| 22 | }, |
| 23 | { |
| 24 | title: 'col2', |
| 25 | dataIndex: 'col2', |
| 26 | key: 'col2', |
| 27 | }, |
| 28 | { |
| 29 | title: 'col3', |
| 30 | dataIndex: 'col3', |
| 31 | key: 'col3', |
| 32 | }, |
| 33 | { |
| 34 | title: 'col4', |
| 35 | dataIndex: 'col4', |
| 36 | key: 'col4', |
| 37 | }, |
| 38 | { |
| 39 | title: 'col5', |
| 40 | dataIndex: 'col5', |
| 41 | key: 'col5', |
| 42 | }, |
| 43 | { |
| 44 | title: 'col6', |
| 45 | dataIndex: 'col6', |
| 46 | key: 'col6', |
| 47 | }, |
| 48 | { |
| 49 | title: 'col7', |
| 50 | dataIndex: 'col7', |
| 51 | key: 'col7', |
| 52 | }, |
| 53 | { |
| 54 | title: 'col8', |
| 55 | dataIndex: 'col8', |
| 56 | key: 'col8', |
| 57 | }, |
| 58 | ]; |
| 59 | |
| 60 | const usageFormatter = (val: string): string => { |
| 61 | switch (val) { |
| 62 | case '10': |
| 63 | return '100%'; |
| 64 | case '8': |
| 65 | return '80%'; |
| 66 | case '6': |
| 67 | return '60%'; |
| 68 | case '4': |
| 69 | return '40%'; |
| 70 | case '2': |
| 71 | return '20%'; |
| 72 | case '0': |
| 73 | return '0%'; |
| 74 | default: |
| 75 | return ''; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | const CacheInfo: React.FC = () => { |
| 80 | const [baseInfoData, setBaseInfoData] = useState<any>([]); |
| 81 | const [memUsage, setMemUsage] = useState<Number>(0); |
| 82 | const [memUsageTitle, setMemUsageTitle] = useState<any>([]); |
| 83 | const [cmdInfoData, setCmdInfoData] = useState<DataItem[]>([]); |
| 84 | useEffect(() => { |
| 85 | getCacheInfo().then((res) => { |
| 86 | if (res.code === 200) { |
| 87 | const baseinfo = []; |
| 88 | baseinfo.push({ |
| 89 | col1: 'Redis版本', |
| 90 | col2: res.data.info.redis_version, |
| 91 | col3: '运行模式', |
| 92 | col4: res.data.info.redis_mode === 'standalone' ? '单机' : '集群', |
| 93 | col5: '端口', |
| 94 | col6: res.data.info.tcp_port, |
| 95 | col7: '客户端数', |
| 96 | col8: res.data.info.connected_clients, |
| 97 | }); |
| 98 | baseinfo.push({ |
| 99 | col1: '运行时间(天)', |
| 100 | col2: res.data.info.uptime_in_days, |
| 101 | col3: '使用内存', |
| 102 | col4: res.data.info.used_memory_human, |
| 103 | col5: '使用CPU', |
| 104 | col6: `${res.data.info.used_cpu_user_children}%`, |
| 105 | col7: '内存配置', |
| 106 | col8: res.data.info.maxmemory_human, |
| 107 | }); |
| 108 | baseinfo.push({ |
| 109 | col1: 'AOF是否开启', |
| 110 | col2: res.data.info.aof_enabled === '0' ? '否' : '是', |
| 111 | col3: 'RDB是否成功', |
| 112 | col4: res.data.info.rdb_last_bgsave_status, |
| 113 | col5: 'Key数量', |
| 114 | col6: res.data.dbSize, |
| 115 | col7: '网络入口/出口', |
| 116 | col8: `${res.data.info.instantaneous_input_kbps}/${res.data.info.instantaneous_output_kbps}kps`, |
| 117 | }); |
| 118 | setBaseInfoData(baseinfo); |
| 119 | |
| 120 | const data: VisitDataType[] = res.data.commandStats.map((item) => { |
| 121 | return { |
| 122 | x: item.name, |
| 123 | y: Number(item.value), |
| 124 | }; |
| 125 | }); |
| 126 | |
| 127 | setCmdInfoData(data); |
| 128 | setMemUsageTitle(res.data.info.used_memory_human); |
| 129 | setMemUsage(res.data.info.used_memory / res.data.info.total_system_memory); |
| 130 | } |
| 131 | }); |
| 132 | }, []); |
| 133 | |
| 134 | return ( |
| 135 | <div> |
| 136 | <Row gutter={[24, 24]}> |
| 137 | <Col span={24}> |
| 138 | <Card title="基本信息" className={styles.card}> |
| 139 | <Table |
| 140 | rowKey="col1" |
| 141 | pagination={false} |
| 142 | showHeader={false} |
| 143 | dataSource={baseInfoData} |
| 144 | columns={columns} |
| 145 | /> |
| 146 | </Card> |
| 147 | </Col> |
| 148 | </Row> |
| 149 | <Row gutter={[24, 24]}> |
| 150 | <Col span={12}> |
| 151 | <Card title="命令统计" className={styles.card}> |
| 152 | <Pie |
| 153 | height={320} |
| 154 | radius={0.8} |
| 155 | innerRadius={0.5} |
| 156 | angleField="y" |
| 157 | colorField="x" |
| 158 | data={cmdInfoData as any} |
| 159 | legend={false} |
| 160 | label={{ |
| 161 | position: 'spider', |
| 162 | text: (item: { x: number; y: number }) => { |
| 163 | return `${item.x}: ${item.y}`; |
| 164 | }, |
| 165 | }} |
| 166 | /> |
| 167 | </Card> |
| 168 | </Col> |
| 169 | <Col span={12}> |
| 170 | <Card title="内存信息" className={styles.card}> |
| 171 | <Gauge |
| 172 | title={memUsageTitle} |
| 173 | height={320} |
| 174 | percent={memUsage} |
| 175 | formatter={usageFormatter} |
| 176 | padding={-16} |
| 177 | style={{ |
| 178 | textContent: () => { return memUsage.toFixed(2).toString() + '%'}, |
| 179 | }} |
| 180 | data={ |
| 181 | { |
| 182 | target: memUsage, |
| 183 | total: 100, |
| 184 | name: 'score', |
| 185 | thresholds: [20, 40, 60, 80, 100], |
| 186 | } as any |
| 187 | } |
| 188 | meta={{ |
| 189 | color: { |
| 190 | range: ['#C3F71F', '#B5E61D', '#FFC90E', '#FF7F27', '#FF2518'], |
| 191 | }, |
| 192 | }} |
| 193 | /> |
| 194 | </Card> |
| 195 | </Col> |
| 196 | </Row> |
| 197 | </div> |
| 198 | ); |
| 199 | }; |
| 200 | |
| 201 | export default CacheInfo; |