Skip to content

Commit ab7032f

Browse files
YunaiVgitee-org
authored andcommitted
!148 【重构】Vue3 管理后台:[基础管理 -> Redis监控] 使用Echart组件实现
Merge pull request !148 from Chika/dev
2 parents c47cc18 + fbac522 commit ab7032f

File tree

2 files changed

+209
-92
lines changed

2 files changed

+209
-92
lines changed

src/router/modules/remaining.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ const remainingRouter: AppRouteRecordRaw[] = [
330330
}
331331
]
332332
},
333+
{
334+
path: '/infra',
335+
component: Layout,
336+
name: 'InfraRedis',
337+
meta: {
338+
hidden: true
339+
},
340+
children: [
341+
{
342+
path: '/infra/redis',
343+
component: () => import('@/views/infra/redis/index.vue'),
344+
name: 'InfraRedis',
345+
meta: {
346+
noCache: true,
347+
hidden: true,
348+
canTo: true,
349+
title: 'REDIS测试测试测试',
350+
activeMenu: 'infra/redis/index'
351+
}
352+
}
353+
]
354+
},
333355
{
334356
path: '/property',
335357
component: Layout,

src/views/infra/redis/index.vue

Lines changed: 187 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<template>
22
<doc-alert title="Redis 缓存" url="https://doc.iocoder.cn/redis-cache/" />
33
<doc-alert title="本地缓存" url="https://doc.iocoder.cn/local-cache/" />
4-
54
<el-scrollbar height="calc(100vh - 88px - 40px - 50px)">
65
<el-row>
76
<!-- 基本信息 -->
@@ -51,127 +50,223 @@
5150
<!-- 命令统计 -->
5251
<el-col :span="12" class="mt-3">
5352
<el-card :gutter="12" shadow="hover">
54-
<div ref="commandStatsRef" class="h-88"></div>
53+
<Echart :options="commandStatsRefChika" :height="420" />
5554
</el-card>
5655
</el-col>
5756
<!-- 内存使用量统计 -->
5857
<el-col :span="12" class="mt-3">
5958
<el-card class="ml-3" :gutter="12" shadow="hover">
60-
<div ref="usedmemory" class="h-88"></div>
59+
<Echart :options="usedmemoryEchartChika" :height="420" />
6160
</el-card>
6261
</el-col>
6362
</el-row>
6463
</el-scrollbar>
6564
</template>
66-
<script setup lang="ts" name="InfraRedis">
67-
import * as echarts from 'echarts'
65+
<script setup lang="ts">
66+
import echarts from '@/plugins/echarts'
67+
import { GaugeChart } from 'echarts/charts'
68+
import { ToolboxComponent } from 'echarts/components'
6869
import * as RedisApi from '@/api/infra/redis'
6970
import { RedisMonitorInfoVO } from '@/api/infra/redis/types'
70-
71+
echarts.use([ToolboxComponent])
72+
echarts.use([GaugeChart])
7173
const cache = ref<RedisMonitorInfoVO>()
7274
7375
// 基本信息
7476
const readRedisInfo = async () => {
7577
const data = await RedisApi.getCache()
7678
cache.value = data
77-
loadEchartOptions(data.commandStats)
7879
}
79-
// 图表
80-
const commandStatsRef = ref<HTMLElement>()
81-
const usedmemory = ref<HTMLDivElement>()
82-
83-
const loadEchartOptions = (stats) => {
84-
const commandStats = [] as any[]
85-
const nameList = [] as string[]
86-
stats.forEach((row) => {
87-
commandStats.push({
88-
name: row.command,
89-
value: row.calls
90-
})
91-
nameList.push(row.command)
92-
})
93-
94-
const commandStatsInstance = echarts.init(commandStatsRef.value!, 'macarons')
9580
96-
commandStatsInstance.setOption({
97-
title: {
98-
text: '命令统计',
99-
left: 'center'
100-
},
101-
tooltip: {
102-
trigger: 'item',
103-
formatter: '{a} <br/>{b} : {c} ({d}%)'
104-
},
105-
legend: {
106-
type: 'scroll',
107-
orient: 'vertical',
108-
right: 30,
109-
top: 10,
110-
bottom: 20,
111-
data: nameList,
112-
textStyle: {
113-
color: '#a1a1a1'
81+
// 内存使用情况
82+
const usedmemoryEchartChika = reactive({
83+
title: {
84+
// 仪表盘标题。
85+
text: '内存使用情况',
86+
left: 'center',
87+
show: true, // 是否显示标题,默认 true。
88+
offsetCenter: [0, '20%'], //相对于仪表盘中心的偏移位置,数组第一项是水平方向的偏移,第二项是垂直方向的偏移。可以是绝对的数值,也可以是相对于仪表盘半径的百分比。
89+
color: 'yellow', // 文字的颜色,默认 #333。
90+
fontSize: 20 // 文字的字体大小,默认 15。
91+
},
92+
toolbox: {
93+
show: false,
94+
feature: {
95+
restore: { show: true },
96+
saveAsImage: { show: true }
97+
}
98+
},
99+
series: [
100+
{
101+
name: '峰值',
102+
type: 'gauge',
103+
min: 0,
104+
max: 50,
105+
splitNumber: 10,
106+
//这是指针的颜色
107+
color: '#F5C74E',
108+
radius: '85%',
109+
center: ['50%', '50%'],
110+
startAngle: 225,
111+
endAngle: -45,
112+
axisLine: {
113+
// 坐标轴线
114+
lineStyle: {
115+
// 属性lineStyle控制线条样式
116+
color: [
117+
[0.2, '#7FFF00'],
118+
[0.8, '#00FFFF'],
119+
[1, '#FF0000']
120+
],
121+
//width: 6 外框的大小(环的宽度)
122+
width: 10
123+
}
124+
},
125+
axisTick: {
126+
// 坐标轴小标记
127+
//里面的线长是5(短线)
128+
length: 5, // 属性length控制线长
129+
lineStyle: {
130+
// 属性lineStyle控制线条样式
131+
color: '#76D9D7'
132+
}
133+
},
134+
splitLine: {
135+
// 分隔线
136+
length: 20, // 属性length控制线长
137+
lineStyle: {
138+
// 属性lineStyle(详见lineStyle)控制线条样式
139+
color: '#76D9D7'
140+
}
141+
},
142+
axisLabel: {
143+
color: '#76D9D7',
144+
distance: 15,
145+
fontSize: 15
146+
},
147+
pointer: {
148+
//指针的大小
149+
width: 7,
150+
show: true
151+
},
152+
detail: {
153+
textStyle: {
154+
fontWeight: 'normal',
155+
//里面文字下的数值大小(50)
156+
fontSize: 15,
157+
color: '#FFFFFF'
158+
},
159+
valueAnimation: true
160+
},
161+
progress: {
162+
show: true
114163
}
115-
},
116-
series: [
117-
{
118-
name: '命令',
119-
type: 'pie',
120-
radius: [20, 120],
121-
center: ['40%', '60%'],
122-
data: commandStats,
123-
roseType: 'radius',
164+
}
165+
]
166+
})
167+
168+
// 指令使用情况
169+
const commandStatsRefChika = reactive({
170+
title: {
171+
text: '命令统计',
172+
left: 'center'
173+
},
174+
tooltip: {
175+
trigger: 'item',
176+
formatter: '{a} <br/>{b} : {c} ({d}%)'
177+
},
178+
legend: {
179+
type: 'scroll',
180+
orient: 'vertical',
181+
right: 30,
182+
top: 10,
183+
bottom: 20,
184+
data: [] as any[],
185+
textStyle: {
186+
color: '#a1a1a1'
187+
}
188+
},
189+
series: [
190+
{
191+
name: '命令',
192+
type: 'pie',
193+
radius: [20, 120],
194+
center: ['40%', '60%'],
195+
data: [] as any[],
196+
roseType: 'radius',
197+
label: {
198+
show: true
199+
},
200+
emphasis: {
124201
label: {
125202
show: true
126203
},
127-
emphasis: {
128-
label: {
129-
show: true
130-
},
131-
itemStyle: {
132-
shadowBlur: 10,
133-
shadowOffsetX: 0,
134-
shadowColor: 'rgba(0, 0, 0, 0.5)'
135-
}
204+
itemStyle: {
205+
shadowBlur: 10,
206+
shadowOffsetX: 0,
207+
shadowColor: 'rgba(0, 0, 0, 0.5)'
136208
}
137209
}
138-
]
139-
})
210+
}
211+
]
212+
})
213+
214+
/** 加载数据 */
215+
const getSummary = () => {
216+
//初始化命令图表
217+
initcommandStatsChart()
218+
usedMemoryInstance()
219+
}
220+
221+
/** 命令使用情况 */
222+
const initcommandStatsChart = async () => {
223+
usedmemoryEchartChika.series[0].data = []
224+
// 发起请求
225+
try {
226+
const data = await RedisApi.getCache()
227+
cache.value = data
228+
// 处理数据
229+
const commandStats = [] as any[]
230+
const nameList = [] as string[]
231+
data.commandStats.forEach((row) => {
232+
commandStats.push({
233+
name: row.command,
234+
value: row.calls
235+
})
236+
nameList.push(row.command)
237+
})
238+
commandStatsRefChika.legend.data = nameList
239+
commandStatsRefChika.series[0].data = commandStats
240+
} catch {}
241+
}
242+
const usedMemoryInstance = async () => {
243+
try {
244+
const data = await RedisApi.getCache()
245+
cache.value = data
246+
// 仪表盘详情,用于显示数据。
247+
usedmemoryEchartChika.series[0].detail = {
248+
show: true, // 是否显示详情,默认 true。
249+
offsetCenter: [0, '50%'], // 相对于仪表盘中心的偏移位置,数组第一项是水平方向的偏移,第二项是垂直方向的偏移。可以是绝对的数值,也可以是相对于仪表盘半径的百分比。
250+
color: 'auto', // 文字的颜色,默认 auto。
251+
fontSize: 30, // 文字的字体大小,默认 15。
252+
formatter: cache.value!.info.used_memory_human // 格式化函数或者字符串
253+
}
140254
141-
const usedMemoryInstance = echarts.init(usedmemory.value!, 'macarons')
142-
usedMemoryInstance.setOption({
143-
title: {
144-
text: '内存使用情况',
145-
left: 'center'
146-
},
147-
tooltip: {
255+
usedmemoryEchartChika.series[0].data[0] = {
256+
value: cache.value!.info.used_memory_human,
257+
name: '内存消耗'
258+
}
259+
console.log(cache.value!.info)
260+
usedmemoryEchartChika.tooltip = {
148261
formatter: '{b} <br/>{a} : ' + cache.value!.info.used_memory_human
149-
},
150-
series: [
151-
{
152-
name: '峰值',
153-
type: 'gauge',
154-
min: 0,
155-
max: 100,
156-
progress: {
157-
show: true
158-
},
159-
detail: {
160-
formatter: cache.value!.info.used_memory_human
161-
},
162-
data: [
163-
{
164-
value: parseFloat(cache.value!.info.used_memory_human),
165-
name: '内存消耗'
166-
}
167-
]
168-
}
169-
]
170-
})
262+
}
263+
} catch {}
171264
}
172265
173-
onBeforeMount(() => {
174-
// TODO @hiiwbs 微信,优化使用 Echart 组件
266+
/** 初始化 **/
267+
onMounted(() => {
175268
readRedisInfo()
269+
// 加载数据
270+
getSummary()
176271
})
177272
</script>

0 commit comments

Comments
 (0)