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
fb636344
Commit
fb636344
authored
Jul 20, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(alerting): began work on testing alert rule from UI without having to save it
parent
0ce55600
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
1 deletions
+90
-1
pkg/api/alerting.go
+16
-0
pkg/api/api.go
+1
-0
pkg/api/dtos/alerting.go
+10
-1
pkg/services/alerting/test_rule.go
+63
-0
No files found.
pkg/api/alerting.go
View file @
fb636344
...
...
@@ -5,6 +5,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
func
ValidateOrgAlert
(
c
*
middleware
.
Context
)
{
...
...
@@ -71,6 +72,21 @@ func GetAlerts(c *middleware.Context) Response {
return
Json
(
200
,
alertDTOs
)
}
// POST /api/alerts/test
func
TestAlertRule
(
c
*
middleware
.
Context
,
dto
dtos
.
TestAlertRuleCommand
)
Response
{
backendCmd
:=
alerting
.
TestAlertRuleCommand
{
OrgId
:
c
.
OrgId
,
Dashboard
:
dto
.
Dashboard
,
PanelId
:
dto
.
PanelId
,
}
if
err
:=
bus
.
Dispatch
(
&
backendCmd
);
err
!=
nil
{
return
ApiError
(
500
,
"Failed to test rule"
,
err
)
}
return
Json
(
200
,
backendCmd
.
Result
)
}
// GET /api/alerts/:id
func
GetAlert
(
c
*
middleware
.
Context
)
Response
{
id
:=
c
.
ParamsInt64
(
":alertId"
)
...
...
pkg/api/api.go
View file @
fb636344
...
...
@@ -246,6 +246,7 @@ func Register(r *macaron.Macaron) {
r
.
Get
(
"/metrics"
,
wrap
(
GetInternalMetrics
))
r
.
Group
(
"/alerts"
,
func
()
{
r
.
Post
(
"/test"
,
bind
(
dtos
.
TestAlertRuleCommand
{}),
wrap
(
TestAlertRule
))
r
.
Get
(
"/:alertId/states"
,
wrap
(
GetAlertStates
))
//r.Put("/:alertId/state", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState))
r
.
Get
(
"/:alertId"
,
ValidateOrgAlert
,
wrap
(
GetAlert
))
...
...
pkg/api/dtos/alerting.go
View file @
fb636344
package
dtos
import
"time"
import
(
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
)
type
AlertRuleDTO
struct
{
Id
int64
`json:"id"`
...
...
@@ -29,3 +33,8 @@ type AlertNotificationDTO struct {
Created
time
.
Time
`json:"created"`
Updated
time
.
Time
`json:"updated"`
}
type
TestAlertRuleCommand
struct
{
Dashboard
*
simplejson
.
Json
`json:"dashboard"`
PanelId
int64
`json:"panelId"`
}
pkg/services/alerting/test_rule.go
0 → 100644
View file @
fb636344
package
alerting
import
(
"fmt"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m
"github.com/grafana/grafana/pkg/models"
)
type
TestAlertRuleCommand
struct
{
Dashboard
*
simplejson
.
Json
PanelId
int64
OrgId
int64
Result
*
AlertResultContext
}
func
init
()
{
bus
.
AddHandler
(
"alerting"
,
handleTestAlertRuleCommand
)
}
func
handleTestAlertRuleCommand
(
cmd
*
TestAlertRuleCommand
)
error
{
dash
,
err
:=
m
.
NewDashboardFromJson
(
cmd
.
Dashboard
)
if
err
!=
nil
{
return
err
}
extractor
:=
NewDashAlertExtractor
(
cmd
.
Dashboard
)
rules
,
err
:=
extractor
.
GetAlerts
()
if
err
!=
nil
{
return
err
}
for
_
,
rule
:=
range
rules
{
if
rule
.
PanelId
==
cmd
.
PanelId
{
if
res
,
err
:=
testAlertRule
(
rule
);
err
!=
nil
{
return
err
}
else
{
cmd
.
Result
=
res
return
nil
}
}
}
return
fmt
.
Errorf
(
"Could not find alert with panel id %d"
,
cmd
.
PanelId
)
}
func
testAlertRule
(
rule
*
AlertRule
)
(
*
AlertResultContext
,
error
)
{
handler
:=
NewHandler
()
resultChan
:=
make
(
chan
*
AlertResultContext
,
1
)
handler
.
Execute
(
rule
,
resultChan
)
select
{
case
<-
time
.
After
(
time
.
Second
*
10
)
:
return
&
AlertResultContext
{
Error
:
fmt
.
Errorf
(
"Timeout"
)},
nil
case
result
:=
<-
resultChan
:
return
result
,
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