[Lens][Aggs] Gracefully handle the case of missing extended_stats for std_deviation operation (#141248)

* 🐛 Make getValue return undefined if no extended_object is present

*  Add unit test
This commit is contained in:
Marco Liberati 2022-09-22 17:13:41 +02:00 committed by GitHub
parent 585d51444e
commit fa661a445b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View file

@ -13,7 +13,10 @@ import { METRIC_TYPES } from './metric_agg_types';
describe('AggTypeMetricStandardDeviationProvider class', () => {
const typesRegistry = mockAggTypesRegistry();
const getAggConfigs = (customLabel?: string) => {
const getAggConfigs = ({
customLabel,
showBounds,
}: { customLabel?: string; showBounds?: boolean } = {}) => {
const field = {
name: 'memory',
};
@ -38,6 +41,7 @@ describe('AggTypeMetricStandardDeviationProvider class', () => {
displayName: 'memory',
},
customLabel,
...(showBounds != null ? { showBounds } : {}),
},
},
],
@ -47,7 +51,7 @@ describe('AggTypeMetricStandardDeviationProvider class', () => {
};
it('uses the custom label if it is set', () => {
const aggConfigs = getAggConfigs('custom label');
const aggConfigs = getAggConfigs({ customLabel: 'custom label' });
const responseAggs: any = getStdDeviationMetricAgg().getResponseAggs(
aggConfigs.aggs[0] as IStdDevAggConfig
);
@ -102,4 +106,12 @@ describe('AggTypeMetricStandardDeviationProvider class', () => {
}
`);
});
it('returns null without throwing if no "extended_stats" is returned', () => {
const aggConfigs = getAggConfigs({ showBounds: false });
expect(() =>
getStdDeviationMetricAgg().getValue(aggConfigs.aggs[0] as IStdDevAggConfig, {})
).not.toThrow();
});
});

View file

@ -117,7 +117,7 @@ export const getStdDeviationMetricAgg = () => {
getValue(agg, bucket) {
const showBounds = agg.getParam('showBounds');
if (showBounds === false) {
return bucket[agg.id].std_deviation;
return bucket[agg.id]?.std_deviation;
}
return get(bucket[agg.parentId], agg.valProp());
},