quotas.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package models
  2. import (
  3. "errors"
  4. "github.com/grafana/grafana/pkg/setting"
  5. "reflect"
  6. "time"
  7. )
  8. var ErrInvalidQuotaTarget = errors.New("Invalid quota target")
  9. type Quota struct {
  10. Id int64
  11. OrgId int64
  12. UserId int64
  13. Target string
  14. Limit int64
  15. Created time.Time
  16. Updated time.Time
  17. }
  18. type QuotaScope struct {
  19. Name string
  20. Target string
  21. DefaultLimit int64
  22. }
  23. type OrgQuotaDTO struct {
  24. OrgId int64 `json:"org_id"`
  25. Target string `json:"target"`
  26. Limit int64 `json:"limit"`
  27. Used int64 `json:"used"`
  28. }
  29. type UserQuotaDTO struct {
  30. UserId int64 `json:"user_id"`
  31. Target string `json:"target"`
  32. Limit int64 `json:"limit"`
  33. Used int64 `json:"used"`
  34. }
  35. type GlobalQuotaDTO struct {
  36. Target string `json:"target"`
  37. Limit int64 `json:"limit"`
  38. Used int64 `json:"used"`
  39. }
  40. type GetOrgQuotaByTargetQuery struct {
  41. Target string
  42. OrgId int64
  43. Default int64
  44. Result *OrgQuotaDTO
  45. }
  46. type GetOrgQuotasQuery struct {
  47. OrgId int64
  48. Result []*OrgQuotaDTO
  49. }
  50. type GetUserQuotaByTargetQuery struct {
  51. Target string
  52. UserId int64
  53. Default int64
  54. Result *UserQuotaDTO
  55. }
  56. type GetUserQuotasQuery struct {
  57. UserId int64
  58. Result []*UserQuotaDTO
  59. }
  60. type GetGlobalQuotaByTargetQuery struct {
  61. Target string
  62. Default int64
  63. Result *GlobalQuotaDTO
  64. }
  65. type UpdateOrgQuotaCmd struct {
  66. Target string `json:"target"`
  67. Limit int64 `json:"limit"`
  68. OrgId int64 `json:"-"`
  69. }
  70. type UpdateUserQuotaCmd struct {
  71. Target string `json:"target"`
  72. Limit int64 `json:"limit"`
  73. UserId int64 `json:"-"`
  74. }
  75. func GetQuotaScopes(target string) ([]QuotaScope, error) {
  76. scopes := make([]QuotaScope, 0)
  77. switch target {
  78. case "user":
  79. scopes = append(scopes,
  80. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.User},
  81. QuotaScope{Name: "org", Target: "org_user", DefaultLimit: setting.Quota.Org.User},
  82. )
  83. return scopes, nil
  84. case "org":
  85. scopes = append(scopes,
  86. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Org},
  87. QuotaScope{Name: "user", Target: "org_user", DefaultLimit: setting.Quota.User.Org},
  88. )
  89. return scopes, nil
  90. case "dashboard":
  91. scopes = append(scopes,
  92. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Dashboard},
  93. QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.Dashboard},
  94. )
  95. return scopes, nil
  96. case "data_source":
  97. scopes = append(scopes,
  98. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.DataSource},
  99. QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.DataSource},
  100. )
  101. return scopes, nil
  102. case "api_key":
  103. scopes = append(scopes,
  104. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.ApiKey},
  105. QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.ApiKey},
  106. )
  107. return scopes, nil
  108. case "session":
  109. scopes = append(scopes,
  110. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Session},
  111. )
  112. return scopes, nil
  113. default:
  114. return scopes, ErrInvalidQuotaTarget
  115. }
  116. }
  117. func QuotaToMap(q interface{}) map[string]int64 {
  118. qMap := make(map[string]int64)
  119. typ := reflect.TypeOf(q)
  120. val := reflect.ValueOf(q)
  121. if typ.Kind() == reflect.Ptr {
  122. typ = typ.Elem()
  123. val = val.Elem()
  124. }
  125. for i := 0; i < typ.NumField(); i++ {
  126. field := typ.Field(i)
  127. name := field.Tag.Get("target")
  128. if name == "" {
  129. name = field.Name
  130. }
  131. if name == "-" {
  132. continue
  133. }
  134. value := val.Field(i)
  135. qMap[name] = value.Int()
  136. }
  137. return qMap
  138. }