app/vmselect/promql: adjust integrate() calculations to be more similar to calculations from InfluxDB: attempt #2

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/701
This commit is contained in:
Aliaksandr Valialkin 2020-09-08 14:35:39 +03:00
parent 8e85b56737
commit e6da63dffe
2 changed files with 8 additions and 13 deletions

View File

@ -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
}

View File

@ -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)
})