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:
@@ -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 () => {
|
it('throws error when calculating moving average if window size < 1', async () => {
|
||||||
const cfg = {
|
const cfg = {
|
||||||
id: DataTransformerID.calculateField,
|
id: DataTransformerID.calculateField,
|
||||||
|
|||||||
@@ -357,8 +357,11 @@ function getTrailingWindowValues(frame: DataFrame, reducer: ReducerID, selectedF
|
|||||||
sum += currentValue;
|
sum += currentValue;
|
||||||
|
|
||||||
if (i > window - 1) {
|
if (i > window - 1) {
|
||||||
sum -= selectedField.values[i - window];
|
const value = selectedField.values[i - window];
|
||||||
count--;
|
if (value != null) {
|
||||||
|
sum -= value;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vals.push(count === 0 ? 0 : sum / count);
|
vals.push(count === 0 ? 0 : sum / count);
|
||||||
|
|||||||
Reference in New Issue
Block a user