From 970dcdf723142c762e224a8821b3a8e1441de46e Mon Sep 17 00:00:00 2001 From: Josh Romero Date: Thu, 29 Dec 2022 21:20:59 +0000 Subject: [PATCH 1/2] [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 --- .../common/search/aggs/agg_configs.test.ts | 52 ++++++++++++++++++- .../data/common/search/aggs/agg_configs.ts | 10 ++-- 2 files changed, 58 insertions(+), 4 deletions(-) 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; From 1b6f69f2600269d73f54cf1d32e1a48ec2623996 Mon Sep 17 00:00:00 2001 From: Josh Romero Date: Thu, 29 Dec 2022 21:29:19 +0000 Subject: [PATCH 2/2] [Chore] Add changelog Signed-off-by: Josh Romero --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb4fd806d9e5..1be6108397eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Doc] Add current plugin persistence implementation readme ([#3081](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3081)) - [Table Visualization] Refactor table visualization using React and DataGrid component ([#2863](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2863)) - [Vis Builder] Add redux store persistence ([#3088](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3088)) +- [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