project_resources.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package db
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "net/http"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/sqlchemy"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. )
  27. func AddScopeResourceCountHandler(prefix string, app *appsrv.Application) {
  28. prefix = fmt.Sprintf("%s/scope-resources", prefix)
  29. app.AddHandler2("GET", prefix, auth.Authenticate(getAllScopeResourceCountsHandler), nil, "get_scope_resources", nil)
  30. }
  31. func getAllScopeResourceCountsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  32. cnt, err := getAllScopeResourceCounts()
  33. if err != nil {
  34. httperrors.GeneralServerError(ctx, w, err)
  35. return
  36. }
  37. appsrv.SendJSON(w, jsonutils.Marshal(cnt))
  38. }
  39. func getAllScopeResourceCounts() (map[string][]SScopeResourceCount, error) {
  40. ret := make(map[string][]SScopeResourceCount)
  41. for _, manager := range globalTables {
  42. if cntMan, ok := manager.(IResourceCountManager); ok {
  43. resCnt, err := cntMan.GetResourceCount()
  44. if err != nil {
  45. return nil, errors.Wrap(err, "cntMan.GetResourceCount")
  46. }
  47. ret[cntMan.KeywordPlural()] = resCnt
  48. }
  49. }
  50. return ret, nil
  51. }
  52. type SScopeResourceCount struct {
  53. TenantId string `json:"tenant_id"`
  54. DomainId string `json:"domain_id"`
  55. OwnerId string `json:"owner_id"`
  56. ResCount int `json:"res_count"`
  57. }
  58. type IResourceCountManager interface {
  59. GetResourceCount() ([]SScopeResourceCount, error)
  60. KeywordPlural() string
  61. }
  62. func (virtman *SVirtualResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
  63. virts := virtman.GetIVirtualModelManager().Query()
  64. return CalculateResourceCount(virts, "tenant_id")
  65. }
  66. func (domainman *SDomainLevelResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
  67. virts := domainman.GetIDomainLevelModelManager().Query()
  68. return CalculateResourceCount(virts, "domain_id")
  69. }
  70. func (userman *SUserResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
  71. virts := userman.GetIUserModelManager().Query()
  72. return CalculateResourceCount(virts, "owner_id")
  73. }
  74. func CalculateResourceCount(query *sqlchemy.SQuery, field string) ([]SScopeResourceCount, error) {
  75. virts := query.SubQuery()
  76. q := virts.Query(virts.Field(field), sqlchemy.COUNT("res_count"))
  77. q = q.IsNotEmpty(field)
  78. q = q.GroupBy(virts.Field(field))
  79. cnts := make([]SScopeResourceCount, 0)
  80. err := q.All(&cnts)
  81. if err != nil && err != sql.ErrNoRows {
  82. return nil, errors.Wrap(err, "q.All")
  83. }
  84. return cnts, nil
  85. }