app/vmselect/promql: consistently calculate rate_over_sum(m[d]) as sum_over_time(m[d])/d

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3045
This commit is contained in:
Aliaksandr Valialkin 2022-09-02 23:18:01 +03:00
parent 2d4619c9a0
commit 7dc632719d
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 4 additions and 6 deletions

View File

@ -1349,15 +1349,11 @@ func rollupRateOverSum(rfa *rollupFuncArg) float64 {
// Assume that the value didn't change since rfa.prevValue. // Assume that the value didn't change since rfa.prevValue.
return 0 return 0
} }
dt := rfa.window
if !math.IsNaN(rfa.prevValue) {
dt = timestamps[len(timestamps)-1] - rfa.prevTimestamp
}
sum := float64(0) sum := float64(0)
for _, v := range rfa.values { for _, v := range rfa.values {
sum += v sum += v
} }
return sum / (float64(dt) / 1e3) return sum / (float64(rfa.window) / 1e3)
} }
func rollupRange(rfa *rollupFuncArg) float64 { func rollupRange(rfa *rollupFuncArg) float64 {

View File

@ -1307,7 +1307,7 @@ func TestRollupFuncsNoWindow(t *testing.T) {
if samplesScanned == 0 { if samplesScanned == 0 {
t.Fatalf("expecting non-zero samplesScanned from rollupConfig.Do") t.Fatalf("expecting non-zero samplesScanned from rollupConfig.Do")
} }
valuesExpected := []float64{nan, 2775, 5262.5, 3678.5714285714284, 2880} valuesExpected := []float64{nan, 2775, 5262.5, 3862.5, 1800}
timestampsExpected := []int64{0, 40, 80, 120, 160} timestampsExpected := []int64{0, 40, 80, 120, 160}
testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected) testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected)
}) })

View File

@ -15,6 +15,8 @@ The following tip changes can be tested by building VictoriaMetrics components f
## tip ## tip
* BUGFIX: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): properly calculate `rate_over_sum(m[d])` as `sum_over_time(m[d])/d`. Previously the `sum_over_time(m[d])` could be improperly divided by smaller than `d` time range. See [rate_over_sum() docs](https://docs.victoriametrics.com/MetricsQL.html#rate_over_sum) and [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3045).
## [v1.81.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.81.1) ## [v1.81.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.81.1)