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
ad56f67a
Commit
ad56f67a
authored
Oct 21, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(alerting): add support to keep last state on no data
closes #6332
parent
96008c97
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
58 deletions
+41
-58
pkg/models/alert.go
+2
-0
pkg/services/alerting/result_handler.go
+10
-2
pkg/services/alerting/result_handler_test.go
+28
-56
public/app/features/alerting/alert_def.ts
+1
-0
No files found.
pkg/models/alert.go
View file @
ad56f67a
...
...
@@ -15,6 +15,8 @@ const (
AlertStatePaused
AlertStateType
=
"paused"
AlertStateAlerting
AlertStateType
=
"alerting"
AlertStateOK
AlertStateType
=
"ok"
KeepLastAlertState
AlertStateType
=
"keep_last"
)
func
(
s
AlertStateType
)
IsValid
()
bool
{
...
...
pkg/services/alerting/result_handler.go
View file @
ad56f67a
...
...
@@ -41,7 +41,6 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
evalContext
.
Rule
.
State
=
m
.
AlertStateAlerting
annotationData
=
simplejson
.
NewFromAny
(
evalContext
.
EvalMatches
)
}
else
{
// handle no data case
if
evalContext
.
NoDataFound
{
evalContext
.
Rule
.
State
=
evalContext
.
Rule
.
NoDataState
}
else
{
...
...
@@ -50,7 +49,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
}
countStateResult
(
evalContext
.
Rule
.
State
)
if
evalContext
.
Rule
.
State
!=
oldState
{
if
handler
.
shouldUpdateAlertState
(
evalContext
,
oldState
)
{
handler
.
log
.
Info
(
"New state change"
,
"alertId"
,
evalContext
.
Rule
.
Id
,
"newState"
,
evalContext
.
Rule
.
State
,
"oldState"
,
oldState
)
cmd
:=
&
m
.
SetAlertStateCommand
{
...
...
@@ -91,6 +90,15 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
return
nil
}
func
(
handler
*
DefaultResultHandler
)
shouldUpdateAlertState
(
evalContext
*
EvalContext
,
oldState
m
.
AlertStateType
)
bool
{
if
evalContext
.
NoDataFound
&&
evalContext
.
Rule
.
NoDataState
==
m
.
KeepLastAlertState
{
evalContext
.
Rule
.
State
=
oldState
return
false
}
return
evalContext
.
Rule
.
State
!=
oldState
}
func
countStateResult
(
state
m
.
AlertStateType
)
{
switch
state
{
case
m
.
AlertStateAlerting
:
...
...
pkg/services/alerting/result_handler_test.go
View file @
ad56f67a
package
alerting
// import (
// "testing"
// "time"
//
// "github.com/grafana/grafana/pkg/bus"
// m "github.com/grafana/grafana/pkg/models"
// "github.com/grafana/grafana/pkg/services/alerting/alertstates"
//
// . "github.com/smartystreets/goconvey/convey"
// )
//
// func TestAlertResultHandler(t *testing.T) {
// Convey("Test result Handler", t, func() {
// resultHandler := ResultHandlerImpl{}
// mockResult := &AlertResultContext{
// Triggered: false,
// Rule: &AlertRule{
// Id: 1,
// OrgId 1,
// },
// }
// mockAlertState := &m.AlertState{}
// bus.ClearBusHandlers()
// bus.AddHandler("test", func(query *m.GetLastAlertStateQuery) error {
// query.Result = mockAlertState
// return nil
// })
//
// Convey("Should update", func() {
//
// Convey("when no earlier alert state", func() {
// mockAlertState = nil
// So(resultHandler.shouldUpdateState(mockResult), ShouldBeTrue)
// })
//
// Convey("alert state have changed", func() {
// mockAlertState = &m.AlertState{
// State: alertstates.Critical,
// }
// mockResult.Triggered = false
// So(resultHandler.shouldUpdateState(mockResult), ShouldBeTrue)
// })
//
// Convey("last alert state was 15min ago", func() {
// now := time.Now()
// mockAlertState = &m.AlertState{
// State: alertstates.Critical,
// Created: now.Add(time.Minute * -30),
// }
// mockResult.Triggered = true
// mockResult.StartTime = time.Now()
// So(resultHandler.shouldUpdateState(mockResult), ShouldBeTrue)
// })
// })
// })
// }
import
(
"context"
"testing"
"github.com/grafana/grafana/pkg/models"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestAlertResultHandler
(
t
*
testing
.
T
)
{
Convey
(
"Test result Handler"
,
t
,
func
()
{
handler
:=
NewResultHandler
()
evalContext
:=
NewEvalContext
(
context
.
TODO
(),
&
Rule
{})
Convey
(
"Should update"
,
func
()
{
Convey
(
"when no earlier alert state"
,
func
()
{
oldState
:=
models
.
AlertStateOK
evalContext
.
Rule
.
State
=
models
.
AlertStateAlerting
evalContext
.
Rule
.
NoDataState
=
models
.
KeepLastAlertState
evalContext
.
NoDataFound
=
true
So
(
handler
.
shouldUpdateAlertState
(
evalContext
,
oldState
),
ShouldBeFalse
)
})
})
})
}
public/app/features/alerting/alert_def.ts
View file @
ad56f67a
...
...
@@ -40,6 +40,7 @@ var noDataModes = [
{
text
:
'OK'
,
value
:
'ok'
},
{
text
:
'Alerting'
,
value
:
'alerting'
},
{
text
:
'No Data'
,
value
:
'no_data'
},
{
text
:
'Keep Last'
,
value
:
'keep_last'
},
];
function
createReducerPart
(
model
)
{
...
...
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