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
bbd6adab
Unverified
Commit
bbd6adab
authored
Mar 07, 2018
by
Dan Cech
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move quota to dedicated service
parent
c0ecdee3
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
95 additions
and
88 deletions
+95
-88
pkg/api/dashboard.go
+2
-2
pkg/api/login_oauth.go
+2
-2
pkg/middleware/quota.go
+4
-84
pkg/services/quota/quota.go
+87
-0
No files found.
pkg/api/dashboard.go
View file @
bbd6adab
...
@@ -14,10 +14,10 @@ import (
...
@@ -14,10 +14,10 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/quota"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util"
)
)
...
@@ -202,7 +202,7 @@ func PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand) Response {
...
@@ -202,7 +202,7 @@ func PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand) Response {
dash
:=
cmd
.
GetDashboardModel
()
dash
:=
cmd
.
GetDashboardModel
()
if
dash
.
Id
==
0
&&
dash
.
Uid
==
""
{
if
dash
.
Id
==
0
&&
dash
.
Uid
==
""
{
limitReached
,
err
:=
middleware
.
QuotaReached
(
c
,
"dashboard"
)
limitReached
,
err
:=
quota
.
QuotaReached
(
c
,
"dashboard"
)
if
err
!=
nil
{
if
err
!=
nil
{
return
ApiError
(
500
,
"failed to get quota"
,
err
)
return
ApiError
(
500
,
"failed to get quota"
,
err
)
}
}
...
...
pkg/api/login_oauth.go
View file @
bbd6adab
...
@@ -17,8 +17,8 @@ import (
...
@@ -17,8 +17,8 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/quota"
"github.com/grafana/grafana/pkg/services/session"
"github.com/grafana/grafana/pkg/services/session"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/social"
"github.com/grafana/grafana/pkg/social"
...
@@ -168,7 +168,7 @@ func OAuthLogin(ctx *m.ReqContext) {
...
@@ -168,7 +168,7 @@ func OAuthLogin(ctx *m.ReqContext) {
redirectWithError
(
ctx
,
ErrSignUpNotAllowed
)
redirectWithError
(
ctx
,
ErrSignUpNotAllowed
)
return
return
}
}
limitReached
,
err
:=
middleware
.
QuotaReached
(
ctx
,
"user"
)
limitReached
,
err
:=
quota
.
QuotaReached
(
ctx
,
"user"
)
if
err
!=
nil
{
if
err
!=
nil
{
ctx
.
Handle
(
500
,
"Failed to get user quota"
,
err
)
ctx
.
Handle
(
500
,
"Failed to get user quota"
,
err
)
return
return
...
...
pkg/middleware/quota.go
View file @
bbd6adab
...
@@ -3,16 +3,15 @@ package middleware
...
@@ -3,16 +3,15 @@ package middleware
import
(
import
(
"fmt"
"fmt"
"github.com/grafana/grafana/pkg/bus"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/session"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
"gopkg.in/macaron.v1"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/quota"
)
)
func
Quota
(
target
string
)
macaron
.
Handler
{
func
Quota
(
target
string
)
macaron
.
Handler
{
return
func
(
c
*
m
.
ReqContext
)
{
return
func
(
c
*
m
.
ReqContext
)
{
limitReached
,
err
:=
QuotaReached
(
c
,
target
)
limitReached
,
err
:=
quota
.
QuotaReached
(
c
,
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
...
@@ -23,82 +22,3 @@ func Quota(target string) macaron.Handler {
...
@@ -23,82 +22,3 @@ func Quota(target string) macaron.Handler {
}
}
}
}
}
}
func
QuotaReached
(
c
*
m
.
ReqContext
,
target
string
)
(
bool
,
error
)
{
if
!
setting
.
Quota
.
Enabled
{
return
false
,
nil
}
// get the list of scopes that this target is valid for. Org, User, Global
scopes
,
err
:=
m
.
GetQuotaScopes
(
target
)
if
err
!=
nil
{
return
false
,
err
}
for
_
,
scope
:=
range
scopes
{
c
.
Logger
.
Debug
(
"Checking quota"
,
"target"
,
target
,
"scope"
,
scope
)
switch
scope
.
Name
{
case
"global"
:
if
scope
.
DefaultLimit
<
0
{
continue
}
if
scope
.
DefaultLimit
==
0
{
return
true
,
nil
}
if
target
==
"session"
{
usedSessions
:=
session
.
GetSessionCount
()
if
int64
(
usedSessions
)
>
scope
.
DefaultLimit
{
c
.
Logger
.
Debug
(
"Sessions limit reached"
,
"active"
,
usedSessions
,
"limit"
,
scope
.
DefaultLimit
)
return
true
,
nil
}
continue
}
query
:=
m
.
GetGlobalQuotaByTargetQuery
{
Target
:
scope
.
Target
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
true
,
err
}
if
query
.
Result
.
Used
>=
scope
.
DefaultLimit
{
return
true
,
nil
}
case
"org"
:
if
!
c
.
IsSignedIn
{
continue
}
query
:=
m
.
GetOrgQuotaByTargetQuery
{
OrgId
:
c
.
OrgId
,
Target
:
scope
.
Target
,
Default
:
scope
.
DefaultLimit
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
true
,
err
}
if
query
.
Result
.
Limit
<
0
{
continue
}
if
query
.
Result
.
Limit
==
0
{
return
true
,
nil
}
if
query
.
Result
.
Used
>=
query
.
Result
.
Limit
{
return
true
,
nil
}
case
"user"
:
if
!
c
.
IsSignedIn
||
c
.
UserId
==
0
{
continue
}
query
:=
m
.
GetUserQuotaByTargetQuery
{
UserId
:
c
.
UserId
,
Target
:
scope
.
Target
,
Default
:
scope
.
DefaultLimit
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
true
,
err
}
if
query
.
Result
.
Limit
<
0
{
continue
}
if
query
.
Result
.
Limit
==
0
{
return
true
,
nil
}
if
query
.
Result
.
Used
>=
query
.
Result
.
Limit
{
return
true
,
nil
}
}
}
return
false
,
nil
}
pkg/services/quota/quota.go
0 → 100644
View file @
bbd6adab
package
quota
import
(
"github.com/grafana/grafana/pkg/bus"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/session"
"github.com/grafana/grafana/pkg/setting"
)
func
QuotaReached
(
c
*
m
.
ReqContext
,
target
string
)
(
bool
,
error
)
{
if
!
setting
.
Quota
.
Enabled
{
return
false
,
nil
}
// get the list of scopes that this target is valid for. Org, User, Global
scopes
,
err
:=
m
.
GetQuotaScopes
(
target
)
if
err
!=
nil
{
return
false
,
err
}
for
_
,
scope
:=
range
scopes
{
c
.
Logger
.
Debug
(
"Checking quota"
,
"target"
,
target
,
"scope"
,
scope
)
switch
scope
.
Name
{
case
"global"
:
if
scope
.
DefaultLimit
<
0
{
continue
}
if
scope
.
DefaultLimit
==
0
{
return
true
,
nil
}
if
target
==
"session"
{
usedSessions
:=
session
.
GetSessionCount
()
if
int64
(
usedSessions
)
>
scope
.
DefaultLimit
{
c
.
Logger
.
Debug
(
"Sessions limit reached"
,
"active"
,
usedSessions
,
"limit"
,
scope
.
DefaultLimit
)
return
true
,
nil
}
continue
}
query
:=
m
.
GetGlobalQuotaByTargetQuery
{
Target
:
scope
.
Target
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
true
,
err
}
if
query
.
Result
.
Used
>=
scope
.
DefaultLimit
{
return
true
,
nil
}
case
"org"
:
if
!
c
.
IsSignedIn
{
continue
}
query
:=
m
.
GetOrgQuotaByTargetQuery
{
OrgId
:
c
.
OrgId
,
Target
:
scope
.
Target
,
Default
:
scope
.
DefaultLimit
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
true
,
err
}
if
query
.
Result
.
Limit
<
0
{
continue
}
if
query
.
Result
.
Limit
==
0
{
return
true
,
nil
}
if
query
.
Result
.
Used
>=
query
.
Result
.
Limit
{
return
true
,
nil
}
case
"user"
:
if
!
c
.
IsSignedIn
||
c
.
UserId
==
0
{
continue
}
query
:=
m
.
GetUserQuotaByTargetQuery
{
UserId
:
c
.
UserId
,
Target
:
scope
.
Target
,
Default
:
scope
.
DefaultLimit
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
true
,
err
}
if
query
.
Result
.
Limit
<
0
{
continue
}
if
query
.
Result
.
Limit
==
0
{
return
true
,
nil
}
if
query
.
Result
.
Used
>=
query
.
Result
.
Limit
{
return
true
,
nil
}
}
}
return
false
,
nil
}
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