Skip to content

Commit

Permalink
make base id check more stable (#123367)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Jan 24, 2022
1 parent 2284470 commit 4ec7f08
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/plugins/data/common/search/aggs/agg_configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }];
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/data/common/search/aggs/agg_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 4ec7f08

Please sign in to comment.