diff --git a/backend/app/dto/common_req.go b/backend/app/dto/common_req.go index 014c06e2d..2810ec83a 100644 --- a/backend/app/dto/common_req.go +++ b/backend/app/dto/common_req.go @@ -2,7 +2,9 @@ package dto type SearchWithPage struct { PageInfo - Info string `json:"info"` + Info string `json:"info"` + OrderBy string `json:"orderBy"` + Order string `json:"order"` } type PageInfo struct { diff --git a/backend/app/dto/container.go b/backend/app/dto/container.go index 37e493ed9..d3ee164c0 100644 --- a/backend/app/dto/container.go +++ b/backend/app/dto/container.go @@ -5,6 +5,8 @@ import "time" type PageContainer struct { PageInfo Name string `json:"name"` + OrderBy string `json:"orderBy"` + Order string `json:"order"` Filters string `json:"filters"` } diff --git a/backend/app/dto/request/website.go b/backend/app/dto/request/website.go index 037592055..4e7fba52a 100644 --- a/backend/app/dto/request/website.go +++ b/backend/app/dto/request/website.go @@ -7,6 +7,8 @@ import ( type WebsiteSearch struct { dto.PageInfo Name string `json:"name"` + OrderBy string `json:"orderBy"` + Order string `json:"order"` WebsiteGroupID uint `json:"websiteGroupId"` } diff --git a/backend/app/repo/common.go b/backend/app/repo/common.go index 8be9ee4ed..ec52f2bd3 100644 --- a/backend/app/repo/common.go +++ b/backend/app/repo/common.go @@ -2,6 +2,7 @@ package repo import ( "context" + "fmt" "time" "github.com/1Panel-dev/1Panel/backend/constant" @@ -16,6 +17,7 @@ type ICommonRepo interface { WithByName(name string) DBOption WithByType(tp string) DBOption WithOrderBy(orderStr string) DBOption + WithOrderRuleBy(orderBy, order string) DBOption WithByGroupID(groupID uint) DBOption WithLikeName(name string) DBOption WithIdsIn(ids []uint) DBOption @@ -93,6 +95,21 @@ func (c *CommonRepo) WithOrderBy(orderStr string) DBOption { } } +func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) DBOption { + switch order { + case constant.OrderDesc: + order = "desc" + case constant.OrderAsc: + order = "asc" + default: + orderBy = "created_at" + order = "desc" + } + return func(g *gorm.DB) *gorm.DB { + return g.Order(fmt.Sprintf("%s %s", orderBy, order)) + } +} + func (c *CommonRepo) WithIdsIn(ids []uint) DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("id in (?)", ids) diff --git a/backend/app/repo/cronjob.go b/backend/app/repo/cronjob.go index 29f42b545..0d630e895 100644 --- a/backend/app/repo/cronjob.go +++ b/backend/app/repo/cronjob.go @@ -83,7 +83,7 @@ func (u *CronjobRepo) Page(page, size int, opts ...DBOption) (int64, []model.Cro } count := int64(0) db = db.Count(&count) - err := db.Order("created_at desc").Limit(size).Offset(size * (page - 1)).Find(&cronjobs).Error + err := db.Limit(size).Offset(size * (page - 1)).Find(&cronjobs).Error return count, cronjobs, err } diff --git a/backend/app/service/container.go b/backend/app/service/container.go index 7701c4c9c..3dbb56d85 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -67,9 +67,8 @@ func NewIContainerService() IContainerService { func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, error) { var ( - records []types.Container - list []types.Container - backDatas []dto.ContainerInfo + records []types.Container + list []types.Container ) client, err := docker.NewDockerClient() if err != nil { @@ -95,9 +94,30 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro } } } - sort.Slice(list, func(i, j int) bool { - return list[i].Created > list[j].Created - }) + switch req.OrderBy { + case "name": + sort.Slice(list, func(i, j int) bool { + if req.Order == constant.OrderAsc { + return list[i].Names[0][1:] < list[j].Names[0][1:] + } + 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 { + return list[i].Created < list[j].Created + } + return list[i].Created > list[j].Created + }) + } + total, start, end := len(list), (req.Page-1)*req.PageSize, req.Page*req.PageSize if start > total { records = make([]types.Container, 0) @@ -108,10 +128,11 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro records = list[start:end] } + backDatas := make([]dto.ContainerInfo, len(records)) var wg sync.WaitGroup wg.Add(len(records)) - for _, container := range records { - go func(item types.Container) { + for i := 0; i < len(records); i++ { + go func(item types.Container, i int) { IsFromCompose := false if _, ok := item.Labels[composeProjectLabel]; ok { IsFromCompose = true @@ -129,7 +150,7 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro ports = append(ports, fmt.Sprintf("%v:%v/%s", port.PublicPort, port.PrivatePort, port.Type)) } cpu, mem := loadCpuAndMem(client, item.ID) - backDatas = append(backDatas, dto.ContainerInfo{ + backDatas[i] = dto.ContainerInfo{ ContainerID: item.ID, CreateTime: time.Unix(item.Created, 0).Format("2006-01-02 15:04:05"), Name: item.Names[0][1:], @@ -142,9 +163,9 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro Ports: ports, IsFromApp: IsFromApp, IsFromCompose: IsFromCompose, - }) + } wg.Done() - }(container) + }(records[i], i) } wg.Wait() diff --git a/backend/app/service/cornjob.go b/backend/app/service/cornjob.go index 0458efbc3..52ecc2dc5 100644 --- a/backend/app/service/cornjob.go +++ b/backend/app/service/cornjob.go @@ -36,7 +36,7 @@ func NewICronjobService() ICronjobService { } func (u *CronjobService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) { - total, cronjobs, err := cronjobRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info)) + total, cronjobs, err := cronjobRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info), commonRepo.WithOrderRuleBy(search.OrderBy, search.Order)) var dtoCronjobs []dto.CronjobInfo for _, cronjob := range cronjobs { var item dto.CronjobInfo diff --git a/backend/app/service/database_mysql.go b/backend/app/service/database_mysql.go index 70a5901ce..58a4ff293 100644 --- a/backend/app/service/database_mysql.go +++ b/backend/app/service/database_mysql.go @@ -48,7 +48,7 @@ func NewIMysqlService() IMysqlService { } func (u *MysqlService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) { - total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info)) + total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info), commonRepo.WithOrderRuleBy(search.OrderBy, search.Order)) var dtoMysqls []dto.MysqlDBInfo for _, mysql := range mysqls { var item dto.MysqlDBInfo diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 5b5632fa6..79bbcc725 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -8,6 +8,14 @@ import ( "encoding/pem" "errors" "fmt" + "os" + "path" + "reflect" + "regexp" + "strconv" + "strings" + "time" + "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" @@ -18,13 +26,6 @@ import ( "golang.org/x/crypto/bcrypt" "gopkg.in/ini.v1" "gorm.io/gorm" - "os" - "path" - "reflect" - "regexp" - "strconv" - "strings" - "time" "github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/app/dto/response" @@ -96,7 +97,7 @@ func (w WebsiteService) PageWebsite(req request.WebsiteSearch) (int64, []respons } return 0, nil, err } - opts = append(opts, commonRepo.WithOrderBy("created_at desc")) + opts = append(opts, commonRepo.WithOrderRuleBy(req.OrderBy, req.Order)) if req.Name != "" { opts = append(opts, websiteRepo.WithDomainLike(req.Name)) } diff --git a/backend/constant/status.go b/backend/constant/status.go index a7bdab663..2e9ec0383 100644 --- a/backend/constant/status.go +++ b/backend/constant/status.go @@ -10,4 +10,7 @@ const ( StatusEnable = "Enable" StatusDisable = "Disable" StatusNone = "None" + + OrderDesc = "descending" + OrderAsc = "ascending" ) diff --git a/frontend/src/api/interface/container.ts b/frontend/src/api/interface/container.ts index c814ee3e7..42d232787 100644 --- a/frontend/src/api/interface/container.ts +++ b/frontend/src/api/interface/container.ts @@ -9,6 +9,8 @@ export namespace Container { export interface ContainerSearch extends ReqPage { name: string; filters: string; + orderBy: string; + order: string; } export interface ResourceLimit { cpu: number; diff --git a/frontend/src/api/interface/index.ts b/frontend/src/api/interface/index.ts index 375625d2d..eff34b927 100644 --- a/frontend/src/api/interface/index.ts +++ b/frontend/src/api/interface/index.ts @@ -22,6 +22,8 @@ export interface SearchWithPage { info: string; page: number; pageSize: number; + orderBy?: string; + order?: string; } export interface CommonModel { id: number; diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index c2dc69fc8..de59e711f 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -39,6 +39,8 @@ export namespace Website { export interface WebSiteSearch extends ReqPage { name: string; + orderBy: string; + order: string; websiteGroupId: number; } diff --git a/frontend/src/views/container/container/index.vue b/frontend/src/views/container/container/index.vue index 4f876fd3e..5426d3f2e 100644 --- a/frontend/src/views/container/container/index.vue +++ b/frontend/src/views/container/container/index.vue @@ -60,10 +60,11 @@ :pagination-config="paginationConfig" v-model:selects="selects" :data="data" + @sort-change="search" @search="search" > - + @@ -74,7 +75,7 @@ min-width="80" prop="imageName" /> - + @@ -225,13 +226,15 @@ const mydetail = ref(); const dialogContainerLogRef = ref(); const dialogReNameRef = ref(); -const search = async () => { +const search = async (column?: any) => { let filterItem = props.filters ? props.filters : ''; let params = { name: searchName.value, page: paginationConfig.currentPage, pageSize: paginationConfig.pageSize, filters: filterItem, + orderBy: column?.order ? column.prop : 'created_at', + order: column?.order ? column.order : 'null', }; loading.value = true; await searchContainer(params) diff --git a/frontend/src/views/cronjob/index.vue b/frontend/src/views/cronjob/index.vue index 4a1795c4a..9ec15250b 100644 --- a/frontend/src/views/cronjob/index.vue +++ b/frontend/src/views/cronjob/index.vue @@ -45,16 +45,17 @@ - + - + @@ -199,11 +200,13 @@ const weekOptions = [ { label: i18n.global.t('cronjob.sunday'), value: 7 }, ]; -const search = async () => { +const search = async (column?: any) => { let params = { info: searchName.value, page: paginationConfig.currentPage, pageSize: paginationConfig.pageSize, + orderBy: column?.order ? column.prop : 'created_at', + order: column?.order ? column.order : 'null', }; loading.value = true; await getCronjobPage(params) diff --git a/frontend/src/views/database/mysql/index.vue b/frontend/src/views/database/mysql/index.vue index adfb14caa..789a9a6f0 100644 --- a/frontend/src/views/database/mysql/index.vue +++ b/frontend/src/views/database/mysql/index.vue @@ -43,11 +43,12 @@