From 20f541d7e759dd9c7683f37fd1394151cde280cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Erades?= Date: Wed, 15 Nov 2023 10:12:04 +0100 Subject: [PATCH] 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 --- .../timeSeriesTableTransformer.test.ts | 25 +++++++++++++++++++ .../timeSeriesTableTransformer.ts | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.test.ts b/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.test.ts index e7c66a676c9..d96cd98122f 100644 --- a/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.test.ts +++ b/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.test.ts @@ -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); }); }); diff --git a/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.ts b/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.ts index dfe99397e30..8205accb0c2 100644 --- a/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.ts +++ b/public/app/features/transformers/timeSeriesTable/timeSeriesTableTransformer.ts @@ -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