Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
852f9bd2
Commit
852f9bd2
authored
Sep 11, 2015
by
woodsaj
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor quota settings
parent
555cbeff
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
59 additions
and
43 deletions
+59
-43
conf/defaults.ini
+2
-2
main.go
+0
-3
pkg/api/dashboard.go
+1
-1
pkg/api/quota.go
+11
-0
pkg/middleware/middleware.go
+18
-1
pkg/models/quotas.go
+1
-32
pkg/services/sqlstore/quota.go
+5
-4
pkg/setting/setting.go
+4
-0
pkg/setting/setting_quota.go
+17
-0
No files found.
conf/defaults.ini
View file @
852f9bd2
...
...
@@ -254,8 +254,8 @@ enabled = false
path
=
/var/lib/grafana/dashboards
[quota]
enabled
=
false
user
=
10
dashboard
=
100
data_source
=
10
endpoint
=
10
collector
=
10
main.go
View file @
852f9bd2
...
...
@@ -15,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/login"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/eventpublisher"
"github.com/grafana/grafana/pkg/services/notifications"
...
...
@@ -57,8 +56,6 @@ func main() {
eventpublisher
.
Init
()
plugins
.
Init
()
models
.
InitQuotaDefaults
()
if
err
:=
notifications
.
Init
();
err
!=
nil
{
log
.
Fatal
(
3
,
"Notification service failed to initialize"
,
err
)
}
...
...
pkg/api/dashboard.go
View file @
852f9bd2
...
...
@@ -88,7 +88,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
dash
:=
cmd
.
GetDashboardModel
()
if
dash
.
Id
==
0
{
limitReached
,
err
:=
m
.
QuotaReached
(
cmd
.
OrgId
,
m
.
QUOTA_DASHBOARD
)
limitReached
,
err
:=
m
iddleware
.
QuotaReached
(
cmd
.
OrgId
,
m
.
QUOTA_DASHBOARD
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"failed to get quota"
,
err
)
return
...
...
pkg/api/quota.go
View file @
852f9bd2
...
...
@@ -16,6 +16,17 @@ func GetOrgQuotas(c *middleware.Context) Response {
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
{
cmd
.
OrgId
=
c
.
ParamsInt64
(
":orgId"
)
cmd
.
Target
=
m
.
QuotaTarget
(
c
.
Params
(
":target"
))
...
...
pkg/middleware/middleware.go
View file @
852f9bd2
...
...
@@ -256,7 +256,7 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) {
func
LimitQuota
(
target
m
.
QuotaTarget
)
macaron
.
Handler
{
return
func
(
c
*
Context
)
{
limitReached
,
err
:=
m
.
QuotaReached
(
c
.
OrgId
,
target
)
limitReached
,
err
:=
QuotaReached
(
c
.
OrgId
,
target
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"failed to get quota"
,
err
)
return
...
...
@@ -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
}
pkg/models/quotas.go
View file @
852f9bd2
...
...
@@ -2,7 +2,6 @@ package models
import
(
"errors"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/setting"
"time"
)
...
...
@@ -13,31 +12,15 @@ const (
QUOTA_USER
QuotaTarget
=
"user"
//SQL table to query. ie. "select count(*) from user where org_id=?"
QUOTA_DATASOURCE
QuotaTarget
=
"data_source"
QUOTA_DASHBOARD
QuotaTarget
=
"dashboard"
QUOTA_ENDPOINT
QuotaTarget
=
"endpoint"
QUOTA_COLLECTOR
QuotaTarget
=
"collector"
)
var
ErrInvalidQuotaTarget
=
errors
.
New
(
"Invalid quota target"
)
func
(
q
QuotaTarget
)
IsValid
()
bool
{
_
,
ok
:=
DefaultQuotas
[
q
]
_
,
ok
:=
setting
.
Quota
.
Default
[
string
(
q
)
]
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
{
Id
int64
OrgId
int64
...
...
@@ -70,17 +53,3 @@ type UpdateQuotaCmd struct {
Limit
int64
`json:"limit"`
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
}
pkg/services/sqlstore/quota.go
View file @
852f9bd2
...
...
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/grafana/grafana/pkg/bus"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
func
init
()
{
...
...
@@ -25,7 +26,7 @@ func GetQuotaByTarget(query *m.GetQuotaByTargetQuery) error {
if
err
!=
nil
{
return
err
}
else
if
has
==
false
{
quota
.
Limit
=
m
.
DefaultQuotas
[
query
.
Target
]
quota
.
Limit
=
setting
.
Quota
.
Default
[
string
(
query
.
Target
)
]
}
//get quota used.
...
...
@@ -57,11 +58,11 @@ func GetQuotas(query *m.GetQuotasQuery) error {
seenTargets
[
q
.
Target
]
=
true
}
for
t
,
v
:=
range
m
.
DefaultQuotas
{
if
_
,
ok
:=
seenTargets
[
t
];
!
ok
{
for
t
,
v
:=
range
setting
.
Quota
.
Default
{
if
_
,
ok
:=
seenTargets
[
m
.
QuotaTarget
(
t
)
];
!
ok
{
quotas
=
append
(
quotas
,
&
m
.
Quota
{
OrgId
:
query
.
OrgId
,
Target
:
t
,
Target
:
m
.
QuotaTarget
(
t
)
,
Limit
:
v
,
})
}
...
...
pkg/setting/setting.go
View file @
852f9bd2
...
...
@@ -126,6 +126,9 @@ var (
// SMTP email settings
Smtp
SmtpSettings
// QUOTA
Quota
QuotaSettings
)
type
CommandLineArgs
struct
{
...
...
@@ -434,6 +437,7 @@ func NewConfigContext(args *CommandLineArgs) {
readSessionConfig
()
readSmtpSettings
()
readQuotaSettings
()
if
VerifyEmailEnabled
&&
!
Smtp
.
Enabled
{
log
.
Warn
(
"require_email_validation is enabled but smpt is disabled"
)
...
...
pkg/setting/setting_quota.go
0 → 100644
View file @
852f9bd2
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment