From c730e58e7576c978ca8b8508ff2f6a47a8f96cb9 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Fri, 24 Sep 2021 18:19:06 +0800 Subject: [PATCH] fix(region): add project and domain statistic --- cmd/climc/shell/compute/servers.go | 2 + pkg/apis/output.go | 6 +++ pkg/cloudcommon/db/virtualresource.go | 56 +++++++++++++++++++++++++++ pkg/mcclient/options/base.go | 14 +++++++ pkg/mcclient/options/servers.go | 10 +++++ 5 files changed, 88 insertions(+) diff --git a/cmd/climc/shell/compute/servers.go b/cmd/climc/shell/compute/servers.go index 6757b9e49e..6176f23c2a 100644 --- a/cmd/climc/shell/compute/servers.go +++ b/cmd/climc/shell/compute/servers.go @@ -101,6 +101,8 @@ func init() { cmd.Get("change-owner-candidate-domains", new(options.ServerChangeOwnerCandidateDomainsOptions)) cmd.Get("change-owner-candidate-domains", new(options.ServerChangeOwnerCandidateDomainsOptions)) cmd.GetProperty(&options.ServerStatusStatisticsOptions{}) + cmd.GetProperty(&options.ServerProjectStatisticsOptions{}) + cmd.GetProperty(&options.ServerDomainStatisticsOptions{}) type ServerTaskShowOptions struct { ID string `help:"ID or name of server" json:"-"` diff --git a/pkg/apis/output.go b/pkg/apis/output.go index f628293c3a..933494493b 100644 --- a/pkg/apis/output.go +++ b/pkg/apis/output.go @@ -238,3 +238,9 @@ type StatusStatistic struct { // 需要指定pending_delete=all PendingDeletedCount int64 `json:"pending_deleted_count"` } + +type ProjectStatistic struct { + Count int + Id string + Name string +} diff --git a/pkg/cloudcommon/db/virtualresource.go b/pkg/cloudcommon/db/virtualresource.go index 46488160aa..55a578da3b 100644 --- a/pkg/cloudcommon/db/virtualresource.go +++ b/pkg/cloudcommon/db/virtualresource.go @@ -130,6 +130,62 @@ func (manager *SVirtualResourceBaseManager) GetPropertyStatistics(ctx context.Co return result, nil } +func (manager *SVirtualResourceBaseManager) AllowGetPropertyProjectStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { + return IsAdminAllowGetSpec(userCred, manager, "project-statistics") +} + +func (manager *SVirtualResourceBaseManager) GetPropertyProjectStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) ([]apis.ProjectStatistic, error) { + im, ok := manager.GetVirtualObject().(IModelManager) + if !ok { + im = manager + } + + tenants := TenantCacheManager.GetTenantQuery().Equals("domain_id", userCred.GetProjectDomainId()).SubQuery() + + sq := im.Query().SubQuery() + + q := sq.Query( + sq.Field("tenant_id"), + sqlchemy.COUNT("count"), + ).GroupBy(sq.Field("tenant_id")) + + q.Join(tenants, sqlchemy.Equals(q.Field("tenant_id"), tenants.Field("id"))) + + q.AppendField(tenants.Field("id")) + q.AppendField(tenants.Field("name")) + + result := []apis.ProjectStatistic{} + return result, q.All(&result) +} + +func (manager *SVirtualResourceBaseManager) AllowGetPropertyDomainStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { + return IsAdminAllowGetSpec(userCred, manager, "domain-statistics") +} + +func (manager *SVirtualResourceBaseManager) GetPropertyDomainStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) ([]apis.ProjectStatistic, error) { + im, ok := manager.GetVirtualObject().(IModelManager) + if !ok { + im = manager + } + + domains := TenantCacheManager.GetDomainQuery().SubQuery() + + sq := im.Query().SubQuery() + + q := sq.Query( + sq.Field("domain_id"), + sqlchemy.COUNT("count"), + ).GroupBy(sq.Field("domain_id")) + + q.Join(domains, sqlchemy.Equals(q.Field("domain_id"), domains.Field("id"))) + + q.AppendField(domains.Field("id")) + q.AppendField(domains.Field("name")) + + result := []apis.ProjectStatistic{} + return result, q.All(&result) +} + func (manager *SVirtualResourceBaseManager) FilterByHiddenSystemAttributes(q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject, scope rbacutils.TRbacScope) *sqlchemy.SQuery { q = manager.SStatusStandaloneResourceBaseManager.FilterByHiddenSystemAttributes(q, userCred, query, scope) diff --git a/pkg/mcclient/options/base.go b/pkg/mcclient/options/base.go index 9ca70cf016..8b8ecdce26 100644 --- a/pkg/mcclient/options/base.go +++ b/pkg/mcclient/options/base.go @@ -471,3 +471,17 @@ type StatusStatisticsOptions struct { func (o StatusStatisticsOptions) Property() string { return "statistics" } + +type ProjectStatisticsOptions struct { +} + +func (o ProjectStatisticsOptions) Property() string { + return "project-statistics" +} + +type DomainStatisticsOptions struct { +} + +func (o DomainStatisticsOptions) Property() string { + return "domain-statistics" +} diff --git a/pkg/mcclient/options/servers.go b/pkg/mcclient/options/servers.go index 1c8ab2077b..e2713e7504 100644 --- a/pkg/mcclient/options/servers.go +++ b/pkg/mcclient/options/servers.go @@ -1148,3 +1148,13 @@ type ServerStatusStatisticsOptions struct { ServerListOptions StatusStatisticsOptions } + +type ServerProjectStatisticsOptions struct { + ServerListOptions + ProjectStatisticsOptions +} + +type ServerDomainStatisticsOptions struct { + ServerListOptions + DomainStatisticsOptions +}