Commit 852f9bd2 by woodsaj

refactor quota settings

parent 555cbeff
...@@ -254,8 +254,8 @@ enabled = false ...@@ -254,8 +254,8 @@ enabled = false
path = /var/lib/grafana/dashboards path = /var/lib/grafana/dashboards
[quota] [quota]
enabled = false
user = 10 user = 10
dashboard = 100 dashboard = 100
data_source = 10 data_source = 10
endpoint = 10
collector = 10
...@@ -15,7 +15,6 @@ import ( ...@@ -15,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/login" "github.com/grafana/grafana/pkg/login"
"github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/eventpublisher" "github.com/grafana/grafana/pkg/services/eventpublisher"
"github.com/grafana/grafana/pkg/services/notifications" "github.com/grafana/grafana/pkg/services/notifications"
...@@ -57,8 +56,6 @@ func main() { ...@@ -57,8 +56,6 @@ func main() {
eventpublisher.Init() eventpublisher.Init()
plugins.Init() plugins.Init()
models.InitQuotaDefaults()
if err := notifications.Init(); err != nil { if err := notifications.Init(); err != nil {
log.Fatal(3, "Notification service failed to initialize", err) log.Fatal(3, "Notification service failed to initialize", err)
} }
......
...@@ -88,7 +88,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { ...@@ -88,7 +88,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
dash := cmd.GetDashboardModel() dash := cmd.GetDashboardModel()
if dash.Id == 0 { if dash.Id == 0 {
limitReached, err := m.QuotaReached(cmd.OrgId, m.QUOTA_DASHBOARD) limitReached, err := middleware.QuotaReached(cmd.OrgId, m.QUOTA_DASHBOARD)
if err != nil { if err != nil {
c.JsonApiErr(500, "failed to get quota", err) c.JsonApiErr(500, "failed to get quota", err)
return return
......
...@@ -16,6 +16,17 @@ func GetOrgQuotas(c *middleware.Context) Response { ...@@ -16,6 +16,17 @@ func GetOrgQuotas(c *middleware.Context) Response {
return Json(200, query.Result) return Json(200, query.Result)
} }
// allow users to query the quotas of their own org.
func GetQuotas(c *middleware.Context) Response {
query := m.GetQuotasQuery{OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
return ApiError(500, "Failed to get quotas", err)
}
return Json(200, query.Result)
}
func UpdateOrgQuota(c *middleware.Context, cmd m.UpdateQuotaCmd) Response { func UpdateOrgQuota(c *middleware.Context, cmd m.UpdateQuotaCmd) Response {
cmd.OrgId = c.ParamsInt64(":orgId") cmd.OrgId = c.ParamsInt64(":orgId")
cmd.Target = m.QuotaTarget(c.Params(":target")) cmd.Target = m.QuotaTarget(c.Params(":target"))
......
...@@ -256,7 +256,7 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) { ...@@ -256,7 +256,7 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) {
func LimitQuota(target m.QuotaTarget) macaron.Handler { func LimitQuota(target m.QuotaTarget) macaron.Handler {
return func(c *Context) { return func(c *Context) {
limitReached, err := m.QuotaReached(c.OrgId, target) limitReached, err := QuotaReached(c.OrgId, target)
if err != nil { if err != nil {
c.JsonApiErr(500, "failed to get quota", err) c.JsonApiErr(500, "failed to get quota", err)
return return
...@@ -267,3 +267,20 @@ func LimitQuota(target m.QuotaTarget) macaron.Handler { ...@@ -267,3 +267,20 @@ func LimitQuota(target m.QuotaTarget) macaron.Handler {
} }
} }
} }
func QuotaReached(org_id int64, target m.QuotaTarget) (bool, error) {
if !setting.Quota.Enabled {
return false, nil
}
if !target.IsValid() {
return true, m.ErrInvalidQuotaTarget
}
query := m.GetQuotaByTargetQuery{OrgId: org_id, Target: target}
if err := bus.Dispatch(&query); err != nil {
return true, err
}
if query.Result.Used >= query.Result.Limit {
return true, nil
}
return false, nil
}
...@@ -2,7 +2,6 @@ package models ...@@ -2,7 +2,6 @@ package models
import ( import (
"errors" "errors"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"time" "time"
) )
...@@ -13,31 +12,15 @@ const ( ...@@ -13,31 +12,15 @@ const (
QUOTA_USER QuotaTarget = "user" //SQL table to query. ie. "select count(*) from user where org_id=?" QUOTA_USER QuotaTarget = "user" //SQL table to query. ie. "select count(*) from user where org_id=?"
QUOTA_DATASOURCE QuotaTarget = "data_source" QUOTA_DATASOURCE QuotaTarget = "data_source"
QUOTA_DASHBOARD QuotaTarget = "dashboard" QUOTA_DASHBOARD QuotaTarget = "dashboard"
QUOTA_ENDPOINT QuotaTarget = "endpoint"
QUOTA_COLLECTOR QuotaTarget = "collector"
) )
var ErrInvalidQuotaTarget = errors.New("Invalid quota target") var ErrInvalidQuotaTarget = errors.New("Invalid quota target")
func (q QuotaTarget) IsValid() bool { func (q QuotaTarget) IsValid() bool {
_, ok := DefaultQuotas[q] _, ok := setting.Quota.Default[string(q)]
return ok return ok
} }
// defaults are set from settings package.
var DefaultQuotas map[QuotaTarget]int64
func InitQuotaDefaults() {
// set global defaults.
DefaultQuotas = make(map[QuotaTarget]int64)
quota := setting.Cfg.Section("quota")
DefaultQuotas[QUOTA_USER] = quota.Key("user").MustInt64(10)
DefaultQuotas[QUOTA_DATASOURCE] = quota.Key("data_source").MustInt64(10)
DefaultQuotas[QUOTA_DASHBOARD] = quota.Key("dashboard").MustInt64(10)
DefaultQuotas[QUOTA_ENDPOINT] = quota.Key("endpoint").MustInt64(10)
DefaultQuotas[QUOTA_COLLECTOR] = quota.Key("collector").MustInt64(10)
}
type Quota struct { type Quota struct {
Id int64 Id int64
OrgId int64 OrgId int64
...@@ -70,17 +53,3 @@ type UpdateQuotaCmd struct { ...@@ -70,17 +53,3 @@ type UpdateQuotaCmd struct {
Limit int64 `json:"limit"` Limit int64 `json:"limit"`
OrgId int64 `json:"-"` OrgId int64 `json:"-"`
} }
func QuotaReached(org_id int64, target QuotaTarget) (bool, error) {
if !target.IsValid() {
return true, ErrInvalidQuotaTarget
}
query := GetQuotaByTargetQuery{OrgId: org_id, Target: target}
if err := bus.Dispatch(&query); err != nil {
return true, err
}
if query.Result.Used >= query.Result.Limit {
return true, nil
}
return false, nil
}
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
) )
func init() { func init() {
...@@ -25,7 +26,7 @@ func GetQuotaByTarget(query *m.GetQuotaByTargetQuery) error { ...@@ -25,7 +26,7 @@ func GetQuotaByTarget(query *m.GetQuotaByTargetQuery) error {
if err != nil { if err != nil {
return err return err
} else if has == false { } else if has == false {
quota.Limit = m.DefaultQuotas[query.Target] quota.Limit = setting.Quota.Default[string(query.Target)]
} }
//get quota used. //get quota used.
...@@ -57,11 +58,11 @@ func GetQuotas(query *m.GetQuotasQuery) error { ...@@ -57,11 +58,11 @@ func GetQuotas(query *m.GetQuotasQuery) error {
seenTargets[q.Target] = true seenTargets[q.Target] = true
} }
for t, v := range m.DefaultQuotas { for t, v := range setting.Quota.Default {
if _, ok := seenTargets[t]; !ok { if _, ok := seenTargets[m.QuotaTarget(t)]; !ok {
quotas = append(quotas, &m.Quota{ quotas = append(quotas, &m.Quota{
OrgId: query.OrgId, OrgId: query.OrgId,
Target: t, Target: m.QuotaTarget(t),
Limit: v, Limit: v,
}) })
} }
......
...@@ -126,6 +126,9 @@ var ( ...@@ -126,6 +126,9 @@ var (
// SMTP email settings // SMTP email settings
Smtp SmtpSettings Smtp SmtpSettings
// QUOTA
Quota QuotaSettings
) )
type CommandLineArgs struct { type CommandLineArgs struct {
...@@ -434,6 +437,7 @@ func NewConfigContext(args *CommandLineArgs) { ...@@ -434,6 +437,7 @@ func NewConfigContext(args *CommandLineArgs) {
readSessionConfig() readSessionConfig()
readSmtpSettings() readSmtpSettings()
readQuotaSettings()
if VerifyEmailEnabled && !Smtp.Enabled { if VerifyEmailEnabled && !Smtp.Enabled {
log.Warn("require_email_validation is enabled but smpt is disabled") log.Warn("require_email_validation is enabled but smpt is disabled")
......
package setting
type QuotaSettings struct {
Enabled bool
Default map[string]int64
}
func readQuotaSettings() {
// set global defaults.
DefaultQuotas := make(map[string]int64)
quota := Cfg.Section("quota")
Quota.Enabled = quota.Key("enabled").MustBool(false)
DefaultQuotas["user"] = quota.Key("user").MustInt64(10)
DefaultQuotas["data_source"] = quota.Key("data_source").MustInt64(10)
DefaultQuotas["dashboard"] = quota.Key("dashboard").MustInt64(10)
Quota.Default = DefaultQuotas
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment