Skip to content

Commit 747a0c4

Browse files
feat: async load data of top CPU and memory processes data (#11081)
* feat: async load data of top CPU and memory processes data * refactor: Update CPU and memory top toggle functionality and state management --------- Co-authored-by: 王贺 <[email protected]>
1 parent 8e03b24 commit 747a0c4

File tree

7 files changed

+181
-24
lines changed

7 files changed

+181
-24
lines changed

agent/app/api/v2/dashboard.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ func (b *BaseApi) LoadDashboardCurrentInfo(c *gin.Context) {
179179
helper.SuccessWithData(c, data)
180180
}
181181

182+
// @Tags Dashboard
183+
// @Summary Load top cpu processes
184+
// @Success 200 {Array} dto.Process
185+
// @Security ApiKeyAuth
186+
// @Security Timestamp
187+
// @Router /dashboard/current/top/cpu [get]
188+
func (b *BaseApi) LoadDashboardTopCPU(c *gin.Context) {
189+
data := dashboardService.LoadTopCPU()
190+
helper.SuccessWithData(c, data)
191+
}
192+
193+
// @Tags Dashboard
194+
// @Summary Load top memory processes
195+
// @Success 200 {Array} dto.Process
196+
// @Security ApiKeyAuth
197+
// @Security Timestamp
198+
// @Router /dashboard/current/top/mem [get]
199+
func (b *BaseApi) LoadDashboardTopMem(c *gin.Context) {
200+
data := dashboardService.LoadTopMem()
201+
helper.SuccessWithData(c, data)
202+
}
203+
182204
// @Tags Dashboard
183205
// @Summary System restart
184206
// @Accept json

agent/app/service/dashboard.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type IDashboardService interface {
3939
LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error)
4040
LoadCurrentInfoForNode() *dto.NodeCurrent
4141
LoadCurrentInfo(ioOption string, netOption string) *dto.DashboardCurrent
42+
LoadTopCPU() []dto.Process
43+
LoadTopMem() []dto.Process
4244

4345
LoadQuickOptions() []dto.QuickJump
4446
ChangeQuick(req dto.ChangeQuicks) error
@@ -217,9 +219,6 @@ func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *d
217219
currentInfo.GPUData = loadGPUInfo()
218220
currentInfo.XPUData = loadXpuInfo()
219221

220-
currentInfo.TopCPUItems = loadTopCPU()
221-
currentInfo.TopMemItems = loadTopMem()
222-
223222
if ioOption == "all" {
224223
diskInfo, _ := disk.IOCounters()
225224
for _, state := range diskInfo {
@@ -261,6 +260,14 @@ func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *d
261260
return &currentInfo
262261
}
263262

263+
func (u *DashboardService) LoadTopCPU() []dto.Process {
264+
return loadTopCPU()
265+
}
266+
267+
func (u *DashboardService) LoadTopMem() []dto.Process {
268+
return loadTopMem()
269+
}
270+
264271
func (u *DashboardService) LoadAppLauncher(ctx *gin.Context) ([]dto.AppLauncher, error) {
265272
var data []dto.AppLauncher
266273
appInstalls, err := appInstallRepo.ListBy(context.Background())

agent/router/ro_dashboard.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func (s *DashboardRouter) InitRouter(Router *gin.RouterGroup) {
2020
cmdRouter.GET("/base/:ioOption/:netOption", baseApi.LoadDashboardBaseInfo)
2121
cmdRouter.GET("/current/node", baseApi.LoadCurrentInfoForNode)
2222
cmdRouter.GET("/current/:ioOption/:netOption", baseApi.LoadDashboardCurrentInfo)
23+
cmdRouter.GET("/current/top/cpu", baseApi.LoadDashboardTopCPU)
24+
cmdRouter.GET("/current/top/mem", baseApi.LoadDashboardTopMem)
2325
cmdRouter.POST("/system/restart/:operation", baseApi.SystemRestart)
2426
}
2527
}

frontend/src/api/interface/dashboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export namespace Dashboard {
103103
gpuData: Array<GPUInfo>;
104104
xpuData: Array<XPUInfo>;
105105

106-
topCPUItems: Array<Process>;
107-
topMemItems: Array<Process>;
106+
topCPUItems?: Array<Process>;
107+
topMemItems?: Array<Process>;
108108

109109
netBytesSent: number;
110110
netBytesRecv: number;

frontend/src/api/modules/dashboard.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export const loadCurrentInfo = (ioOption: string, netOption: string) => {
2929
return http.get<Dashboard.CurrentInfo>(`/dashboard/current/${ioOption}/${netOption}`);
3030
};
3131

32+
export const loadTopCPU = () => {
33+
return http.get<Array<Dashboard.Process>>(`/dashboard/current/top/cpu`);
34+
};
35+
36+
export const loadTopMem = () => {
37+
return http.get<Array<Dashboard.Process>>(`/dashboard/current/top/mem`);
38+
};
39+
3240
export const systemRestart = (operation: string) => {
3341
return http.post(`/dashboard/system/restart/${operation}`);
3442
};

frontend/src/views/home/index.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@ const currentChartInfo = reactive({
450450
451451
const chartsOption = ref({ ioChart1: null, networkChart: null });
452452
453+
const updateCurrentInfo = (data: Dashboard.CurrentInfo) => {
454+
currentInfo.value = {
455+
...data,
456+
topCPUItems: currentInfo.value.topCPUItems || [],
457+
topMemItems: currentInfo.value.topMemItems || [],
458+
};
459+
};
460+
453461
const changeOption = async () => {
454462
isInit.value = true;
455463
loadData();
@@ -484,7 +492,7 @@ const onLoadBaseInfo = async (isInit: boolean, range: string) => {
484492
}
485493
const res = await loadBaseInfo(searchInfo.ioOption, searchInfo.netOption);
486494
baseInfo.value = res.data;
487-
currentInfo.value = baseInfo.value.currentInfo;
495+
updateCurrentInfo(baseInfo.value.currentInfo);
488496
onLoadCurrentInfo();
489497
isStatusInit.value = false;
490498
statusRef.value?.acceptParams(currentInfo.value, baseInfo.value);
@@ -584,7 +592,7 @@ const onLoadCurrentInfo = async () => {
584592
timeNetDatas.value.splice(0, 1);
585593
}
586594
loadData();
587-
currentInfo.value = res.data;
595+
updateCurrentInfo(res.data);
588596
statusRef.value?.acceptParams(currentInfo.value, baseInfo.value);
589597
};
590598

frontend/src/views/home/status/index.vue

Lines changed: 127 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<template>
22
<div class="custom-row">
33
<el-col :xs="6" :sm="6" :md="3" :lg="3" :xl="3" align="center">
4-
<el-popover :hide-after="20" :teleported="false" :width="320" v-if="chartsOption['load']">
4+
<el-popover
5+
:hide-after="20"
6+
:teleported="false"
7+
:width="320"
8+
v-if="chartsOption['load']"
9+
@hide="onCpuPopoverHide"
10+
>
511
<el-descriptions :column="1" size="small">
612
<el-descriptions-item :label="$t('home.loadAverage', [1])">
713
{{ formatNumber(currentInfo.load1) }}
@@ -14,12 +20,12 @@
1420
</el-descriptions-item>
1521
</el-descriptions>
1622

17-
<el-button link size="small" type="primary" class="float-left mb-2" @click="showTop = !showTop">
23+
<el-button link size="small" type="primary" class="float-left mb-2" @click="toggleCpuTop">
1824
{{ $t('home.cpuTop') }}
19-
<el-icon v-if="!showTop"><ArrowRight /></el-icon>
20-
<el-icon v-if="showTop"><ArrowDown /></el-icon>
25+
<el-icon v-if="!showCpuTop"><ArrowRight /></el-icon>
26+
<el-icon v-if="showCpuTop"><ArrowDown /></el-icon>
2127
</el-button>
22-
<ComplexTable v-if="showTop" :data="currentInfo.topCPUItems">
28+
<ComplexTable v-if="showCpuTop" :data="currentInfo.topCPUItems">
2329
<el-table-column :min-width="120" show-overflow-tooltip :label="$t('menu.process')" prop="name" />
2430
<el-table-column :min-width="60" :label="$t('monitor.percent')" prop="percent">
2531
<template #default="{ row }">{{ row.percent.toFixed(2) }}%</template>
@@ -45,7 +51,13 @@
4551
<span class="input-help">{{ loadStatus(currentInfo.loadUsagePercent) }}</span>
4652
</el-col>
4753
<el-col :xs="6" :sm="6" :md="3" :lg="3" :xl="3">
48-
<el-popover :hide-after="20" :teleported="false" :width="430" v-if="chartsOption['cpu']">
54+
<el-popover
55+
:hide-after="20"
56+
:teleported="false"
57+
:width="430"
58+
v-if="chartsOption['cpu']"
59+
@hide="onCpuPopoverHide"
60+
>
4961
<el-descriptions :title="baseInfo.cpuModelName" :column="2" size="small">
5062
<el-descriptions-item :label="$t('home.core')">
5163
{{ baseInfo.cpuCores }}
@@ -71,12 +83,12 @@
7183
</div>
7284
<br />
7385

74-
<el-button link size="small" type="primary" class="mt-1 mb-2" @click="showTop = !showTop">
86+
<el-button link size="small" type="primary" class="mt-2 mb-2" @click="toggleCpuTop">
7587
{{ $t('home.cpuTop') }}
76-
<el-icon v-if="!showTop"><ArrowRight /></el-icon>
77-
<el-icon v-if="showTop"><ArrowDown /></el-icon>
88+
<el-icon v-if="!showCpuTop"><ArrowRight /></el-icon>
89+
<el-icon v-if="showCpuTop"><ArrowDown /></el-icon>
7890
</el-button>
79-
<ComplexTable v-if="showTop" :data="currentInfo.topCPUItems">
91+
<ComplexTable v-if="showCpuTop" :data="currentInfo.topCPUItems">
8092
<el-table-column :min-width="120" show-overflow-tooltip :label="$t('menu.process')" prop="name" />
8193
<el-table-column :min-width="60" :label="$t('monitor.percent')" prop="percent">
8294
<template #default="{ row }">{{ row.percent.toFixed(2) }}%</template>
@@ -107,7 +119,13 @@
107119
</div>
108120
</el-col>
109121
<el-col :xs="6" :sm="6" :md="3" :lg="3" :xl="3" align="center">
110-
<el-popover :hide-after="20" :teleported="false" :width="480" v-if="chartsOption['memory']">
122+
<el-popover
123+
:hide-after="20"
124+
:teleported="false"
125+
:width="480"
126+
v-if="chartsOption['memory']"
127+
@hide="onMemPopoverHide"
128+
>
111129
<el-descriptions direction="vertical" :title="$t('home.mem')" class="ml-1" :column="4" size="small">
112130
<el-descriptions-item :label-width="60" :label="$t('home.total')">
113131
{{ computeSize(currentInfo.memoryTotal) }}
@@ -154,12 +172,12 @@
154172
</el-descriptions-item>
155173
</el-descriptions>
156174

157-
<el-button link size="small" type="primary" class="float-left mb-2" @click="showTop = !showTop">
175+
<el-button link size="small" type="primary" class="float-left mb-2" @click="toggleMemTop">
158176
{{ $t('home.memTop') }}
159-
<el-icon v-if="!showTop"><ArrowRight /></el-icon>
160-
<el-icon v-if="showTop"><ArrowDown /></el-icon>
177+
<el-icon v-if="!showMemTop"><ArrowRight /></el-icon>
178+
<el-icon v-if="showMemTop"><ArrowDown /></el-icon>
161179
</el-button>
162-
<ComplexTable v-if="showTop" :data="currentInfo.topMemItems">
180+
<ComplexTable v-if="showMemTop" :data="currentInfo.topMemItems">
163181
<el-table-column :min-width="120" show-overflow-tooltip :label="$t('menu.process')" prop="name" />
164182
<el-table-column :min-width="100" :label="$t('monitor.memory')" prop="memory">
165183
<template #default="{ row }">
@@ -334,13 +352,19 @@
334352
import { Dashboard } from '@/api/interface/dashboard';
335353
import { computeSize } from '@/utils/util';
336354
import i18n from '@/lang';
337-
import { nextTick, ref } from 'vue';
355+
import { nextTick, onBeforeUnmount, ref } from 'vue';
338356
import { routerToFileWithPath, routerToName } from '@/utils/router';
339357
import { stopProcess } from '@/api/modules/process';
358+
import { loadTopCPU, loadTopMem } from '@/api/modules/dashboard';
340359
import { MsgSuccess } from '@/utils/message';
341360
const showMore = ref(false);
342361
const totalCount = ref();
343362
363+
let cpuPopoverTimer: ReturnType<typeof setTimeout> | null = null;
364+
let memPopoverTimer: ReturnType<typeof setTimeout> | null = null;
365+
let cpuLoading = false;
366+
let memLoading = false;
367+
344368
const baseInfo = ref<Dashboard.BaseInfo>({
345369
hostname: '',
346370
os: '',
@@ -405,7 +429,8 @@ const currentInfo = ref<Dashboard.CurrentInfo>({
405429
});
406430
407431
const cpuShowAll = ref();
408-
const showTop = ref();
432+
const showCpuTop = ref(false);
433+
const showMemTop = ref(false);
409434
const killProcessID = ref();
410435
const confirmConfRef = ref();
411436
@@ -515,6 +540,91 @@ function formatNumber(val: number) {
515540
return Number(val.toFixed(2));
516541
}
517542
543+
const toggleCpuTop = async () => {
544+
showCpuTop.value = !showCpuTop.value;
545+
if (showCpuTop.value) {
546+
await loadTopCPUData();
547+
if (cpuPopoverTimer) {
548+
clearInterval(Number(cpuPopoverTimer));
549+
}
550+
cpuPopoverTimer = setInterval(loadTopCPUData, 5000);
551+
} else {
552+
if (cpuPopoverTimer) {
553+
clearInterval(Number(cpuPopoverTimer));
554+
cpuPopoverTimer = null;
555+
}
556+
}
557+
};
558+
559+
const onCpuPopoverHide = () => {
560+
showCpuTop.value = false;
561+
if (cpuPopoverTimer) {
562+
clearInterval(Number(cpuPopoverTimer));
563+
cpuPopoverTimer = null;
564+
}
565+
};
566+
567+
const toggleMemTop = async () => {
568+
showMemTop.value = !showMemTop.value;
569+
if (showMemTop.value) {
570+
await loadTopMemData();
571+
if (memPopoverTimer) {
572+
clearInterval(Number(memPopoverTimer));
573+
}
574+
memPopoverTimer = setInterval(loadTopMemData, 5000);
575+
} else {
576+
if (memPopoverTimer) {
577+
clearInterval(Number(memPopoverTimer));
578+
memPopoverTimer = null;
579+
}
580+
}
581+
};
582+
583+
const onMemPopoverHide = () => {
584+
showMemTop.value = false;
585+
if (memPopoverTimer) {
586+
clearInterval(Number(memPopoverTimer));
587+
memPopoverTimer = null;
588+
}
589+
};
590+
591+
const loadTopCPUData = async () => {
592+
if (cpuLoading) return;
593+
cpuLoading = true;
594+
try {
595+
const res = await loadTopCPU();
596+
currentInfo.value.topCPUItems = res.data || [];
597+
} catch (_error) {
598+
// ignore load errors
599+
} finally {
600+
cpuLoading = false;
601+
}
602+
};
603+
604+
const loadTopMemData = async () => {
605+
if (memLoading) return;
606+
memLoading = true;
607+
try {
608+
const res = await loadTopMem();
609+
currentInfo.value.topMemItems = res.data || [];
610+
} catch (_error) {
611+
// ignore load errors
612+
} finally {
613+
memLoading = false;
614+
}
615+
};
616+
617+
onBeforeUnmount(() => {
618+
if (cpuPopoverTimer) {
619+
clearInterval(Number(cpuPopoverTimer));
620+
cpuPopoverTimer = null;
621+
}
622+
if (memPopoverTimer) {
623+
clearInterval(Number(memPopoverTimer));
624+
memPopoverTimer = null;
625+
}
626+
});
627+
518628
defineExpose({
519629
acceptParams,
520630
});

0 commit comments

Comments
 (0)