Files
iot-device-management-frontend/apps/web-antd/src/views/infra/redis/modules/memory.vue

136 lines
2.8 KiB
Vue
Raw Normal View History

2025-04-07 22:00:14 +08:00
<script lang="ts" setup>
import type { EchartsUIType } from '@vben/plugins/echarts';
2025-04-22 22:10:33 +08:00
import type { InfraRedisApi } from '#/api/infra/redis';
2025-04-07 22:00:14 +08:00
import { onMounted, ref, watch } from 'vue';
2025-04-22 22:10:33 +08:00
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
2025-04-07 22:00:14 +08:00
const props = defineProps<{
2025-04-22 22:10:33 +08:00
redisData?: InfraRedisApi.RedisMonitorInfo;
2025-04-07 22:00:14 +08:00
}>();
const chartRef = ref<EchartsUIType>();
const { renderEcharts } = useEcharts(chartRef);
/** 解析内存值,移除单位,转为数字 */
2025-05-20 16:45:38 +08:00
function parseMemoryValue(memStr: string | undefined): number {
2025-04-07 22:00:14 +08:00
if (!memStr) {
return 0;
}
try {
// 从字符串中提取数字部分,例如 "1.2M" 中的 1.2
const str = String(memStr); // 显式转换为字符串类型
const match = str.match(/^([\d.]+)/);
2025-04-22 22:10:33 +08:00
return match ? Number.parseFloat(match[1] as string) : 0;
} catch {
2025-04-07 22:00:14 +08:00
return 0;
}
2025-05-20 16:45:38 +08:00
}
2025-04-07 22:00:14 +08:00
/** 渲染内存使用图表 */
2025-05-20 16:45:38 +08:00
function renderMemoryChart() {
2025-04-07 22:00:14 +08:00
if (!props.redisData?.info) {
return;
}
// 处理数据
const usedMemory = props.redisData.info.used_memory_human || '0';
const memoryValue = parseMemoryValue(usedMemory);
// 渲染图表
renderEcharts({
title: {
text: '内存使用情况',
left: 'center',
},
tooltip: {
2025-04-22 22:10:33 +08:00
formatter: `{b} <br/>{a} : ${usedMemory}`,
2025-04-07 22:00:14 +08:00
},
series: [
{
name: '峰值',
type: 'gauge',
min: 0,
max: 100,
splitNumber: 10,
color: '#F5C74E',
radius: '85%',
center: ['50%', '50%'],
startAngle: 225,
endAngle: -45,
axisLine: {
lineStyle: {
color: [
[0.2, '#7FFF00'],
[0.8, '#00FFFF'],
2025-04-22 22:10:33 +08:00
[1, '#FF0000'],
2025-04-07 22:00:14 +08:00
],
2025-04-22 22:10:33 +08:00
width: 10,
},
2025-04-07 22:00:14 +08:00
},
axisTick: {
length: 5,
lineStyle: {
2025-04-22 22:10:33 +08:00
color: '#76D9D7',
},
2025-04-07 22:00:14 +08:00
},
splitLine: {
length: 20,
lineStyle: {
2025-04-22 22:10:33 +08:00
color: '#76D9D7',
},
2025-04-07 22:00:14 +08:00
},
axisLabel: {
color: '#76D9D7',
distance: 15,
2025-04-22 22:10:33 +08:00
fontSize: 15,
2025-04-07 22:00:14 +08:00
},
pointer: {
width: 7,
2025-04-22 22:10:33 +08:00
show: true,
2025-04-07 22:00:14 +08:00
},
detail: {
show: true,
offsetCenter: [0, '50%'],
2025-05-20 16:45:38 +08:00
color: 'inherit',
2025-04-07 22:00:14 +08:00
fontSize: 30,
2025-04-22 22:10:33 +08:00
formatter: usedMemory,
2025-04-07 22:00:14 +08:00
},
progress: {
2025-04-22 22:10:33 +08:00
show: true,
2025-04-07 22:00:14 +08:00
},
data: [
{
value: memoryValue,
2025-04-22 22:10:33 +08:00
name: '内存消耗',
},
],
},
],
2025-04-07 22:00:14 +08:00
});
2025-05-20 16:45:38 +08:00
}
2025-04-07 22:00:14 +08:00
/** 监听数据变化,重新渲染图表 */
2025-04-22 22:10:33 +08:00
watch(
() => props.redisData,
(newVal) => {
if (newVal) {
renderMemoryChart();
}
},
{ deep: true },
);
2025-04-07 22:00:14 +08:00
onMounted(() => {
if (props.redisData) {
renderMemoryChart();
}
});
</script>
<template>
2025-05-20 16:45:38 +08:00
<EchartsUI ref="chartRef" height="420px" />
2025-04-07 22:00:14 +08:00
</template>