app/vmselect/promql: properly take into account the first datapoint when calculating rollup_candlestick

This commit is contained in:
Aliaksandr Valialkin 2020-02-24 13:24:18 +02:00
parent b58e3fc8a9
commit 6ca1e58d98
2 changed files with 26 additions and 14 deletions

View File

@ -4750,7 +4750,7 @@ func TestExecSuccess(t *testing.T) {
}} }}
r2 := netstorage.Result{ r2 := netstorage.Result{
MetricName: metricNameExpected, MetricName: metricNameExpected,
Values: []float64{0.85, 0.15, 0.43, 0.76, 0.47, 0.21}, Values: []float64{0.9, 0.32, 0.82, 0.13, 0.28, 0.86},
Timestamps: timestampsExpected, Timestamps: timestampsExpected,
} }
r2.MetricName.Tags = []storage.Tag{{ r2.MetricName.Tags = []storage.Tag{{
@ -4768,7 +4768,7 @@ func TestExecSuccess(t *testing.T) {
}} }}
r4 := netstorage.Result{ r4 := netstorage.Result{
MetricName: metricNameExpected, MetricName: metricNameExpected,
Values: []float64{0.85, 0.94, 0.97, 0.93, 0.98, 0.92}, Values: []float64{0.9, 0.94, 0.97, 0.93, 0.98, 0.92},
Timestamps: timestampsExpected, Timestamps: timestampsExpected,
} }
r4.MetricName.Tags = []storage.Tag{{ r4.MetricName.Tags = []storage.Tag{{

View File

@ -1434,17 +1434,21 @@ func getCandlestickValues(rfa *rollupFuncArg) []float64 {
return rfa.values[:len(timestamps)] return rfa.values[:len(timestamps)]
} }
func getPrevValueForCandlestick(rfa *rollupFuncArg) float64 { func getFirstValueForCandlestick(rfa *rollupFuncArg) float64 {
if rfa.prevTimestamp+rfa.step == rfa.currTimestamp { if rfa.prevTimestamp+rfa.step >= rfa.currTimestamp {
return rfa.prevValue return rfa.prevValue
} }
return nan return nan
} }
func rollupOpen(rfa *rollupFuncArg) float64 { func rollupOpen(rfa *rollupFuncArg) float64 {
v := getFirstValueForCandlestick(rfa)
if !math.IsNaN(v) {
return v
}
values := getCandlestickValues(rfa) values := getCandlestickValues(rfa)
if len(values) == 0 { if len(values) == 0 {
return getPrevValueForCandlestick(rfa) return nan
} }
return values[0] return values[0]
} }
@ -1452,18 +1456,22 @@ func rollupOpen(rfa *rollupFuncArg) float64 {
func rollupClose(rfa *rollupFuncArg) float64 { func rollupClose(rfa *rollupFuncArg) float64 {
values := getCandlestickValues(rfa) values := getCandlestickValues(rfa)
if len(values) == 0 { if len(values) == 0 {
return getPrevValueForCandlestick(rfa) return getFirstValueForCandlestick(rfa)
} }
return values[len(values)-1] return values[len(values)-1]
} }
func rollupHigh(rfa *rollupFuncArg) float64 { func rollupHigh(rfa *rollupFuncArg) float64 {
values := getCandlestickValues(rfa) values := getCandlestickValues(rfa)
if len(values) == 0 { max := getFirstValueForCandlestick(rfa)
return getPrevValueForCandlestick(rfa) if math.IsNaN(max) {
if len(values) == 0 {
return nan
}
max = values[0]
values = values[1:]
} }
max := values[0] for _, v := range values {
for _, v := range values[1:] {
if v > max { if v > max {
max = v max = v
} }
@ -1473,11 +1481,15 @@ func rollupHigh(rfa *rollupFuncArg) float64 {
func rollupLow(rfa *rollupFuncArg) float64 { func rollupLow(rfa *rollupFuncArg) float64 {
values := getCandlestickValues(rfa) values := getCandlestickValues(rfa)
if len(values) == 0 { min := getFirstValueForCandlestick(rfa)
return getPrevValueForCandlestick(rfa) if math.IsNaN(min) {
if len(values) == 0 {
return nan
}
min = values[0]
values = values[1:]
} }
min := values[0] for _, v := range values {
for _, v := range values[1:] {
if v < min { if v < min {
min = v min = v
} }