From 7754824ae472992704db0e076a896874873141e5 Mon Sep 17 00:00:00 2001 From: Josh Romero Date: Mon, 9 Jan 2023 11:21:54 -0800 Subject: [PATCH] [Feature] Allow order control in `createAggConfig` (#3160) * [Feature] Allow order control in `createAggConfig` Add new optional boolean param that adds the new `AggConfig` to the beginning of the array rather than the end. Makes it easier to work with Pie or other visualizations with `Schemas` that set `mustBeFirst` Signed-off-by: Josh Romero * [Chore] Add changelog Signed-off-by: Josh Romero Signed-off-by: Josh Romero Co-authored-by: Qingyang(Abby) Hu Signed-off-by: David Sinclair --- CHANGELOG.md | 2 + .../common/search/aggs/agg_configs.test.ts | 52 ++++++++++++++++++- .../data/common/search/aggs/agg_configs.ts | 10 ++-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e058276fb67..f8a6f0754f80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Multi DataSource] Improve test connection ([#3110](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3110)) - [Vis Builder] Add app filter and query persistence without using state container ([#3100](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3100)) - [Optimizer] Increase timeout waiting for the exiting of an optimizer worker ([#3193](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3193)) +- [Data] Update `createAggConfig` so that newly created configs can be added to beginning of `aggConfig` array ([#3160](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3160)) + ### 🐛 Bug Fixes 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 076fbd9de3d7..583c73a2724a 100644 --- a/src/plugins/data/common/search/aggs/agg_configs.test.ts +++ b/src/plugins/data/common/search/aggs/agg_configs.test.ts @@ -117,7 +117,7 @@ describe('AggConfigs', () => { expect(ac.aggs).toHaveLength(3); }); - it('adds new AggConfig entries to AggConfigs by default', () => { + it('adds new AggConfig entries to end of AggConfigs by default', () => { const configStates = [ { enabled: true, @@ -136,6 +136,7 @@ describe('AggConfigs', () => { schema: 'split', }); expect(ac.aggs).toHaveLength(2); + expect(ac.aggs[1].schema).toBe('split'); }); it('does not add an agg to AggConfigs if addToAggConfigs: false', () => { @@ -161,6 +162,55 @@ describe('AggConfigs', () => { ); expect(ac.aggs).toHaveLength(1); }); + + it('adds new AggConfig entries to beginning of AggConfigs if mustBeFirst: true', () => { + const configStates = [ + { + enabled: true, + type: 'histogram', + params: {}, + }, + ]; + + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry }); + expect(ac.aggs).toHaveLength(1); + + ac.createAggConfig( + { + enabled: true, + type: 'terms', + params: {}, + schema: 'split', + }, + { mustBeFirst: true } + ); + expect(ac.aggs).toHaveLength(2); + expect(ac.aggs[0].schema).toBe('split'); + }); + + it('does not add an agg to AggConfigs if addToAggConfigs: false and mustBeFirst: true', () => { + const configStates = [ + { + enabled: true, + type: 'histogram', + params: {}, + }, + ]; + + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry }); + expect(ac.aggs).toHaveLength(1); + + ac.createAggConfig( + { + enabled: true, + type: 'terms', + params: {}, + schema: 'split', + }, + { addToAggConfigs: false, mustBeFirst: true } + ); + expect(ac.aggs).toHaveLength(1); + }); }); describe('#getRequestAggs', () => { diff --git a/src/plugins/data/common/search/aggs/agg_configs.ts b/src/plugins/data/common/search/aggs/agg_configs.ts index 90ad5820ea3c..8f13fcd54f0e 100644 --- a/src/plugins/data/common/search/aggs/agg_configs.ts +++ b/src/plugins/data/common/search/aggs/agg_configs.ts @@ -130,10 +130,10 @@ export class AggConfigs { createAggConfig = ( params: CreateAggConfigParams, - { addToAggConfigs = true } = {} + { addToAggConfigs = true, mustBeFirst = false } = {} ) => { const { type } = params; - let aggConfig; + let aggConfig: AggConfig; if (params instanceof AggConfig) { aggConfig = params; @@ -145,8 +145,12 @@ export class AggConfigs { }); } + const addAggConfig = () => { + return mustBeFirst ? this.aggs.unshift(aggConfig) : this.aggs.push(aggConfig); + }; + if (addToAggConfigs) { - this.aggs.push(aggConfig); + addAggConfig(); } return aggConfig as T;