Transformations: Fix Timeseries to table transformation trend reduction when result is 0 (#78026)

* Transformations: Fix Timeseries to table transformation trend reduction when result is 0

When reduceField function returns 0, the reduced value is set to null. I
think we can remove this "|| null" statement, as:
- There is a check a few lines above to make sure the field type is a number. This might be a reasonable guarantee that the calculation we are doing will make sense and there will be no need to set to null.
- Sparkline cell type already has an option to hide value if we believe the calculation
  does not make sense.

Fixes #78025

* make sure we check for undefined

---------

Co-authored-by: Oscar Kilhed <oscar.kilhed@grafana.com>
This commit is contained in:
Óscar Erades
2023-11-15 10:12:04 +01:00
committed by GitHub
parent b97e485fe0
commit 20f541d7e7
2 changed files with 26 additions and 1 deletions

View File

@ -78,6 +78,8 @@ describe('timeSeriesTableTransformer', () => {
const series = [
getTimeSeries('A', { instance: 'A', pod: 'B' }, [4, 2, 3]),
getTimeSeries('B', { instance: 'A', pod: 'C' }, [3, 4, 5]),
getTimeSeries('C', { instance: 'B', pod: 'X' }, [4, 2, 0]),
getTimeSeries('D', { instance: 'B', pod: 'Y' }, [0, 0, 0]),
];
const results = timeSeriesToTableTransform(
@ -85,12 +87,35 @@ describe('timeSeriesTableTransformer', () => {
B: {
stat: ReducerID.mean,
},
D: {
stat: ReducerID.mean,
},
},
series
);
expect(results[0].fields[2].values[0].value).toEqual(3);
expect(results[1].fields[2].values[0].value).toEqual(4);
expect(results[2].fields[2].values[0].value).toEqual(0);
expect(results[3].fields[2].values[0].value).toEqual(0);
});
it('calculate the value for an empty series to null', () => {
const series = [getTimeSeries('D', { instance: 'B', pod: 'Y' }, [])];
const results = timeSeriesToTableTransform(
{
B: {
stat: ReducerID.mean,
},
D: {
stat: ReducerID.mean,
},
},
series
);
expect(results[0].fields[2].values[0].value).toEqual(null);
});
});

View File

@ -172,7 +172,7 @@ export function timeSeriesToTableTransform(options: TimeSeriesTableTransformerOp
// and push the frame with reduction
// into the the appropriate field
const reducerId = options[refId]?.stat ?? ReducerID.lastNotNull;
const value = reduceField({ field, reducers: [reducerId] })[reducerId] || null;
const value = reduceField({ field, reducers: [reducerId] })[reducerId] ?? null;
// Push the appropriate time and value frame
// to the trend frame for the sparkline