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
9e0da02b
Unverified
Commit
9e0da02b
authored
Nov 05, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor dashboard alert extractor
parent
c5ce8536
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
39 additions
and
15 deletions
+39
-15
pkg/api/alerting.go
+4
-0
pkg/models/alert.go
+2
-1
pkg/services/alerting/commands.go
+3
-3
pkg/services/alerting/extractor.go
+18
-1
pkg/services/alerting/extractor_test.go
+8
-8
pkg/services/alerting/test_rule.go
+2
-1
pkg/services/dashboards/dashboard_service.go
+2
-1
No files found.
pkg/api/alerting.go
View file @
9e0da02b
...
...
@@ -134,12 +134,16 @@ func AlertTest(c *m.ReqContext, dto dtos.AlertTestCommand) Response {
OrgId
:
c
.
OrgId
,
Dashboard
:
dto
.
Dashboard
,
PanelId
:
dto
.
PanelId
,
User
:
c
.
SignedInUser
,
}
if
err
:=
bus
.
Dispatch
(
&
backendCmd
);
err
!=
nil
{
if
validationErr
,
ok
:=
err
.
(
alerting
.
ValidationError
);
ok
{
return
Error
(
422
,
validationErr
.
Error
(),
nil
)
}
if
err
==
m
.
ErrDataSourceAccessDenied
{
return
Error
(
403
,
"Access denied to datasource"
,
err
)
}
return
Error
(
500
,
"Failed to test rule"
,
err
)
}
...
...
pkg/models/alert.go
View file @
9e0da02b
...
...
@@ -215,13 +215,14 @@ type AlertStateInfoDTO struct {
// "Internal" commands
type
UpdateDashboardAlertsCommand
struct
{
UserId
int64
OrgId
int64
Dashboard
*
Dashboard
User
*
SignedInUser
}
type
ValidateDashboardAlertsCommand
struct
{
UserId
int64
OrgId
int64
Dashboard
*
Dashboard
User
*
SignedInUser
}
pkg/services/alerting/commands.go
View file @
9e0da02b
...
...
@@ -11,7 +11,7 @@ func init() {
}
func
validateDashboardAlerts
(
cmd
*
m
.
ValidateDashboardAlertsCommand
)
error
{
extractor
:=
NewDashAlertExtractor
(
cmd
.
Dashboard
,
cmd
.
OrgId
)
extractor
:=
NewDashAlertExtractor
(
cmd
.
Dashboard
,
cmd
.
OrgId
,
cmd
.
User
)
return
extractor
.
ValidateAlerts
()
}
...
...
@@ -19,11 +19,11 @@ func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
func
updateDashboardAlerts
(
cmd
*
m
.
UpdateDashboardAlertsCommand
)
error
{
saveAlerts
:=
m
.
SaveAlertsCommand
{
OrgId
:
cmd
.
OrgId
,
UserId
:
cmd
.
UserId
,
UserId
:
cmd
.
User
.
User
Id
,
DashboardId
:
cmd
.
Dashboard
.
Id
,
}
extractor
:=
NewDashAlertExtractor
(
cmd
.
Dashboard
,
cmd
.
OrgId
)
extractor
:=
NewDashAlertExtractor
(
cmd
.
Dashboard
,
cmd
.
OrgId
,
cmd
.
User
)
alerts
,
err
:=
extractor
.
GetAlerts
()
if
err
!=
nil
{
...
...
pkg/services/alerting/extractor.go
View file @
9e0da02b
...
...
@@ -13,14 +13,16 @@ import (
// DashAlertExtractor extracts alerts from the dashboard json
type
DashAlertExtractor
struct
{
User
*
m
.
SignedInUser
Dash
*
m
.
Dashboard
OrgID
int64
log
log
.
Logger
}
// NewDashAlertExtractor returns a new DashAlertExtractor
func
NewDashAlertExtractor
(
dash
*
m
.
Dashboard
,
orgID
int64
)
*
DashAlertExtractor
{
func
NewDashAlertExtractor
(
dash
*
m
.
Dashboard
,
orgID
int64
,
user
*
m
.
SignedInUser
)
*
DashAlertExtractor
{
return
&
DashAlertExtractor
{
User
:
user
,
Dash
:
dash
,
OrgID
:
orgID
,
log
:
log
.
New
(
"alerting.extractor"
),
...
...
@@ -149,6 +151,21 @@ func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json,
return
nil
,
ValidationError
{
Reason
:
fmt
.
Sprintf
(
"Data source used by alert rule not found, alertName=%v, datasource=%s"
,
alert
.
Name
,
dsName
)}
}
dsFilterQuery
:=
m
.
DatasourcesPermissionFilterQuery
{
User
:
e
.
User
,
Datasources
:
[]
*
m
.
DataSource
{
datasource
},
}
if
err
:=
bus
.
Dispatch
(
&
dsFilterQuery
);
err
!=
nil
{
if
err
!=
bus
.
ErrHandlerNotFound
{
return
nil
,
err
}
}
else
{
if
len
(
dsFilterQuery
.
Result
)
==
0
{
return
nil
,
m
.
ErrDataSourceAccessDenied
}
}
jsonQuery
.
SetPath
([]
string
{
"datasourceId"
},
datasource
.
Id
)
if
interval
,
err
:=
panel
.
Get
(
"interval"
)
.
String
();
err
==
nil
{
...
...
pkg/services/alerting/extractor_test.go
View file @
9e0da02b
...
...
@@ -69,7 +69,7 @@ func TestAlertRuleExtraction(t *testing.T) {
So
(
getTarget
(
dashJson
),
ShouldEqual
,
""
)
})
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
_
,
_
=
extractor
.
GetAlerts
()
Convey
(
"Dashboard json should not be updated after extracting rules"
,
func
()
{
...
...
@@ -83,7 +83,7 @@ func TestAlertRuleExtraction(t *testing.T) {
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJson
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
alerts
,
err
:=
extractor
.
GetAlerts
()
...
...
@@ -146,7 +146,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson
,
err
:=
simplejson
.
NewJson
(
panelWithoutId
)
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJson
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
_
,
err
=
extractor
.
GetAlerts
()
...
...
@@ -162,7 +162,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson
,
err
:=
simplejson
.
NewJson
(
panelWithIdZero
)
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJson
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
_
,
err
=
extractor
.
GetAlerts
()
...
...
@@ -178,7 +178,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson
,
err
:=
simplejson
.
NewJson
(
json
)
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJson
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
alerts
,
err
:=
extractor
.
GetAlerts
()
...
...
@@ -198,7 +198,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson
,
err
:=
simplejson
.
NewJson
(
json
)
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJson
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
alerts
,
err
:=
extractor
.
GetAlerts
()
...
...
@@ -228,7 +228,7 @@ func TestAlertRuleExtraction(t *testing.T) {
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJson
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
alerts
,
err
:=
extractor
.
GetAlerts
()
...
...
@@ -248,7 +248,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJSON
,
err
:=
simplejson
.
NewJson
(
json
)
So
(
err
,
ShouldBeNil
)
dash
:=
m
.
NewDashboardFromJson
(
dashJSON
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
)
extractor
:=
NewDashAlertExtractor
(
dash
,
1
,
nil
)
err
=
extractor
.
ValidateAlerts
()
...
...
pkg/services/alerting/test_rule.go
View file @
9e0da02b
...
...
@@ -13,6 +13,7 @@ type AlertTestCommand struct {
Dashboard
*
simplejson
.
Json
PanelId
int64
OrgId
int64
User
*
m
.
SignedInUser
Result
*
EvalContext
}
...
...
@@ -25,7 +26,7 @@ func handleAlertTestCommand(cmd *AlertTestCommand) error {
dash
:=
m
.
NewDashboardFromJson
(
cmd
.
Dashboard
)
extractor
:=
NewDashAlertExtractor
(
dash
,
cmd
.
OrgId
)
extractor
:=
NewDashAlertExtractor
(
dash
,
cmd
.
OrgId
,
cmd
.
User
)
alerts
,
err
:=
extractor
.
GetAlerts
()
if
err
!=
nil
{
return
err
...
...
pkg/services/dashboards/dashboard_service.go
View file @
9e0da02b
...
...
@@ -90,6 +90,7 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
validateAlertsCmd
:=
models
.
ValidateDashboardAlertsCommand
{
OrgId
:
dto
.
OrgId
,
Dashboard
:
dash
,
User
:
dto
.
User
,
}
if
err
:=
bus
.
Dispatch
(
&
validateAlertsCmd
);
err
!=
nil
{
...
...
@@ -159,8 +160,8 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
func
(
dr
*
dashboardServiceImpl
)
updateAlerting
(
cmd
*
models
.
SaveDashboardCommand
,
dto
*
SaveDashboardDTO
)
error
{
alertCmd
:=
models
.
UpdateDashboardAlertsCommand
{
OrgId
:
dto
.
OrgId
,
UserId
:
dto
.
User
.
UserId
,
Dashboard
:
cmd
.
Result
,
User
:
dto
.
User
,
}
if
err
:=
bus
.
Dispatch
(
&
alertCmd
);
err
!=
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