project_resources.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 models
  15. import (
  16. "database/sql"
  17. "time"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/sqlchemy"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. )
  22. type SScopeResourceManager struct {
  23. db.SModelBaseManager
  24. }
  25. var ScopeResourceManager *SScopeResourceManager
  26. func init() {
  27. ScopeResourceManager = &SScopeResourceManager{
  28. SModelBaseManager: db.NewModelBaseManager(
  29. SScopeResource{},
  30. "scope_resource_tbl",
  31. "scope_resource",
  32. "scope_resources",
  33. ),
  34. }
  35. ScopeResourceManager.SetVirtualObject(ScopeResourceManager)
  36. }
  37. type SScopeResource struct {
  38. db.SModelBase
  39. DomainId string `width:"64" charset:"ascii" primary:"true"`
  40. ProjectId string `width:"64" charset:"ascii" primary:"true"`
  41. OwnerId string `width:"64" charset:"ascii" primary:"true"`
  42. RegionId string `width:"32" charset:"ascii" primary:"true"`
  43. ServiceId string `width:"32" charset:"ascii" primary:"true"`
  44. Resource string `width:"32" charset:"ascii" primary:"true"`
  45. Count int
  46. UpdatedAt time.Time `nullable:"true" updated_at:"true"`
  47. }
  48. type sScopeResourceCount struct {
  49. Resource string
  50. ResCount int
  51. ProjectId string
  52. LastUpdate time.Time
  53. }
  54. func (manager *SScopeResourceManager) getScopeResource(domainId, projId, ownerId string) (map[string]int, time.Time, error) {
  55. resources := manager.Query().SubQuery()
  56. q := resources.Query(
  57. resources.Field("resource"),
  58. sqlchemy.SUM("res_count", resources.Field("count")),
  59. sqlchemy.MAX("last_update", resources.Field("updated_at")),
  60. )
  61. if len(domainId) > 0 {
  62. q = q.Filter(sqlchemy.Equals(resources.Field("domain_id"), domainId))
  63. }
  64. if len(projId) > 0 {
  65. q = q.Filter(sqlchemy.Equals(resources.Field("project_id"), projId))
  66. }
  67. if len(ownerId) > 0 {
  68. q = q.Filter(sqlchemy.Equals(resources.Field("owner_id"), ownerId))
  69. }
  70. q = q.GroupBy(resources.Field("resource"))
  71. resCnts := make([]sScopeResourceCount, 0)
  72. err := q.All(&resCnts)
  73. if err != nil && err != sql.ErrNoRows {
  74. return nil, time.Time{}, errors.Wrap(err, "query.All")
  75. }
  76. ret := make(map[string]int)
  77. lastUpdate := time.Time{}
  78. for i := range resCnts {
  79. if resCnts[i].ResCount == 0 {
  80. continue
  81. }
  82. ret[resCnts[i].Resource] = resCnts[i].ResCount
  83. if lastUpdate.IsZero() || lastUpdate.Before(resCnts[i].LastUpdate) {
  84. lastUpdate = resCnts[i].LastUpdate
  85. }
  86. }
  87. return ret, lastUpdate, nil
  88. }
  89. func (manager *SScopeResourceManager) FetchProjectsScopeResources(projIds []string) (map[string]map[string]int, map[string]time.Time, error) {
  90. resources := manager.Query().SubQuery()
  91. q := resources.Query(
  92. resources.Field("resource"),
  93. resources.Field("project_id"),
  94. sqlchemy.SUM("res_count", resources.Field("count")),
  95. sqlchemy.MAX("last_update", resources.Field("updated_at")),
  96. ).Filter(sqlchemy.In(resources.Field("project_id"), projIds))
  97. q = q.GroupBy(resources.Field("resource"))
  98. resCnts := make([]sScopeResourceCount, 0)
  99. err := q.All(&resCnts)
  100. if err != nil && err != sql.ErrNoRows {
  101. return nil, nil, errors.Wrap(err, "query.All")
  102. }
  103. ret := make(map[string]map[string]int)
  104. lastUpdate := map[string]time.Time{}
  105. for i := range resCnts {
  106. _, ok := ret[resCnts[i].ProjectId]
  107. if !ok {
  108. ret[resCnts[i].ProjectId] = map[string]int{}
  109. lastUpdate[resCnts[i].ProjectId] = resCnts[i].LastUpdate
  110. }
  111. if resCnts[i].ResCount == 0 {
  112. continue
  113. }
  114. last := lastUpdate[resCnts[i].ProjectId]
  115. ret[resCnts[i].ProjectId][resCnts[i].Resource] = resCnts[i].ResCount
  116. if last.IsZero() || last.Before(resCnts[i].LastUpdate) {
  117. lastUpdate[resCnts[i].ProjectId] = resCnts[i].LastUpdate
  118. }
  119. }
  120. return ret, lastUpdate, nil
  121. }