From 4ec7f08c64d39880373fa4629a28be063498696a Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Mon, 24 Jan 2022 17:24:11 +0100 Subject: [PATCH] make base id check more stable (#123367) --- .../common/search/aggs/agg_configs.test.ts | 36 +++++++++++++++++++ .../data/common/search/aggs/agg_configs.ts | 9 ++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/plugins/data/common/search/aggs/agg_configs.test.ts b/src/plugins/data/common/search/aggs/agg_configs.test.ts index 80e5a079cfd59..b70b057fa6e79 100644 --- a/src/plugins/data/common/search/aggs/agg_configs.test.ts +++ b/src/plugins/data/common/search/aggs/agg_configs.test.ts @@ -226,6 +226,42 @@ describe('AggConfigs', () => { }); }); + describe('#getResponseAggById', () => { + it('returns aggs by matching id without confusing prefixes', () => { + const configStates = [ + { id: '1', type: 'terms', enabled: true, params: {}, schema: 'split' }, + { id: '10', type: 'date_histogram', enabled: true, params: {}, schema: 'segment' }, + { id: '101', type: 'count', enabled: true, params: {}, schema: 'metric' }, + ]; + + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry }); + expect(ac.getResponseAggById('1')?.type.name).toEqual('terms'); + expect(ac.getResponseAggById('10')?.type.name).toEqual('date_histogram'); + expect(ac.getResponseAggById('101')?.type.name).toEqual('count'); + }); + + it('returns right agg for id within a multi-value agg', () => { + const configStates = [ + { id: '1', type: 'terms', enabled: true, params: {}, schema: 'split' }, + { id: '10', type: 'date_histogram', enabled: true, params: {}, schema: 'segment' }, + { + id: '101', + type: 'percentiles', + enabled: true, + params: { percents: [1, 10, 3.33] }, + schema: 'metric', + }, + ]; + + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry }); + expect(ac.getResponseAggById('1')?.type.name).toEqual('terms'); + expect(ac.getResponseAggById('10')?.type.name).toEqual('date_histogram'); + expect(ac.getResponseAggById('101.1')?.type.name).toEqual('percentiles'); + expect(ac.getResponseAggById('101.10')?.type.name).toEqual('percentiles'); + expect(ac.getResponseAggById("101['3.33']")?.type.name).toEqual('percentiles'); + }); + }); + describe('#toDsl', () => { it('uses the sorted aggs', () => { const configStates = [{ enabled: true, type: 'avg', params: { field: 'bytes' } }]; diff --git a/src/plugins/data/common/search/aggs/agg_configs.ts b/src/plugins/data/common/search/aggs/agg_configs.ts index d221273edcb7f..e1af4bb7cd771 100644 --- a/src/plugins/data/common/search/aggs/agg_configs.ts +++ b/src/plugins/data/common/search/aggs/agg_configs.ts @@ -473,7 +473,14 @@ export class AggConfigs { getResponseAggById(id: string): AggConfig | undefined { id = String(id); const reqAgg = _.find(this.getRequestAggs(), function (agg: AggConfig) { - return id.substr(0, String(agg.id).length) === agg.id; + const aggId = String(agg.id); + // only multi-value aggs like percentiles are allowed to contain dots and [ + const isMultiValueId = id.includes('[') || id.includes('.'); + if (!isMultiValueId) { + return id === aggId; + } + const baseId = id.substring(0, id.indexOf('[') !== -1 ? id.indexOf('[') : id.indexOf('.')); + return baseId === aggId; }); if (!reqAgg) return; return _.find(reqAgg.getResponseAggs(), { id });