Files
cloudpods/pkg/cloudcommon/db/project_resources.go
T
Yousong Zhou c2bca8ab39 Feature/yousong text (#7487)
* notify: log with context info

* region: guests: log with context info

* region: hosts: log with context info

* region: misc: remove redundant log

* region: skus: use NewInternalServerError instead of NewGeneralError

* appsrv: dispatcher: preserve text id

* appsrv: dispatcher: remove commented-out code

* cloudcommon: db_dispatcher: note errors that won't be translated

* cloudcommon: db: rbac: preserve text id

* cloudcommon: caller: preserve text id

* treewide: fix fmt string

* region: guest_actions: preserve text id

* region: guest_actions: fix fmt string

* region: usages: fix fmt string

* mcclient: hosts: remove redundant return value

* cloudcommon: fetch: preserve text id

* region: guest_template: preserve text id

* cloudcommon: db: db_dispatcher: preserve text id

* region: guest_actions: preserve text id

* httperrors: add funcs for wrap lang tag into context

* vendor: golang.org/x/text

* httperrors: error with context

* httperrors: HTTPError

* treewide: fix typo

* treewide: use en in source code

* locales: initial version
2020-08-25 15:26:31 +08:00

106 lines
3.4 KiB
Go

// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"context"
"database/sql"
"fmt"
"net/http"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient/auth"
)
func AddScopeResourceCountHandler(prefix string, app *appsrv.Application) {
prefix = fmt.Sprintf("%s/scope-resources", prefix)
app.AddHandler2("GET", prefix, auth.Authenticate(getAllScopeResourceCountsHandler), nil, "get_scope_resources", nil)
}
func getAllScopeResourceCountsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
cnt, err := getAllScopeResourceCounts()
if err != nil {
httperrors.GeneralServerError(ctx, w, err)
return
}
appsrv.SendJSON(w, jsonutils.Marshal(cnt))
}
func getAllScopeResourceCounts() (map[string][]SScopeResourceCount, error) {
ret := make(map[string][]SScopeResourceCount)
for _, manager := range globalTables {
if virtman, ok := manager.(IVirtualModelManager); ok {
resCnt, err := virtman.GetResourceCount()
if err != nil {
return nil, errors.Wrap(err, "getProjectResourceCount")
}
ret[virtman.KeywordPlural()] = resCnt
} else if domainMan, ok := manager.(IDomainLevelModelManager); ok {
resCnt, err := domainMan.GetResourceCount()
if err != nil {
return nil, errors.Wrap(err, "getDomainResourceCount")
}
ret[domainMan.KeywordPlural()] = resCnt
} else if userMan, ok := manager.(IUserModelManager); ok {
resCnt, err := userMan.GetResourceCount()
if err != nil {
return nil, errors.Wrap(err, "getUserResourceCount")
}
ret[userMan.KeywordPlural()] = resCnt
}
}
return ret, nil
}
type SScopeResourceCount struct {
TenantId string `json:"tenant_id"`
DomainId string `json:"domain_id"`
OwnerId string `json:"owner_id"`
ResCount int `json:"res_count"`
}
func (virtman *SVirtualResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
virts := virtman.GetIVirtualModelManager().Query()
return CalculateResourceCount(virts, "tenant_id")
}
func (domainman *SDomainLevelResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
virts := domainman.GetIDomainLevelModelManager().Query()
return CalculateResourceCount(virts, "domain_id")
}
func (userman *SUserResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
virts := userman.GetIUserModelManager().Query()
return CalculateResourceCount(virts, "owner_id")
}
func CalculateResourceCount(query *sqlchemy.SQuery, field string) ([]SScopeResourceCount, error) {
virts := query.SubQuery()
q := virts.Query(virts.Field(field), sqlchemy.COUNT("res_count"))
q = q.IsNotEmpty(field)
q = q.GroupBy(virts.Field(field))
cnts := make([]SScopeResourceCount, 0)
err := q.All(&cnts)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrap(err, "q.All")
}
return cnts, nil
}