Skip to content

Commit 7d92436

Browse files
authored
perf: Change container overview page disk usage to asynchronous requests (#11148)
1 parent 66a070c commit 7d92436

File tree

8 files changed

+98
-53
lines changed

8 files changed

+98
-53
lines changed

agent/app/api/v2/container.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,19 @@ func (b *BaseApi) ContainerListStats(c *gin.Context) {
264264
}
265265

266266
// @Summary Load container stats size
267+
// @Accept json
268+
// @Param request body dto.OperationWithName true "request"
267269
// @Success 200 {object} dto.ContainerItemStats
268270
// @Security ApiKeyAuth
269271
// @Security Timestamp
270-
// @Router /containers/item/stats/:id [get]
272+
// @Router /containers/item/stats [post]
271273
func (b *BaseApi) ContainerItemStats(c *gin.Context) {
272-
containerID := c.Param("id")
273-
data, err := containerService.ContainerItemStats(containerID)
274+
var req dto.OperationWithName
275+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
276+
return
277+
}
278+
279+
data, err := containerService.ContainerItemStats(req)
274280
if err != nil {
275281
helper.InternalServer(c, err)
276282
return

agent/app/dto/container.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ type ContainerStatus struct {
6363
NetworkCount int `json:"networkCount"`
6464
VolumeCount int `json:"volumeCount"`
6565
RepoCount int `json:"repoCount"`
66-
67-
ContainerUsage int64 `json:"containerUsage"`
68-
ContainerReclaimable int64 `json:"containerReclaimable"`
69-
ImageUsage int64 `json:"imageUsage"`
70-
ImageReclaimable int64 `json:"imageReclaimable"`
71-
VolumeUsage int64 `json:"volumeUsage"`
72-
VolumeReclaimable int64 `json:"volumeReclaimable"`
73-
BuildCacheUsage int64 `json:"buildCacheUsage"`
74-
BuildCacheReclaimable int64 `json:"buildCacheReclaimable"`
7566
}
7667
type ResourceLimit struct {
7768
CPU int `json:"cpu"`
@@ -129,6 +120,15 @@ type ContainerUpgrade struct {
129120
type ContainerItemStats struct {
130121
SizeRw int64 `json:"sizeRw"`
131122
SizeRootFs int64 `json:"sizeRootFs"`
123+
124+
ContainerUsage int64 `json:"containerUsage"`
125+
ContainerReclaimable int64 `json:"containerReclaimable"`
126+
ImageUsage int64 `json:"imageUsage"`
127+
ImageReclaimable int64 `json:"imageReclaimable"`
128+
VolumeUsage int64 `json:"volumeUsage"`
129+
VolumeReclaimable int64 `json:"volumeReclaimable"`
130+
BuildCacheUsage int64 `json:"buildCacheUsage"`
131+
BuildCacheReclaimable int64 `json:"buildCacheReclaimable"`
132132
}
133133
type ContainerListStats struct {
134134
ContainerID string `json:"containerID"`

agent/app/service/container.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type IContainerService interface {
7070
ContainerUpgrade(req dto.ContainerUpgrade) error
7171
ContainerInfo(req dto.OperationWithName) (*dto.ContainerOperate, error)
7272
ContainerListStats() ([]dto.ContainerListStats, error)
73-
ContainerItemStats(containerID string) (dto.ContainerItemStats, error)
73+
ContainerItemStats(req dto.OperationWithName) (dto.ContainerItemStats, error)
7474
LoadResourceLimit() (*dto.ResourceLimit, error)
7575
ContainerRename(req dto.ContainerRename) error
7676
ContainerCommit(req dto.ContainerCommit) error
@@ -199,34 +199,8 @@ func (u *ContainerService) LoadStatus() (dto.ContainerStatus, error) {
199199
defer client.Close()
200200
c := context.Background()
201201

202-
usage, err := client.DiskUsage(c, types.DiskUsageOptions{})
203-
if err != nil {
204-
return data, err
205-
}
206-
207-
data.ImageCount = len(usage.Images)
208-
data.VolumeCount = len(usage.Volumes)
209-
for _, item := range usage.Images {
210-
data.ImageUsage += item.Size
211-
if item.Containers < 1 {
212-
data.ImageReclaimable += item.Size
213-
}
214-
}
215-
for _, item := range usage.Containers {
216-
data.ContainerUsage += item.SizeRw
217-
if item.State != "running" {
218-
data.ContainerReclaimable += item.SizeRw
219-
}
220-
}
221-
for _, item := range usage.Volumes {
222-
data.VolumeUsage += item.UsageData.Size
223-
if item.UsageData.RefCount == 0 {
224-
data.VolumeReclaimable += item.UsageData.Size
225-
}
226-
}
227-
for _, item := range usage.BuildCache {
228-
data.BuildCacheUsage += item.Size
229-
}
202+
images, _ := client.ImageList(c, image.ListOptions{All: true})
203+
data.ImageCount = len(images)
230204
repo, _ := imageRepoRepo.List()
231205
data.RepoCount = len(repo)
232206
templates, _ := composeRepo.List()
@@ -236,8 +210,9 @@ func (u *ContainerService) LoadStatus() (dto.ContainerStatus, error) {
236210
volumes, _ := client.VolumeList(c, volume.ListOptions{})
237211
data.VolumeCount = len(volumes.Volumes)
238212
data.ComposeCount = loadComposeCount(client)
239-
data.ContainerCount = len(usage.Containers)
240-
for _, item := range usage.Containers {
213+
containers, _ := client.ContainerList(c, container.ListOptions{All: true})
214+
data.ContainerCount = len(containers)
215+
for _, item := range containers {
241216
switch item.State {
242217
case "created":
243218
data.Created++
@@ -257,19 +232,48 @@ func (u *ContainerService) LoadStatus() (dto.ContainerStatus, error) {
257232
}
258233
return data, nil
259234
}
260-
func (u *ContainerService) ContainerItemStats(containerID string) (dto.ContainerItemStats, error) {
235+
func (u *ContainerService) ContainerItemStats(req dto.OperationWithName) (dto.ContainerItemStats, error) {
261236
var data dto.ContainerItemStats
262237
client, err := docker.NewDockerClient()
263238
if err != nil {
264239
return data, err
265240
}
266-
defer client.Close()
267-
containerInfo, _, err := client.ContainerInspectWithRaw(context.Background(), containerID, true)
241+
if req.Name != "system" {
242+
defer client.Close()
243+
containerInfo, _, err := client.ContainerInspectWithRaw(context.Background(), req.Name, true)
244+
if err != nil {
245+
return data, err
246+
}
247+
data.SizeRw = *containerInfo.SizeRw
248+
data.SizeRootFs = *containerInfo.SizeRootFs
249+
return data, nil
250+
}
251+
252+
usage, err := client.DiskUsage(context.Background(), types.DiskUsageOptions{})
268253
if err != nil {
269254
return data, err
270255
}
271-
data.SizeRw = *containerInfo.SizeRw
272-
data.SizeRootFs = *containerInfo.SizeRootFs
256+
for _, item := range usage.Images {
257+
data.ImageUsage += item.Size
258+
if item.Containers < 1 {
259+
data.ImageReclaimable += item.Size
260+
}
261+
}
262+
for _, item := range usage.Containers {
263+
data.ContainerUsage += item.SizeRw
264+
if item.State != "running" {
265+
data.ContainerReclaimable += item.SizeRw
266+
}
267+
}
268+
for _, item := range usage.Volumes {
269+
data.VolumeUsage += item.UsageData.Size
270+
if item.UsageData.RefCount == 0 {
271+
data.VolumeReclaimable += item.UsageData.Size
272+
}
273+
}
274+
for _, item := range usage.BuildCache {
275+
data.BuildCacheUsage += item.Size
276+
}
273277
return data, nil
274278
}
275279
func (u *ContainerService) ContainerListStats() ([]dto.ContainerListStats, error) {

agent/router/ro_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (s *ContainerRouter) InitRouter(Router *gin.RouterGroup) {
2424
baRouter.POST("/list/byimage", baseApi.ListContainerByImage)
2525
baRouter.GET("/status", baseApi.LoadContainerStatus)
2626
baRouter.GET("/list/stats", baseApi.ContainerListStats)
27-
baRouter.GET("/item/stats/:id", baseApi.ContainerItemStats)
27+
baRouter.POST("/item/stats", baseApi.ContainerItemStats)
2828
baRouter.GET("/search/log", baseApi.ContainerStreamLogs)
2929
baRouter.POST("/download/log", baseApi.DownloadContainerLogs)
3030
baRouter.GET("/limit", baseApi.LoadResourceLimit)

frontend/src/api/interface/container.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ export namespace Container {
137137
export interface ContainerItemStats {
138138
sizeRw: number;
139139
sizeRootFs: number;
140+
141+
containerUsage: number;
142+
containerReclaimable: number;
143+
imageUsage: number;
144+
imageReclaimable: number;
145+
volumeUsage: number;
146+
volumeReclaimable: number;
147+
buildCacheUsage: number;
148+
buildCacheReclaimable: number;
140149
}
141150
export interface ContainerListStats {
142151
containerID: string;

frontend/src/api/modules/container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const cleanContainerLog = (containerName: string, operateNode?: string) =
4444
return http.post(`/containers/clean/log${params}`, { name: containerName });
4545
};
4646
export const containerItemStats = (containerID: string) => {
47-
return http.get<Container.ContainerItemStats>(`/containers/item/stats/${containerID}`);
47+
return http.post<Container.ContainerItemStats>(`/containers/item/stats`, { name: containerID }, TimeoutEnum.T_60S);
4848
};
4949
export const containerListStats = () => {
5050
return http.get<Array<Container.ContainerListStats>>(`/containers/list/stats`);

frontend/src/views/container/dashboard/index.vue

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@
9090

9191
<CardWithHeader :header="$t('container.diskUsage')" class="card-interval">
9292
<template #body>
93-
<el-descriptions direction="vertical" align="center" :column="4" class="mt-2">
93+
<el-descriptions
94+
direction="vertical"
95+
align="center"
96+
v-loading="usageLoading"
97+
:column="4"
98+
class="mt-2"
99+
>
94100
<el-descriptions-item label-width="25%" align="center" :label="$t('container.image')">
95101
{{
96102
$t('container.usage', [
@@ -188,7 +194,7 @@
188194
</template>
189195

190196
<script lang="ts" setup>
191-
import { containerPrune, loadContainerStatus, loadDaemonJson } from '@/api/modules/container';
197+
import { containerItemStats, containerPrune, loadContainerStatus, loadDaemonJson } from '@/api/modules/container';
192198
import DockerStatus from '@/views/container/docker-status/index.vue';
193199
import { getSettingInfo } from '@/api/modules/setting';
194200
import { computeSize2, newUUID } from '@/utils/util';
@@ -200,6 +206,7 @@ import i18n from '@/lang';
200206
const taskLogRef = ref();
201207
202208
const loading = ref();
209+
const usageLoading = ref(false);
203210
const isActive = ref(false);
204211
const isExist = ref(false);
205212
const countItem = reactive({
@@ -237,6 +244,7 @@ const search = () => {
237244
return;
238245
}
239246
loadContainerCount();
247+
loadUsage();
240248
loadContainerSetting();
241249
};
242250
@@ -269,6 +277,24 @@ const loadContainerCount = async () => {
269277
});
270278
};
271279
280+
const loadUsage = async () => {
281+
usageLoading.value = true;
282+
await containerItemStats('system')
283+
.then((res) => {
284+
countItem.containerUsage = res.data.containerUsage;
285+
countItem.containerReclaimable = res.data.containerReclaimable;
286+
countItem.imageUsage = res.data.imageUsage;
287+
countItem.imageReclaimable = res.data.imageReclaimable;
288+
countItem.volumeUsage = res.data.volumeUsage;
289+
countItem.volumeReclaimable = res.data.volumeReclaimable;
290+
countItem.buildCacheUsage = res.data.buildCacheUsage;
291+
countItem.buildCacheReclaimable = res.data.buildCacheReclaimable;
292+
})
293+
.finally(() => {
294+
usageLoading.value = false;
295+
});
296+
};
297+
272298
const loadContainerSetting = async () => {
273299
const res = await loadDaemonJson();
274300
countItem.mirrors = res.data.registryMirrors || [];

frontend/src/views/container/template/detail/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
v-model="detailInfo"
66
mode="yaml"
77
:heightDiff="160"
8-
:disabled="true"
8+
:readonly="true"
99
></CodemirrorPro>
1010
<template #footer>
1111
<span class="dialog-footer">

0 commit comments

Comments
 (0)