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
4c28ec83
Unverified
Commit
4c28ec83
authored
Feb 15, 2019
by
Torkel Ödegaard
Committed by
GitHub
Feb 15, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #15444 from max-neverov/percent_diff_null
Fix percent_diff calculation when points are nulls
parents
bc1aec9e
28eaac3a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
36 deletions
+54
-36
pkg/services/alerting/conditions/reducer.go
+30
-36
pkg/services/alerting/conditions/reducer_test.go
+24
-0
No files found.
pkg/services/alerting/conditions/reducer.go
View file @
4c28ec83
...
@@ -95,29 +95,33 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
...
@@ -95,29 +95,33 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
}
}
}
}
case
"diff"
:
case
"diff"
:
var
(
allNull
,
value
=
calculateDiff
(
series
,
allNull
,
value
,
diff
)
points
=
series
.
Points
case
"percent_diff"
:
first
float64
allNull
,
value
=
calculateDiff
(
series
,
allNull
,
value
,
percentDiff
)
i
int
case
"count_non_null"
:
)
for
_
,
v
:=
range
series
.
Points
{
// get the newest point
if
v
[
0
]
.
Valid
{
for
i
=
len
(
points
)
-
1
;
i
>=
0
;
i
--
{
value
++
if
points
[
i
][
0
]
.
Valid
{
allNull
=
false
first
=
points
[
i
][
0
]
.
Float64
break
}
}
}
}
// get the oldest point
points
=
points
[
0
:
i
]
if
value
>
0
{
for
i
:=
0
;
i
<
len
(
points
);
i
++
{
if
points
[
i
][
0
]
.
Valid
{
allNull
=
false
allNull
=
false
value
=
first
-
points
[
i
][
0
]
.
Float64
break
}
}
}
}
case
"percent_diff"
:
if
allNull
{
return
null
.
FloatFromPtr
(
nil
)
}
return
null
.
FloatFrom
(
value
)
}
func
NewSimpleReducer
(
typ
string
)
*
SimpleReducer
{
return
&
SimpleReducer
{
Type
:
typ
}
}
func
calculateDiff
(
series
*
tsdb
.
TimeSeries
,
allNull
bool
,
value
float64
,
fn
func
(
float64
,
float64
)
float64
)
(
bool
,
float64
)
{
var
(
var
(
points
=
series
.
Points
points
=
series
.
Points
first
float64
first
float64
...
@@ -131,35 +135,25 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
...
@@ -131,35 +135,25 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
break
break
}
}
}
}
if
i
>=
1
{
// get the oldest point
// get the oldest point
points
=
points
[
0
:
i
]
points
=
points
[
0
:
i
]
for
i
:=
0
;
i
<
len
(
points
);
i
++
{
for
i
:=
0
;
i
<
len
(
points
);
i
++
{
if
points
[
i
][
0
]
.
Valid
{
if
points
[
i
][
0
]
.
Valid
{
allNull
=
false
allNull
=
false
val
:=
(
first
-
points
[
i
][
0
]
.
Float64
)
/
points
[
i
][
0
]
.
Float64
*
100
val
:=
fn
(
first
,
points
[
i
][
0
]
.
Float64
)
value
=
math
.
Abs
(
val
)
value
=
math
.
Abs
(
val
)
break
break
}
}
}
}
case
"count_non_null"
:
for
_
,
v
:=
range
series
.
Points
{
if
v
[
0
]
.
Valid
{
value
++
}
}
if
value
>
0
{
allNull
=
false
}
}
if
allNull
{
return
null
.
FloatFromPtr
(
nil
)
}
}
return
allNull
,
value
}
return
null
.
FloatFrom
(
value
)
var
diff
=
func
(
newest
,
oldest
float64
)
float64
{
return
newest
-
oldest
}
}
func
NewSimpleReducer
(
typ
string
)
*
SimpleReducer
{
var
percentDiff
=
func
(
newest
,
oldest
float64
)
float64
{
return
&
SimpleReducer
{
Type
:
typ
}
return
(
newest
-
oldest
)
/
oldest
*
100
}
}
pkg/services/alerting/conditions/reducer_test.go
View file @
4c28ec83
...
@@ -143,6 +143,18 @@ func TestSimpleReducer(t *testing.T) {
...
@@ -143,6 +143,18 @@ func TestSimpleReducer(t *testing.T) {
So
(
result
,
ShouldEqual
,
float64
(
10
))
So
(
result
,
ShouldEqual
,
float64
(
10
))
})
})
Convey
(
"diff with only nulls"
,
func
()
{
reducer
:=
NewSimpleReducer
(
"diff"
)
series
:=
&
tsdb
.
TimeSeries
{
Name
:
"test time serie"
,
}
series
.
Points
=
append
(
series
.
Points
,
tsdb
.
NewTimePoint
(
null
.
FloatFromPtr
(
nil
),
1
))
series
.
Points
=
append
(
series
.
Points
,
tsdb
.
NewTimePoint
(
null
.
FloatFromPtr
(
nil
),
2
))
So
(
reducer
.
Reduce
(
series
)
.
Valid
,
ShouldEqual
,
false
)
})
Convey
(
"percent_diff one point"
,
func
()
{
Convey
(
"percent_diff one point"
,
func
()
{
result
:=
testReducer
(
"percent_diff"
,
40
)
result
:=
testReducer
(
"percent_diff"
,
40
)
So
(
result
,
ShouldEqual
,
float64
(
0
))
So
(
result
,
ShouldEqual
,
float64
(
0
))
...
@@ -157,6 +169,18 @@ func TestSimpleReducer(t *testing.T) {
...
@@ -157,6 +169,18 @@ func TestSimpleReducer(t *testing.T) {
result
:=
testReducer
(
"percent_diff"
,
30
,
40
,
40
)
result
:=
testReducer
(
"percent_diff"
,
30
,
40
,
40
)
So
(
result
,
ShouldEqual
,
float64
(
33.33333333333333
))
So
(
result
,
ShouldEqual
,
float64
(
33.33333333333333
))
})
})
Convey
(
"percent_diff with only nulls"
,
func
()
{
reducer
:=
NewSimpleReducer
(
"percent_diff"
)
series
:=
&
tsdb
.
TimeSeries
{
Name
:
"test time serie"
,
}
series
.
Points
=
append
(
series
.
Points
,
tsdb
.
NewTimePoint
(
null
.
FloatFromPtr
(
nil
),
1
))
series
.
Points
=
append
(
series
.
Points
,
tsdb
.
NewTimePoint
(
null
.
FloatFromPtr
(
nil
),
2
))
So
(
reducer
.
Reduce
(
series
)
.
Valid
,
ShouldEqual
,
false
)
})
})
})
}
}
...
...
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