quotas.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package models
  2. import (
  3. "errors"
  4. "github.com/grafana/grafana/pkg/setting"
  5. "time"
  6. )
  7. var ErrInvalidQuotaTarget = errors.New("Invalid quota target")
  8. type Quota struct {
  9. Id int64
  10. OrgId int64
  11. UserId int64
  12. Target string
  13. Limit int64
  14. Created time.Time
  15. Updated time.Time
  16. }
  17. type QuotaScope struct {
  18. Name string
  19. Target string
  20. DefaultLimit int64
  21. }
  22. type OrgQuotaDTO struct {
  23. OrgId int64 `json:"org_id"`
  24. Target string `json:"target"`
  25. Limit int64 `json:"limit"`
  26. Used int64 `json:"used"`
  27. }
  28. type UserQuotaDTO struct {
  29. UserId int64 `json:"user_id"`
  30. Target string `json:"target"`
  31. Limit int64 `json:"limit"`
  32. Used int64 `json:"used"`
  33. }
  34. type GlobalQuotaDTO struct {
  35. Target string `json:"target"`
  36. Limit int64 `json:"limit"`
  37. Used int64 `json:"used"`
  38. }
  39. type GetOrgQuotaByTargetQuery struct {
  40. Target string
  41. OrgId int64
  42. Default int64
  43. Result *OrgQuotaDTO
  44. }
  45. type GetOrgQuotasQuery struct {
  46. OrgId int64
  47. Result []*OrgQuotaDTO
  48. }
  49. type GetUserQuotaByTargetQuery struct {
  50. Target string
  51. UserId int64
  52. Default int64
  53. Result *UserQuotaDTO
  54. }
  55. type GetUserQuotasQuery struct {
  56. UserId int64
  57. Result []*UserQuotaDTO
  58. }
  59. type GetGlobalQuotaByTargetQuery struct {
  60. Target string
  61. Default int64
  62. Result *GlobalQuotaDTO
  63. }
  64. type UpdateOrgQuotaCmd struct {
  65. Target string `json:"target"`
  66. Limit int64 `json:"limit"`
  67. OrgId int64 `json:"-"`
  68. }
  69. type UpdateUserQuotaCmd struct {
  70. Target string `json:"target"`
  71. Limit int64 `json:"limit"`
  72. UserId int64 `json:"-"`
  73. }
  74. func GetQuotaScopes(target string) ([]QuotaScope, error) {
  75. scopes := make([]QuotaScope, 0)
  76. switch target {
  77. case "user":
  78. scopes = append(scopes,
  79. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.User},
  80. QuotaScope{Name: "org", Target: "org_user", DefaultLimit: setting.Quota.Org.User},
  81. )
  82. return scopes, nil
  83. case "org":
  84. scopes = append(scopes,
  85. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Org},
  86. QuotaScope{Name: "user", Target: "org_user", DefaultLimit: setting.Quota.User.Org},
  87. )
  88. return scopes, nil
  89. case "dashboard":
  90. scopes = append(scopes,
  91. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Dashboard},
  92. QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.Dashboard},
  93. )
  94. return scopes, nil
  95. case "data_source":
  96. scopes = append(scopes,
  97. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.DataSource},
  98. QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.DataSource},
  99. )
  100. return scopes, nil
  101. case "api_key":
  102. scopes = append(scopes,
  103. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.ApiKey},
  104. QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.ApiKey},
  105. )
  106. return scopes, nil
  107. case "session":
  108. scopes = append(scopes,
  109. QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Session},
  110. )
  111. return scopes, nil
  112. default:
  113. return scopes, ErrInvalidQuotaTarget
  114. }
  115. }