From 4f06eed1c11902473cafb733d4e30a4a6b4f5cf0 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 8 Sep 2020 14:35:39 +0300 Subject: [PATCH] app/vmselect/promql: adjust `integrate()` calculations to be more similar to calculations from InfluxDB: attempt #2 Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/701 --- app/vmselect/promql/rollup.go | 17 ++++++----------- app/vmselect/promql/rollup_test.go | 4 ++-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/app/vmselect/promql/rollup.go b/app/vmselect/promql/rollup.go index 16087c052e..27d390df8c 100644 --- a/app/vmselect/promql/rollup.go +++ b/app/vmselect/promql/rollup.go @@ -1683,28 +1683,21 @@ func rollupDistinct(rfa *rollupFuncArg) float64 { } func rollupIntegrate(rfa *rollupFuncArg) float64 { - prevTimestamp := rfa.prevTimestamp - // There is no need in handling NaNs here, since they must be cleaned up // before calling rollup funcs. values := rfa.values timestamps := rfa.timestamps - if len(values) == 0 { - if math.IsNaN(rfa.prevValue) { + prevValue := rfa.prevValue + prevTimestamp := rfa.currTimestamp - rfa.window + if math.IsNaN(prevValue) { + if len(values) == 0 { return nan } - return 0 - } - prevValue := rfa.prevValue - if math.IsNaN(prevValue) { prevValue = values[0] prevTimestamp = timestamps[0] values = values[1:] timestamps = timestamps[1:] } - if len(values) == 0 { - return 0 - } var sum float64 for i, v := range values { @@ -1714,6 +1707,8 @@ func rollupIntegrate(rfa *rollupFuncArg) float64 { prevTimestamp = timestamp prevValue = v } + dt := float64(rfa.currTimestamp - prevTimestamp) / 1e3 + sum += prevValue * dt return sum } diff --git a/app/vmselect/promql/rollup_test.go b/app/vmselect/promql/rollup_test.go index aef7c335f6..c75626f452 100644 --- a/app/vmselect/promql/rollup_test.go +++ b/app/vmselect/promql/rollup_test.go @@ -431,7 +431,7 @@ func TestRollupNewRollupFuncSuccess(t *testing.T) { f("stdvar_over_time", 945.7430555555555) f("first_over_time", 123) f("last_over_time", 34) - f("integrate", 5.237) + f("integrate", 0.817) f("distinct_over_time", 8) f("ideriv", 0) f("decreases_over_time", 5) @@ -970,7 +970,7 @@ func TestRollupFuncsNoWindow(t *testing.T) { } rc.Timestamps = getTimestamps(rc.Start, rc.End, rc.Step) values := rc.Do(nil, testValues, testTimestamps) - valuesExpected := []float64{nan, 2.064, 1.677, 1.156, 0.34} + valuesExpected := []float64{nan, 2.148, 1.593, 1.156, 1.36} timestampsExpected := []int64{0, 40, 80, 120, 160} testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected) })