mirror of https://github.com/grafana/grafana.git
Transformations: Window calculation should use 0 if value in window does not exist (#95625)
* Use 0 for window functions if value does not exist * Simplify logic, add test * Fix tests, have first value count as 0 if null as well * evaluate entire array instead of individual values * Adjust logic, move prior results back
This commit is contained in:
parent
68db1c6e68
commit
9f02acd4ca
|
|
@ -728,6 +728,35 @@ describe('calculateField transformer w/ timeseries', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('calculates fixed, trailing moving average with missing values', async () => {
|
||||
const cfg = {
|
||||
id: DataTransformerID.calculateField,
|
||||
options: {
|
||||
mode: CalculateFieldMode.WindowFunctions,
|
||||
window: {
|
||||
windowAlignment: WindowAlignment.Trailing,
|
||||
field: 'x',
|
||||
windowSize: 2,
|
||||
windowSizeMode: WindowSizeMode.Fixed,
|
||||
reducer: ReducerID.mean,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const series = toDataFrame({
|
||||
fields: [{ name: 'x', type: FieldType.number, values: [1, undefined, 3, 4, 5] }],
|
||||
});
|
||||
|
||||
await expect(transformDataFrame([cfg], [series])).toEmitValuesWith((received) => {
|
||||
const data = received[0][0];
|
||||
|
||||
//console.log(data.fields);
|
||||
|
||||
expect(data.fields.length).toEqual(2);
|
||||
expect(data.fields[1].values).toEqual([1, 1, 3, 3.5, 4.5]);
|
||||
});
|
||||
});
|
||||
|
||||
it('throws error when calculating moving average if window size < 1', async () => {
|
||||
const cfg = {
|
||||
id: DataTransformerID.calculateField,
|
||||
|
|
|
|||
|
|
@ -357,8 +357,11 @@ function getTrailingWindowValues(frame: DataFrame, reducer: ReducerID, selectedF
|
|||
sum += currentValue;
|
||||
|
||||
if (i > window - 1) {
|
||||
sum -= selectedField.values[i - window];
|
||||
count--;
|
||||
const value = selectedField.values[i - window];
|
||||
if (value != null) {
|
||||
sum -= value;
|
||||
count--;
|
||||
}
|
||||
}
|
||||
}
|
||||
vals.push(count === 0 ? 0 : sum / count);
|
||||
|
|
|
|||
Loading…
Reference in New Issue