Commit 7d2b9894 by Carl Bergquist Committed by GitHub

Merge pull request #9386 from shanhuhai5739/master

add diff and pdiff for conditions
parents 992cc1b6 68ed4d45
...@@ -94,6 +94,53 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { ...@@ -94,6 +94,53 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
value = (values[(length/2)-1] + values[length/2]) / 2 value = (values[(length/2)-1] + values[length/2]) / 2
} }
} }
case "diff":
var (
points = series.Points
first float64
i int
)
// get the newest point
for i = len(points) - 1; i >= 0; i-- {
if points[i][0].Valid {
allNull = false
first = points[i][0].Float64
break
}
}
// get other points
points = points[0:i]
for i := len(points) - 1; i >= 0; i-- {
if points[i][0].Valid {
allNull = false
value = first - points[i][0].Float64
break
}
}
case "percent_diff":
var (
points = series.Points
first float64
i int
)
// get the newest point
for i = len(points) - 1; i >= 0; i-- {
if points[i][0].Valid {
allNull = false
first = points[i][0].Float64
break
}
}
// get other points
points = points[0:i]
for i := len(points) - 1; i >= 0; i-- {
if points[i][0].Valid {
allNull = false
val := (first - points[i][0].Float64) / points[i][0].Float64 * 100
value = math.Abs(val)
break
}
}
} }
if allNull { if allNull {
......
...@@ -80,6 +80,17 @@ func TestSimpleReducer(t *testing.T) { ...@@ -80,6 +80,17 @@ func TestSimpleReducer(t *testing.T) {
So(reducer.Reduce(series).Float64, ShouldEqual, float64(3)) So(reducer.Reduce(series).Float64, ShouldEqual, float64(3))
}) })
Convey("diff", func() {
result := testReducer("diff", 30, 40)
So(result, ShouldEqual, float64(10))
})
Convey("percent_diff", func() {
result := testReducer("percent_diff", 30, 40)
So(result, ShouldEqual, float64(33.33333333333333))
})
}) })
} }
......
...@@ -49,6 +49,8 @@ var reducerTypes = [ ...@@ -49,6 +49,8 @@ var reducerTypes = [
{text: 'count()', value: 'count'}, {text: 'count()', value: 'count'},
{text: 'last()', value: 'last'}, {text: 'last()', value: 'last'},
{text: 'median()', value: 'median'}, {text: 'median()', value: 'median'},
{text: 'diff()', value: 'diff'},
{text: 'percent_diff()', value: 'percent_diff'},
]; ];
var noDataModes = [ var noDataModes = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment