setting_quota.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package setting
  2. import (
  3. "reflect"
  4. )
  5. type OrgQuota struct {
  6. User int64 `target:"org_user"`
  7. DataSource int64 `target:"data_source"`
  8. Dashboard int64 `target:"dashboard"`
  9. ApiKey int64 `target:"api_key"`
  10. }
  11. type UserQuota struct {
  12. Org int64 `target:"org_user"`
  13. }
  14. type GlobalQuota struct {
  15. Org int64 `target:"org"`
  16. User int64 `target:"user"`
  17. DataSource int64 `target:"data_source"`
  18. Dashboard int64 `target:"dashboard"`
  19. ApiKey int64 `target:"api_key"`
  20. Session int64 `target:"-"`
  21. }
  22. func (q *OrgQuota) ToMap() map[string]int64 {
  23. return quotaToMap(*q)
  24. }
  25. func (q *UserQuota) ToMap() map[string]int64 {
  26. return quotaToMap(*q)
  27. }
  28. func (q *GlobalQuota) ToMap() map[string]int64 {
  29. return quotaToMap(*q)
  30. }
  31. func quotaToMap(q interface{}) map[string]int64 {
  32. qMap := make(map[string]int64)
  33. typ := reflect.TypeOf(q)
  34. val := reflect.ValueOf(q)
  35. for i := 0; i < typ.NumField(); i++ {
  36. field := typ.Field(i)
  37. name := field.Tag.Get("target")
  38. if name == "" {
  39. name = field.Name
  40. }
  41. if name == "-" {
  42. continue
  43. }
  44. value := val.Field(i)
  45. qMap[name] = value.Int()
  46. }
  47. return qMap
  48. }
  49. type QuotaSettings struct {
  50. Enabled bool
  51. Org *OrgQuota
  52. User *UserQuota
  53. Global *GlobalQuota
  54. }
  55. func (cfg *Cfg) readQuotaSettings() {
  56. // set global defaults.
  57. quota := cfg.Raw.Section("quota")
  58. Quota.Enabled = quota.Key("enabled").MustBool(false)
  59. // per ORG Limits
  60. Quota.Org = &OrgQuota{
  61. User: quota.Key("org_user").MustInt64(10),
  62. DataSource: quota.Key("org_data_source").MustInt64(10),
  63. Dashboard: quota.Key("org_dashboard").MustInt64(10),
  64. ApiKey: quota.Key("org_api_key").MustInt64(10),
  65. }
  66. // per User limits
  67. Quota.User = &UserQuota{
  68. Org: quota.Key("user_org").MustInt64(10),
  69. }
  70. // Global Limits
  71. Quota.Global = &GlobalQuota{
  72. User: quota.Key("global_user").MustInt64(-1),
  73. Org: quota.Key("global_org").MustInt64(-1),
  74. DataSource: quota.Key("global_data_source").MustInt64(-1),
  75. Dashboard: quota.Key("global_dashboard").MustInt64(-1),
  76. ApiKey: quota.Key("global_api_key").MustInt64(-1),
  77. Session: quota.Key("global_session").MustInt64(-1),
  78. }
  79. }