From ea20e64d51267da4a239883eb09fd2d3e88943f0 Mon Sep 17 00:00:00 2001 From: ioito Date: Mon, 28 Mar 2022 11:32:10 +0800 Subject: [PATCH] fix(region): order by host storage --- pkg/apis/compute/host.go | 3 +++ pkg/compute/models/hosts.go | 14 ++++++++++++++ pkg/mcclient/options/compute/host.go | 1 + 3 files changed, 18 insertions(+) diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index 16239a210f..e3c79088fb 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -129,6 +129,9 @@ type HostListInput struct { // 按虚拟机数量排序 // enum: asc,desc OrderByServerCount string `json:"order_by_server_count"` + // 按存储大小排序 + // enmu: asc,desc + OrderByStorage string `json:"order_by_storage"` } type HostDetails struct { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 40279fbdba..149bcaac89 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -491,6 +491,20 @@ func (manager *SHostManager) OrderByExtraFields( db.OrderByFields(q, []string{query.OrderByServerCount}, []sqlchemy.IQueryField{guestCounts.Field("guest_count")}) } + if db.NeedOrderQuery([]string{query.OrderByStorage}) { + hoststorages := HoststorageManager.Query().SubQuery() + storages := StorageManager.Query().IsTrue("enabled").In("storage_type", api.HOST_STORAGE_LOCAL_TYPES).SubQuery() + hoststoragesQ := hoststorages.Query( + hoststorages.Field("host_id"), + sqlchemy.SUM("storage_capacity", storages.Field("capacity")), + ) + hoststoragesQ = hoststoragesQ.LeftJoin(storages, sqlchemy.Equals(hoststoragesQ.Field("storage_id"), storages.Field("id"))) + hoststoragesSQ := hoststoragesQ.GroupBy(hoststoragesQ.Field("host_id")).SubQuery() + + q = q.LeftJoin(hoststoragesSQ, sqlchemy.Equals(q.Field("id"), hoststoragesSQ.Field("host_id"))) + db.OrderByFields(q, []string{query.OrderByStorage}, []sqlchemy.IQueryField{hoststoragesSQ.Field("storage_capacity")}) + } + return q, nil } diff --git a/pkg/mcclient/options/compute/host.go b/pkg/mcclient/options/compute/host.go index 12a8b67228..310f2542fd 100644 --- a/pkg/mcclient/options/compute/host.go +++ b/pkg/mcclient/options/compute/host.go @@ -53,6 +53,7 @@ type HostListOptions struct { Sn string `help:"find host by sn"` OrderByServerCount string `help:"Order by server count" choices:"desc|asc"` + OrderByStorage string `help:"Order by host storage" choices:"desc|asc"` options.BaseListOptions }