Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加容器状态统计及状态快速切换 #6594

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions frontend/src/api/interface/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export namespace Container {
orderBy: string;
order: string;
}
export interface ContainerStatus {
all: number;
created: number;
running: number;
paused: number;
restarting: number;
removing: number;
exited: number;
dead: number;
}
export interface ResourceLimit {
cpu: number;
memory: number;
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/api/modules/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const searchContainer = (params: Container.ContainerSearch) => {
export const listContainer = () => {
return http.post<Array<string>>(`/containers/list`, {});
};
export const loadContainerStatus = () => {
return http.get<Container.ContainerStatus>(`/containers/status`);
};
export const loadResourceLimit = () => {
return http.get<Container.ResourceLimit>(`/containers/limit`);
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lang/modules/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const message = {
host: '主机',
files: '文件',
monitor: '监控',
terminal: '终端',
terminal: 'WEB终端',
settings: '面板设置',
toolbox: '工具箱',
logs: '日志审计',
Expand Down Expand Up @@ -1137,7 +1137,7 @@ const message = {
role: '权限',
info: '属性',
linkFile: '软连接文件',
terminal: 'Web终端',
terminal: '终端',
shareList: '分享列表',
zip: '压缩',
group: '用户组',
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/routers/modules/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ const containerRouter = {
requiresAuth: false,
},
},
{
path: 'container/operate',
name: 'ContainerCreate',
component: () => import('@/views/container/container/operate/index.vue'),
props: true,
hidden: true,
meta: {
activeMenu: '/containers',
requiresAuth: false,
},
},
{
path: 'composeDetail/:filters?',
name: 'ComposeDetail',
Expand Down
Loading
Loading