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
f60efed5
Commit
f60efed5
authored
Jul 19, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(alerting): progress on refactoring backend alert rule model
parent
20fcffb7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
100 additions
and
16 deletions
+100
-16
pkg/services/alerting/alert_rule_test.go
+22
-1
pkg/services/alerting/conditions.go
+66
-15
pkg/services/alerting/interfaces.go
+12
-0
No files found.
pkg/services/alerting/alert_rule_test.go
View file @
f60efed5
...
...
@@ -31,7 +31,7 @@ func TestAlertRuleModel(t *testing.T) {
So
(
seconds
,
ShouldEqual
,
1
)
})
Convey
(
""
,
func
()
{
Convey
(
"
can construct alert rule model
"
,
func
()
{
json
:=
`
{
"name": "name2",
...
...
@@ -70,6 +70,27 @@ func TestAlertRuleModel(t *testing.T) {
So
(
err
,
ShouldBeNil
)
So
(
alertRule
.
Conditions
,
ShouldHaveLength
,
1
)
Convey
(
"Can read query condition from json model"
,
func
()
{
queryCondition
,
ok
:=
alertRule
.
Conditions
[
0
]
.
(
*
QueryCondition
)
So
(
ok
,
ShouldBeTrue
)
So
(
queryCondition
.
Query
.
From
,
ShouldEqual
,
"5m"
)
So
(
queryCondition
.
Query
.
To
,
ShouldEqual
,
"now"
)
So
(
queryCondition
.
Query
.
DatasourceId
,
ShouldEqual
,
1
)
Convey
(
"Can read query reducer"
,
func
()
{
reducer
,
ok
:=
queryCondition
.
Reducer
.
(
*
SimpleReducer
)
So
(
ok
,
ShouldBeTrue
)
So
(
reducer
.
Type
,
ShouldEqual
,
"avg"
)
})
Convey
(
"Can read evaluator"
,
func
()
{
evaluator
,
ok
:=
queryCondition
.
Evaluator
.
(
*
DefaultAlertEvaluator
)
So
(
ok
,
ShouldBeTrue
)
So
(
evaluator
.
Type
,
ShouldEqual
,
">"
)
})
})
})
})
}
pkg/services/alerting/conditions.go
View file @
f60efed5
package
alerting
import
"github.com/grafana/grafana/pkg/components/simplejson"
import
(
"encoding/json"
"errors"
type
AlertCondition
interface
{
Eval
()
}
"github.com/grafana/grafana/pkg/components/simplejson"
)
type
QueryCondition
struct
{
Query
AlertQuery
Reducer
AlertReducerModel
Evaluator
AlertEvaluator
Model
Reducer
QueryReducer
Evaluator
AlertEvaluator
}
func
(
c
*
QueryCondition
)
Eval
()
{
}
type
AlertReducerModel
struct
{
Type
string
Params
[]
interface
{}
func
NewQueryCondition
(
model
*
simplejson
.
Json
)
(
*
QueryCondition
,
error
)
{
condition
:=
QueryCondition
{}
queryJson
:=
model
.
Get
(
"query"
)
condition
.
Query
.
Query
=
queryJson
.
Get
(
"query"
)
.
MustString
()
condition
.
Query
.
From
=
queryJson
.
Get
(
"params"
)
.
MustArray
()[
1
]
.
(
string
)
condition
.
Query
.
To
=
queryJson
.
Get
(
"params"
)
.
MustArray
()[
2
]
.
(
string
)
condition
.
Query
.
DatasourceId
=
queryJson
.
Get
(
"datasourceId"
)
.
MustInt64
()
reducerJson
:=
model
.
Get
(
"reducer"
)
condition
.
Reducer
=
NewSimpleReducer
(
reducerJson
.
Get
(
"type"
)
.
MustString
())
evaluatorJson
:=
model
.
Get
(
"evaluator"
)
evaluator
,
err
:=
NewDefaultAlertEvaluator
(
evaluatorJson
)
if
err
!=
nil
{
return
nil
,
err
}
condition
.
Evaluator
=
evaluator
return
&
condition
,
nil
}
type
AlertEvaluatorModel
struct
{
Type
string
Params
[]
interface
{}
type
SimpleReducer
struct
{
Type
string
}
func
NewQueryCondition
(
model
*
simplejson
.
Json
)
(
*
QueryCondition
,
error
)
{
condition
:=
QueryCondition
{}
func
(
s
*
SimpleReducer
)
Reduce
()
float64
{
return
0
}
return
&
condition
,
nil
func
NewSimpleReducer
(
typ
string
)
*
SimpleReducer
{
return
&
SimpleReducer
{
Type
:
typ
}
}
type
DefaultAlertEvaluator
struct
{
Type
string
Threshold
float64
}
func
(
e
*
DefaultAlertEvaluator
)
Eval
()
bool
{
return
true
}
func
NewDefaultAlertEvaluator
(
model
*
simplejson
.
Json
)
(
*
DefaultAlertEvaluator
,
error
)
{
evaluator
:=
&
DefaultAlertEvaluator
{}
evaluator
.
Type
=
model
.
Get
(
"type"
)
.
MustString
()
if
evaluator
.
Type
==
""
{
return
nil
,
errors
.
New
(
"Alert evaluator missing type property"
)
}
params
:=
model
.
Get
(
"params"
)
.
MustArray
()
if
len
(
params
)
==
0
{
return
nil
,
errors
.
New
(
"Alert evaluator missing threshold parameter"
)
}
threshold
,
ok
:=
params
[
0
]
.
(
json
.
Number
)
if
!
ok
{
return
nil
,
errors
.
New
(
"Alert evaluator has invalid threshold parameter"
)
}
evaluator
.
Threshold
,
_
=
threshold
.
Float64
()
return
evaluator
,
nil
}
pkg/services/alerting/interfaces.go
View file @
f60efed5
...
...
@@ -14,3 +14,15 @@ type Scheduler interface {
type
Notifier
interface
{
Notify
(
alertResult
*
AlertResult
)
}
type
AlertCondition
interface
{
Eval
()
}
type
QueryReducer
interface
{
Reduce
()
float64
}
type
AlertEvaluator
interface
{
Eval
()
bool
}
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