Merge pull request #12293 from ioito/hotfix/qx-project-domain-statistic

fix(region): add project and domain statistic
This commit is contained in:
Zexi Li
2021-09-26 09:57:45 +08:00
committed by GitHub
5 changed files with 88 additions and 0 deletions
+2
View File
@@ -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:"-"`
+6
View File
@@ -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
}
+56
View File
@@ -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)
+14
View File
@@ -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"
}
+10
View File
@@ -1148,3 +1148,13 @@ type ServerStatusStatisticsOptions struct {
ServerListOptions
StatusStatisticsOptions
}
type ServerProjectStatisticsOptions struct {
ServerListOptions
ProjectStatisticsOptions
}
type ServerDomainStatisticsOptions struct {
ServerListOptions
DomainStatisticsOptions
}