diff --git a/pkg/apis/compute/input.go b/pkg/apis/compute/input.go index 5d3adbfcf5..3cdf33565e 100644 --- a/pkg/apis/compute/input.go +++ b/pkg/apis/compute/input.go @@ -147,6 +147,7 @@ type SnapshotPolicyListInput struct { // 按绑定的磁盘数量排序 // pattern:asc|desc OrderByBindDiskCount string `json:"order_by_bind_disk_count"` + OrderBySnapshotCount string `json:"order_by_snapshot_count"` // 按类型过滤 Type string `json:"type"` } diff --git a/pkg/compute/models/snapshotpolicy.go b/pkg/compute/models/snapshotpolicy.go index 5740f6af11..638bb401ae 100644 --- a/pkg/compute/models/snapshotpolicy.go +++ b/pkg/compute/models/snapshotpolicy.go @@ -698,6 +698,22 @@ func (manager *SSnapshotPolicyManager) OrderByExtraFields( q = q.AppendField(sdSQ.Field("disk_count")) q = db.OrderByFields(q, []string{input.OrderByBindDiskCount}, []sqlchemy.IQueryField{q.Field("disk_count")}) } + + if db.NeedOrderQuery([]string{input.OrderBySnapshotCount}) { + spSQ := SnapshotPolicyResourceManager.Query().Equals("resource_type", api.SNAPSHOT_POLICY_TYPE_DISK).SubQuery() + ssq := SnapshotManager.Query().SubQuery() + pQ := spSQ.Query( + spSQ.Field("snapshotpolicy_id"), + sqlchemy.COUNT("snapshot_count", ssq.Field("id")), + ).Join(ssq, sqlchemy.Equals(ssq.Field("disk_id"), spSQ.Field("resource_id"))).GroupBy(spSQ.Field("snapshotpolicy_id")) + + pq := pQ.SubQuery() + + q = q.LeftJoin(pq, sqlchemy.Equals(pq.Field("snapshotpolicy_id"), q.Field("id"))) + q = q.AppendField(q.QueryFields()...) + q = q.AppendField(pq.Field("snapshot_count")) + q = db.OrderByFields(q, []string{input.OrderBySnapshotCount}, []sqlchemy.IQueryField{q.Field("snapshot_count")}) + } return q, nil } diff --git a/pkg/mcclient/options/compute/snapshotpolicy.go b/pkg/mcclient/options/compute/snapshotpolicy.go index 14b3c503c0..a511df616a 100644 --- a/pkg/mcclient/options/compute/snapshotpolicy.go +++ b/pkg/mcclient/options/compute/snapshotpolicy.go @@ -27,6 +27,7 @@ type SnapshotPolicyListOptions struct { options.BaseListOptions OrderByBindDiskCount string + OrderBySnapshotCount string } func (opts *SnapshotPolicyListOptions) Params() (jsonutils.JSONObject, error) {