Merge pull request #13803 from ioito/hotfix/qx-host-order-by-storage

fix(region): order by host storage
This commit is contained in:
Zexi Li
2022-03-29 11:53:10 +08:00
committed by GitHub
3 changed files with 18 additions and 0 deletions
+3
View File
@@ -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 {
+14
View File
@@ -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
}
+1
View File
@@ -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
}