Skip to content

Commit

Permalink
fix agg config sub agg dsl generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Mar 2, 2021
1 parent cce6861 commit 9c88d02
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/plugins/data/common/search/aggs/agg_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,51 @@ describe('AggConfig', () => {
expect(dsl.aggs[avgConfig.id]).toHaveProperty('avg');
expect(dsl.aggs[avgConfig.id].avg).toBe(football);
});

it('merges subAggs from #write() output to the current subaggs', () => {
const configStates = [
{
enabled: true,
type: 'avg',
schema: 'metric',
params: {},
},
{
enabled: true,
type: 'median',
schema: 'metric',
params: {},
},
{
enabled: true,
type: 'date_histogram',
schema: 'segment',
params: {},
},
];
const ac = new AggConfigs(indexPattern, configStates, { typesRegistry });

const histoConfig = ac.byName('date_histogram')[0];
const avgConfig = ac.byName('avg')[0];
const medianConfig = ac.byName('median')[0];
const football = {};

jest
.spyOn(histoConfig, 'write')
.mockImplementation(() => ({ params: {}, subAggs: [avgConfig] }));
jest.spyOn(avgConfig, 'write').mockImplementation(() => ({ params: football }));
jest.spyOn(medianConfig, 'write').mockImplementation(() => ({ params: football }));

(histoConfig as any).subAggs = [medianConfig];
const dsl = histoConfig.toDsl();
expect(dsl).toHaveProperty('aggs');
expect(dsl.aggs).toHaveProperty(avgConfig.id);
expect(dsl.aggs[avgConfig.id]).toHaveProperty('avg');
expect(dsl.aggs[avgConfig.id].avg).toBe(football);
expect(dsl.aggs).toHaveProperty(medianConfig.id);
expect(dsl.aggs[medianConfig.id]).toHaveProperty('percentiles');
expect(dsl.aggs[medianConfig.id].percentiles).toBe(football);
});
});

describe('::ensureIds', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/data/common/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ export class AggConfig {
configDsl[this.type.dslName || this.type.name] = output.params;

// if the config requires subAggs, write them to the dsl as well
if (this.subAggs.length && !output.subAggs) output.subAggs = this.subAggs;
if (this.subAggs.length) {
if (!output.subAggs) output.subAggs = this.subAggs;
else output.subAggs.push(...this.subAggs);
}

if (output.subAggs) {
const subDslLvl = configDsl.aggs || (configDsl.aggs = {});
output.subAggs.forEach(function nestAdhocSubAggs(subAggConfig: any) {
Expand Down

0 comments on commit 9c88d02

Please sign in to comment.