From 9c35c15d7ddb3bd6e0e66512abdedc23f7fd9e25 Mon Sep 17 00:00:00 2001 From: Marco Liberati Date: Wed, 21 Aug 2024 16:36:27 +0200 Subject: [PATCH] [Lens] Fix no data collapse by scenario with Metric chart (#190966) ## Summary Fixes #182628 This PR provides a fix for the editor crash on no data when using collapsing by. In addition the rendering of no data in such scenario now matches the no data rendering with a single tile. Screenshot 2024-08-21 at 12 57 43 Screenshot 2024-08-21 at 12 57 28 Screenshot 2024-08-21 at 12 56 31 ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- .../public/components/metric_vis.test.tsx | 24 ++++++++++++------- .../public/components/metric_vis.tsx | 11 +++++++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.test.tsx b/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.test.tsx index 3687113d9aee0..7311adee7a202 100644 --- a/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.test.tsx +++ b/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.test.tsx @@ -428,14 +428,12 @@ describe('MetricVisComponent', function () { const [[visConfig]] = component.find(Metric).props().data!; - expect(visConfig!.value).toMatchInlineSnapshot( - ` + expect(visConfig!.value).toMatchInlineSnapshot(` Array [ "text-28.984375", "text-100", ] - ` - ); + `); }); it('should display multi-values numeric values formatted and without quotes', () => { @@ -451,14 +449,24 @@ describe('MetricVisComponent', function () { const [[visConfig]] = component.find(Metric).props().data!; - expect(visConfig!.value).toMatchInlineSnapshot( - ` + expect(visConfig!.value).toMatchInlineSnapshot(` Array [ "number-28.984375", "number-100", ] - ` - ); + `); + }); + + it('should display an empty tile if no data is provided', () => { + const newTable = { + ...table, + rows: [], + }; + const component = shallow(); + + const [[visConfig]] = component.find(Metric).props().data!; + + expect(visConfig!.value).toMatchInlineSnapshot(`NaN`); }); }); diff --git a/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.tsx b/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.tsx index 4ed15f5f72554..d0f6b6f3399e6 100644 --- a/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.tsx +++ b/src/plugins/chart_expressions/expression_metric/public/components/metric_vis.tsx @@ -163,7 +163,7 @@ export const MetricVis = ({ ); const onWillRender = useCallback(() => { - const maxTileSideLength = grid.current.length * grid.current[0].length > 1 ? 200 : 300; + const maxTileSideLength = grid.current.length * grid.current[0]?.length > 1 ? 200 : 300; const event: ChartSizeEvent = { name: 'chartSize', data: { @@ -197,8 +197,15 @@ export const MetricVis = ({ ? getColumnByAccessor(config.dimensions.max, data.columns)?.id : undefined; + // For a sigle tile configuration, either no breakdown or with a collapse by, provide + // a fallback in case of missing data. Make sure to provide an exact "null" value to render a N/A metric. + // For reference, undefined will render as - instead of N/A and it is used in a breakdown scenario + const firstRowForNonBreakdown = ( + data.rows.length ? data.rows : [{ [primaryMetricColumn.id]: null }] + ).slice(0, 1); + const metricConfigs: MetricSpec['data'][number] = ( - breakdownByColumn ? data.rows : data.rows.slice(0, 1) + breakdownByColumn ? data.rows : firstRowForNonBreakdown ).map((row, rowIdx) => { const value: number | string = row[primaryMetricColumn.id] !== null ? row[primaryMetricColumn.id] : NaN;