Commit 066d5cf4 by Vikky Omkar Committed by GitHub

Alerting: Fixed the issue/bug of diff and percent_diff functions *Breaking change* (#21338)

fixes #16270 #10129
parent 0606555b
......@@ -96,8 +96,12 @@ func (s *queryReducer) Reduce(series *tsdb.TimeSeries) null.Float {
}
case "diff":
allNull, value = calculateDiff(series, allNull, value, diff)
case "diff_abs":
allNull, value = calculateDiff(series, allNull, value, diffAbs)
case "percent_diff":
allNull, value = calculateDiff(series, allNull, value, percentDiff)
case "percent_diff_abs":
allNull, value = calculateDiff(series, allNull, value, percentDiffAbs)
case "count_non_null":
for _, v := range series.Points {
if isValid(v[0]) {
......@@ -141,8 +145,7 @@ func calculateDiff(series *tsdb.TimeSeries, allNull bool, value float64, fn func
for i := 0; i < len(points); i++ {
if isValid(points[i][0]) {
allNull = false
val := fn(first, points[i][0].Float64)
value = math.Abs(val)
value = fn(first, points[i][0].Float64)
break
}
}
......@@ -158,6 +161,14 @@ var diff = func(newest, oldest float64) float64 {
return newest - oldest
}
var diffAbs = func(newest, oldest float64) float64 {
return math.Abs(newest - oldest)
}
var percentDiff = func(newest, oldest float64) float64 {
return (newest - oldest) / oldest * 100
return (newest - oldest) / math.Abs(oldest) * 100
}
var percentDiffAbs = func(newest, oldest float64) float64 {
return math.Abs((newest - oldest) / oldest * 100)
}
......@@ -47,7 +47,9 @@ const reducerTypes = [
{ text: 'last()', value: 'last' },
{ text: 'median()', value: 'median' },
{ text: 'diff()', value: 'diff' },
{ text: 'diff_abs()', value: 'diff_abs' },
{ text: 'percent_diff()', value: 'percent_diff' },
{ text: 'percent_diff_abs()', value: 'percent_diff_abs' },
{ text: 'count_non_null()', value: 'count_non_null' },
];
......
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