mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-29 10:16:09 +08:00
fix: Optimize container filter component for cronjob (#9595)
<img width="633" height="304" alt="image" src="https://github.com/user-attachments/assets/b5077719-ddf8-438c-9ce4-ca6bb76d7217" />
This commit is contained in:
parent
7994193a39
commit
fb7f638a54
6 changed files with 34 additions and 25 deletions
|
|
@ -50,12 +50,7 @@ func (b *BaseApi) LoadContainerUsers(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
list, err := containerService.LoadUsers(req)
|
||||
if err != nil {
|
||||
helper.InternalServer(c, err)
|
||||
return
|
||||
}
|
||||
helper.SuccessWithData(c, list)
|
||||
helper.SuccessWithData(c, containerService.LoadUsers(req))
|
||||
}
|
||||
|
||||
// @Tags Container
|
||||
|
|
|
|||
|
|
@ -39,6 +39,11 @@ type ContainerInfo struct {
|
|||
Websites []string `json:"websites"`
|
||||
}
|
||||
|
||||
type ContainerOptions struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type ContainerStatus struct {
|
||||
All uint `json:"all"`
|
||||
Created uint `json:"created"`
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ type ContainerService struct{}
|
|||
|
||||
type IContainerService interface {
|
||||
Page(req dto.PageContainer) (int64, interface{}, error)
|
||||
List() []string
|
||||
List() []dto.ContainerOptions
|
||||
LoadStatus() (dto.ContainerStatus, error)
|
||||
PageNetwork(req dto.SearchWithPage) (int64, interface{}, error)
|
||||
ListNetwork() ([]dto.Options, error)
|
||||
|
|
@ -84,7 +84,7 @@ type IContainerService interface {
|
|||
ComposeUpdate(req dto.ComposeUpdate) error
|
||||
Prune(req dto.ContainerPrune) (dto.ContainerPruneReport, error)
|
||||
|
||||
LoadUsers(req dto.OperationWithName) ([]string, error)
|
||||
LoadUsers(req dto.OperationWithName) []string
|
||||
|
||||
StreamLogs(ctx *gin.Context, params dto.StreamLog)
|
||||
}
|
||||
|
|
@ -222,7 +222,8 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro
|
|||
return int64(total), backDatas, nil
|
||||
}
|
||||
|
||||
func (u *ContainerService) List() []string {
|
||||
func (u *ContainerService) List() []dto.ContainerOptions {
|
||||
var options []dto.ContainerOptions
|
||||
client, err := docker.NewDockerClient()
|
||||
if err != nil {
|
||||
global.LOG.Errorf("load docker client for contianer list failed, err: %v", err)
|
||||
|
|
@ -231,19 +232,18 @@ func (u *ContainerService) List() []string {
|
|||
defer client.Close()
|
||||
containers, err := client.ContainerList(context.Background(), container.ListOptions{All: true})
|
||||
if err != nil {
|
||||
global.LOG.Errorf("load contianer list failed, err: %v", err)
|
||||
global.LOG.Errorf("load container list failed, err: %v", err)
|
||||
return nil
|
||||
}
|
||||
var datas []string
|
||||
for _, container := range containers {
|
||||
for _, name := range container.Names {
|
||||
if len(name) != 0 {
|
||||
datas = append(datas, strings.TrimPrefix(name, "/"))
|
||||
options = append(options, dto.ContainerOptions{Name: strings.TrimPrefix(name, "/"), State: container.State})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return datas
|
||||
return options
|
||||
}
|
||||
|
||||
func (u *ContainerService) LoadStatus() (dto.ContainerStatus, error) {
|
||||
|
|
@ -1140,19 +1140,19 @@ func (u *ContainerService) ContainerStats(id string) (*dto.ContainerStats, error
|
|||
return &data, nil
|
||||
}
|
||||
|
||||
func (u *ContainerService) LoadUsers(req dto.OperationWithName) ([]string, error) {
|
||||
func (u *ContainerService) LoadUsers(req dto.OperationWithName) []string {
|
||||
var users []string
|
||||
std, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s cat /etc/passwd", req.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return users
|
||||
}
|
||||
var users []string
|
||||
lines := strings.Split(string(std), "\n")
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, ":") {
|
||||
users = append(users, strings.Split(line, ":")[0])
|
||||
}
|
||||
}
|
||||
return users, nil
|
||||
return users
|
||||
}
|
||||
|
||||
func stringsToMap(list []string) map[string]string {
|
||||
|
|
|
|||
|
|
@ -119,6 +119,10 @@ export namespace Container {
|
|||
cpuPercent: number;
|
||||
memoryPercent: number;
|
||||
}
|
||||
export interface ContainerInfo {
|
||||
name: string;
|
||||
state: string;
|
||||
}
|
||||
export interface ContainerListStats {
|
||||
containerID: string;
|
||||
cpuTotalUsage: number;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const searchContainer = (params: Container.ContainerSearch) => {
|
|||
return http.post<ResPage<Container.ContainerInfo>>(`/containers/search`, params, TimeoutEnum.T_40S);
|
||||
};
|
||||
export const listContainer = () => {
|
||||
return http.post<Array<string>>(`/containers/list`, {});
|
||||
return http.post<Array<Container.ContainerInfo>>(`/containers/list`, {});
|
||||
};
|
||||
export const loadContainerUsers = (name: string) => {
|
||||
return http.post<Array<string>>(`/containers/users`, { name: name });
|
||||
|
|
|
|||
|
|
@ -354,15 +354,19 @@
|
|||
<LayoutCol>
|
||||
<el-form-item :label="$t('cronjob.containerName')" prop="containerName">
|
||||
<el-select
|
||||
@change="loadUserOptions(false)"
|
||||
v-model="form.containerName"
|
||||
@change="loadUserOptions(false)"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in containerOptions"
|
||||
:key="item"
|
||||
:value="item"
|
||||
:label="item"
|
||||
/>
|
||||
<div v-for="item in containerOptions" :key="item.name">
|
||||
<el-option :value="item.name" :label="item.name">
|
||||
{{ item.name }}
|
||||
<Status
|
||||
class="float-right"
|
||||
:key="item.state"
|
||||
:status="item.state"
|
||||
/>
|
||||
</el-option>
|
||||
</div>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</LayoutCol>
|
||||
|
|
@ -1278,6 +1282,7 @@ const changeAccount = async () => {
|
|||
};
|
||||
|
||||
const loadUserOptions = async (isInit: boolean) => {
|
||||
userOptions.value = [];
|
||||
if (!form.inContainer) {
|
||||
const res = await loadUsers();
|
||||
userOptions.value = res.data || [];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue