mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 14:19:49 +08:00
fix(region): order by host storage
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user