From 27e4fc31f9d07e0e80e4b026b36ca7824780fed9 Mon Sep 17 00:00:00 2001 From: tb365 Date: Fri, 27 Aug 2021 09:45:31 +0800 Subject: [PATCH 1/2] count host fix --- pkg/compute/models/hosts.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 5de50b6b23..087ad43e99 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -1061,7 +1061,12 @@ func (self *SHostManager) AllowGetPropertyHostTypeCount(ctx context.Context, use func (self *SHostManager) GetPropertyHostTypeCount(ctx context.Context, userCred mcclient.TokenCredential, query api.HostListInput) (jsonutils.JSONObject, error) { hosts := self.Query().SubQuery() - q := hosts.Query(hosts.Field("host_type"), sqlchemy.COUNT("count", hosts.Field("id"))) + // select host_type, (case host_type when 'huaweicloudstack' then count(DISTINCT external_id) else count(id) end) as count from hosts_tbl group by host_type; + cs := sqlchemy.NewCase() + hcso := sqlchemy.Equals(hosts.Field("host_type"), api.HOST_TYPE_HUAWEI_CLOUD_STACK) + cs.When(hcso, sqlchemy.COUNT("", sqlchemy.DISTINCT("", hosts.Field("external_id")))) + cs.Else(sqlchemy.COUNT("", hosts.Field("id"))) + q := hosts.Query(hosts.Field("host_type"), sqlchemy.NewFunction(cs, "count")) return self.getCount(ctx, userCred, q, query) } From 75a0dee0e4a8cfdfe5c243f12df008371f2d1dfe Mon Sep 17 00:00:00 2001 From: tb365 Date: Tue, 31 Aug 2021 15:48:56 +0800 Subject: [PATCH 2/2] change host type to hcso --- pkg/compute/models/hosts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 087ad43e99..61a35f785c 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -1063,7 +1063,7 @@ func (self *SHostManager) GetPropertyHostTypeCount(ctx context.Context, userCred hosts := self.Query().SubQuery() // select host_type, (case host_type when 'huaweicloudstack' then count(DISTINCT external_id) else count(id) end) as count from hosts_tbl group by host_type; cs := sqlchemy.NewCase() - hcso := sqlchemy.Equals(hosts.Field("host_type"), api.HOST_TYPE_HUAWEI_CLOUD_STACK) + hcso := sqlchemy.Equals(hosts.Field("host_type"), api.HOST_TYPE_HCSO) cs.When(hcso, sqlchemy.COUNT("", sqlchemy.DISTINCT("", hosts.Field("external_id")))) cs.Else(sqlchemy.COUNT("", hosts.Field("id"))) q := hosts.Query(hosts.Field("host_type"), sqlchemy.NewFunction(cs, "count"))