Skip to content

Commit 436cb3a

Browse files
committed
perf: Change container overview page disk usage to asynchronous requests
1 parent bb9d904 commit 436cb3a

File tree

4 files changed

+61
-51
lines changed

4 files changed

+61
-51
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: 42 additions & 38 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) {
@@ -1786,9 +1790,9 @@ func searchWithFilter(req dto.PageContainer, containers []container.Summary) []d
17861790
}
17871791
records = append(records, info)
17881792
}
1789-
descriptions, _ := settingRepo.GetDescriptionList(repo.WithByType("container"))
1793+
dscriptions, _ := settingRepo.GetDescriptionList(repo.WithByType("container"))
17901794
for i := 0; i < len(records); i++ {
1791-
for _, desc := range descriptions {
1795+
for _, desc := range dscriptions {
17921796
if desc.ID == records[i].ContainerID {
17931797
records[i].Description = desc.Description
17941798
records[i].IsPinned = desc.IsPinned

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)

0 commit comments

Comments
 (0)