mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
move toMap function to be a method on the quota structs
This commit is contained in:
parent
b7de847236
commit
86ed85aa6e
@ -3,7 +3,6 @@ package models
|
||||
import (
|
||||
"errors"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -129,26 +128,3 @@ func GetQuotaScopes(target string) ([]QuotaScope, error) {
|
||||
return scopes, ErrInvalidQuotaTarget
|
||||
}
|
||||
}
|
||||
|
||||
func QuotaToMap(q interface{}) map[string]int64 {
|
||||
qMap := make(map[string]int64)
|
||||
typ := reflect.TypeOf(q)
|
||||
val := reflect.ValueOf(q)
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
val = val.Elem()
|
||||
}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
name := field.Tag.Get("target")
|
||||
if name == "" {
|
||||
name = field.Name
|
||||
}
|
||||
if name == "-" {
|
||||
continue
|
||||
}
|
||||
value := val.Field(i)
|
||||
qMap[name] = value.Int()
|
||||
}
|
||||
return qMap
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func GetOrgQuotas(query *m.GetOrgQuotasQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
defaultQuotas := m.QuotaToMap(setting.Quota.Org)
|
||||
defaultQuotas := setting.Quota.Org.ToMap()
|
||||
|
||||
seenTargets := make(map[string]bool)
|
||||
for _, q := range quotas {
|
||||
@ -157,7 +157,7 @@ func GetUserQuotas(query *m.GetUserQuotasQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
defaultQuotas := m.QuotaToMap(setting.Quota.User)
|
||||
defaultQuotas := setting.Quota.User.ToMap()
|
||||
|
||||
seenTargets := make(map[string]bool)
|
||||
for _, q := range quotas {
|
||||
|
@ -1,5 +1,9 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type OrgQuota struct {
|
||||
User int64 `target:"org_user"`
|
||||
DataSource int64 `target:"data_source"`
|
||||
@ -20,6 +24,38 @@ type GlobalQuota struct {
|
||||
Session int64 `target:"-"`
|
||||
}
|
||||
|
||||
func (q *OrgQuota) ToMap() map[string]int64 {
|
||||
return quotaToMap(*q)
|
||||
}
|
||||
|
||||
func (q *UserQuota) ToMap() map[string]int64 {
|
||||
return quotaToMap(*q)
|
||||
}
|
||||
|
||||
func (q *GlobalQuota) ToMap() map[string]int64 {
|
||||
return quotaToMap(*q)
|
||||
}
|
||||
|
||||
func quotaToMap(q interface{}) map[string]int64 {
|
||||
qMap := make(map[string]int64)
|
||||
typ := reflect.TypeOf(q)
|
||||
val := reflect.ValueOf(q)
|
||||
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
name := field.Tag.Get("target")
|
||||
if name == "" {
|
||||
name = field.Name
|
||||
}
|
||||
if name == "-" {
|
||||
continue
|
||||
}
|
||||
value := val.Field(i)
|
||||
qMap[name] = value.Int()
|
||||
}
|
||||
return qMap
|
||||
}
|
||||
|
||||
type QuotaSettings struct {
|
||||
Enabled bool
|
||||
Org *OrgQuota
|
||||
|
Loading…
Reference in New Issue
Block a user