Skip to content

Commit

Permalink
app/vmselect/promql: adjust integrate() calculations to be more sim…
Browse files Browse the repository at this point in the history
…ilar to calculations from InfluxDB: attempt #2

Updates #701
  • Loading branch information
valyala committed Sep 8, 2020
1 parent 8e85b56 commit e6da63d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
17 changes: 6 additions & 11 deletions app/vmselect/promql/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
return nan
}
return 0
}
prevValue := rfa.prevValue
prevTimestamp := rfa.currTimestamp - rfa.window
if math.IsNaN(prevValue) {
if len(values) == 0 {
return nan
}
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 {
Expand All @@ -1714,6 +1707,8 @@ func rollupIntegrate(rfa *rollupFuncArg) float64 {
prevTimestamp = timestamp
prevValue = v
}
dt := float64(rfa.currTimestamp - prevTimestamp) / 1e3
sum += prevValue * dt
return sum
}

Expand Down
4 changes: 2 additions & 2 deletions app/vmselect/promql/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit e6da63d

Please sign in to comment.