Skip to content

Commit

Permalink
feat: 增加容器各状态数量统计
Browse files Browse the repository at this point in the history
  • Loading branch information
ssonglius11 committed Sep 26, 2024
1 parent f8431c7 commit 2bfe0da
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
18 changes: 18 additions & 0 deletions agent/app/api/v2/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ func (b *BaseApi) ListContainer(c *gin.Context) {
helper.SuccessWithData(c, list)
}


// @Tags Container
// @Summary Load containers status
// @Description 获取容器状态
// @Accept json
// @Produce json
// @Success 200
// @Security ApiKeyAuth
// @Router /containers/status [get]
func (b *BaseApi) LoadContainerStatus(c *gin.Context) {
data, err := containerService.LoadStatus()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, data)
}

// @Tags Container Compose
// @Summary Page composes
// @Description 获取编排列表分页
Expand Down
12 changes: 11 additions & 1 deletion agent/app/dto/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type PageContainer struct {
PageInfo
Name string `json:"name"`
State string `json:"state" validate:"required,oneof=all created running paused restarting removing exited dead"`
OrderBy string `json:"orderBy" validate:"required,oneof=name state created_at"`
OrderBy string `json:"orderBy" validate:"required,oneof=name created_at"`
Order string `json:"order" validate:"required,oneof=null ascending descending"`
Filters string `json:"filters"`
ExcludeAppStore bool `json:"excludeAppStore"`
Expand Down Expand Up @@ -39,6 +39,16 @@ type ContainerInfo struct {
Websites []string `json:"websites"`
}

type ContainerStatus struct {
All uint `json:"all"`
Created uint `json:"created"`
Running uint `json:"running"`
Paused uint `json:"paused"`
Restarting uint `json:"restarting"`
Removing uint `json:"removing"`
Exited uint `json:"exited"`
Dead uint `json:"dead"`
}
type ResourceLimit struct {
CPU int `json:"cpu"`
Memory uint64 `json:"memory"`
Expand Down
40 changes: 33 additions & 7 deletions agent/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type ContainerService struct{}
type IContainerService interface {
Page(req dto.PageContainer) (int64, interface{}, error)
List() ([]string, error)
LoadStatus() (dto.ContainerStatus, error)
PageNetwork(req dto.SearchWithPage) (int64, interface{}, error)
ListNetwork() ([]dto.Options, error)
PageVolume(req dto.SearchWithPage) (int64, interface{}, error)
Expand Down Expand Up @@ -149,13 +150,6 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro
}
return list[i].Names[0][1:] > list[j].Names[0][1:]
})
case "state":
sort.Slice(list, func(i, j int) bool {
if req.Order == constant.OrderAsc {
return list[i].State < list[j].State
}
return list[i].State > list[j].State
})
default:
sort.Slice(list, func(i, j int) bool {
if req.Order == constant.OrderAsc {
Expand Down Expand Up @@ -245,6 +239,38 @@ func (u *ContainerService) List() ([]string, error) {
return datas, nil
}

func (u *ContainerService) LoadStatus() (dto.ContainerStatus, error) {
var data dto.ContainerStatus
client, err := docker.NewDockerClient()
if err != nil {
return data, err
}
defer client.Close()
containers, err := client.ContainerList(context.Background(), container.ListOptions{All: true})
if err != nil {
return data, err
}
data.All = uint(len(containers))
for _, item := range containers {
switch item.State {
case "created":
data.Created++
case "running":
data.Running++
case "paused":
data.Paused++
case "restarting":
data.Restarting++
case "dead":
data.Dead++
case "exited":
data.Exited++
case "removing":
data.Removing++
}
}
return data, nil
}
func (u *ContainerService) ContainerListStats() ([]dto.ContainerListStats, error) {
client, err := docker.NewDockerClient()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions agent/router/ro_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (s *ContainerRouter) InitRouter(Router *gin.RouterGroup) {
baRouter.POST("/info", baseApi.ContainerInfo)
baRouter.POST("/search", baseApi.SearchContainer)
baRouter.POST("/list", baseApi.ListContainer)
baRouter.GET("/status", baseApi.LoadContainerStatus)
baRouter.GET("/list/stats", baseApi.ContainerListStats)
baRouter.GET("/search/log", baseApi.ContainerLogs)
baRouter.POST("/download/log", baseApi.DownloadContainerLogs)
Expand Down

0 comments on commit 2bfe0da

Please sign in to comment.