From 540556112f50bd377821f25494f3276e6d3cdd98 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:42:33 -0700 Subject: [PATCH] [Fix] VisType switching persistence and selectively show warning (#3715) (#3760) * Refactor persistnece for simpler logic * renamed files * only show warning when dropping fields * delete unnecessary tests * addresses PR feedback * Fixes comparison --------- (cherry picked from commit 2db2c4314314c5d6453aa43586c8c04d2ff08d1a) Signed-off-by: Ashwin Pc Signed-off-by: Ashwin P Chandran Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../application/components/right_nav.tsx | 60 +-- .../utils/get_persisted_agg_params.test.tsx | 145 ++++++++ .../utils/get_persisted_agg_params.ts | 78 ++++ .../utils/state_management/shared_actions.ts | 2 +- .../use/use_persisted_agg_params.test.tsx | 352 ------------------ .../utils/use/use_persisted_agg_params.ts | 95 ----- test/functional/apps/vis_builder/_base.ts | 17 +- 7 files changed, 261 insertions(+), 488 deletions(-) create mode 100644 src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.test.tsx create mode 100644 src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.ts delete mode 100644 src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.test.tsx delete mode 100644 src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.ts diff --git a/src/plugins/vis_builder/public/application/components/right_nav.tsx b/src/plugins/vis_builder/public/application/components/right_nav.tsx index c98638da28f1..70b22d600ae2 100644 --- a/src/plugins/vis_builder/public/application/components/right_nav.tsx +++ b/src/plugins/vis_builder/public/application/components/right_nav.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { useState } from 'react'; +import React, { useCallback, useMemo, useState } from 'react'; import { EuiSuperSelect, EuiSuperSelectOption, @@ -18,28 +18,48 @@ import './side_nav.scss'; import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; import { VisBuilderServices } from '../../types'; import { + ActiveVisPayload, setActiveVisualization, useTypedDispatch, useTypedSelector, } from '../utils/state_management'; -import { usePersistedAggParams } from '../utils/use/use_persisted_agg_params'; +import { getPersistedAggParams } from '../utils/get_persisted_agg_params'; export const RightNav = () => { - const [newVisType, setNewVisType] = useState(); + const { ui, name: activeVisName } = useVisualizationType(); + const [confirmAggs, setConfirmAggs] = useState(); const { services: { types }, } = useOpenSearchDashboards(); - const { ui, name: activeVisName } = useVisualizationType(); const dispatch = useTypedDispatch(); const StyleSection = ui.containerConfig.style.render; const { activeVisualization } = useTypedSelector((state) => state.visualization); - const aggConfigParams = activeVisualization?.aggConfigParams ?? []; - const persistedAggParams = usePersistedAggParams( - types, - aggConfigParams, - activeVisName, - newVisType + const aggConfigParams = useMemo(() => activeVisualization?.aggConfigParams ?? [], [ + activeVisualization, + ]); + + const handleVisTypeChange = useCallback( + (newVisName) => { + const currentVisSchemas = types.get(activeVisName)?.ui.containerConfig.data.schemas.all ?? []; + const newVisSchemas = types.get(newVisName)?.ui.containerConfig.data.schemas.all ?? []; + const persistedAggParams = getPersistedAggParams( + aggConfigParams, + currentVisSchemas, + newVisSchemas + ); + + const newVis = { + name: newVisName, + aggConfigParams: persistedAggParams, + style: types.get(newVisName)?.ui.containerConfig.style.defaults, + }; + + if (persistedAggParams.length < aggConfigParams.length) return setConfirmAggs(newVis); + + dispatch(setActiveVisualization(newVis)); + }, + [activeVisName, aggConfigParams, dispatch, types] ); const options: Array> = types.all().map(({ name, icon, title }) => ({ @@ -55,9 +75,7 @@ export const RightNav = () => { { - setNewVisType(name); - }} + onChange={handleVisTypeChange} fullWidth data-test-subj="chartPicker" /> @@ -65,7 +83,7 @@ export const RightNav = () => {
- {newVisType && ( + {confirmAggs && ( { cancelButtonText={i18n.translate('visBuilder.rightNav.changeVisType.cancelText', { defaultMessage: 'Cancel', })} - onCancel={() => setNewVisType(undefined)} + onCancel={() => setConfirmAggs(undefined)} onConfirm={() => { - dispatch( - setActiveVisualization({ - name: newVisType, - style: types.get(newVisType)?.ui.containerConfig.style.defaults, - aggConfigParams: persistedAggParams, - }) - ); + dispatch(setActiveVisualization(confirmAggs)); - setNewVisType(undefined); + setConfirmAggs(undefined); }} maxWidth="300px" data-test-subj="confirmVisChangeModal" @@ -94,7 +106,7 @@ export const RightNav = () => {

diff --git a/src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.test.tsx b/src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.test.tsx new file mode 100644 index 000000000000..0b9c0ec8e745 --- /dev/null +++ b/src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.test.tsx @@ -0,0 +1,145 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { AggGroupName, AggGroupNames, CreateAggConfigParams } from '../../../../data/common'; +import { Schema } from '../../../../vis_default_editor/public'; +import { getPersistedAggParams } from './get_persisted_agg_params'; + +describe('getPersistedAggParams', () => { + const getSchema = ( + name: string, + group: AggGroupName, + aggFilter: string[] = ['*'], + max: number = Infinity + ): Schema => ({ + name, + group, + max, + min: 0, + aggFilter, + defaults: [], + editor: true, + params: [], + title: name, + }); + + test('Should return the same aggConfigParams when the new vis type schemas have the same schemas as the existing schemas', () => { + const aggConfigParams: CreateAggConfigParams[] = [ + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'm2' }, + { type: 'avg', schema: 'b2' }, + { type: 'avg', schema: 'b2' }, + ]; + + const schemas = [ + getSchema('m1', AggGroupNames.Metrics), + getSchema('m2', AggGroupNames.Metrics), + getSchema('b2', AggGroupNames.Buckets), + ]; + + const persistResult = getPersistedAggParams(aggConfigParams, schemas, schemas); + + expect(persistResult).toEqual(aggConfigParams); + }); + + test('Should select the next compatible schema when aggConfigParam schema exists but exceeds the max count of the schema', () => { + const aggConfigParams: CreateAggConfigParams[] = [ + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'b2' }, + { type: 'avg', schema: 'b2' }, + ]; + + const schemas = [ + getSchema('m1', AggGroupNames.Metrics, ['avg'], 1), + getSchema('m2', AggGroupNames.Metrics), + getSchema('b2', AggGroupNames.Buckets), + ]; + + const persistResult = getPersistedAggParams(aggConfigParams, schemas, schemas); + + const expected: CreateAggConfigParams[] = [...aggConfigParams]; + expected[1].schema = 'm2'; + expect(persistResult).toEqual(expected); + }); + + test('Should select the next compatible schema when aggConfigParam schema exists but does not match the group in the new schema', () => { + const aggConfigParams: CreateAggConfigParams[] = [ + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'b1' }, + { type: 'avg', schema: 'b1' }, + ]; + + const oldSchemas = [ + getSchema('m1', AggGroupNames.Metrics), + getSchema('b1', AggGroupNames.Buckets), + ]; + + const newSchemas = [ + getSchema('m1', AggGroupNames.Buckets), + getSchema('m2', AggGroupNames.Metrics), + getSchema('b1', AggGroupNames.Buckets), + ]; + + const persistResult = getPersistedAggParams(aggConfigParams, oldSchemas, newSchemas); + + const expected: CreateAggConfigParams[] = [...aggConfigParams]; + expected[0].schema = 'm2'; + expected[1].schema = 'm2'; + expect(persistResult).toEqual(expected); + }); + + test('Should select the next compatible schema with the correct aggfilters', () => { + const aggConfigParams: CreateAggConfigParams[] = [ + { type: 'count', schema: 'm1' }, + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'b1' }, + { type: 'avg', schema: 'b1' }, + ]; + + const oldSchemas = [ + getSchema('m1', AggGroupNames.Metrics), + getSchema('b1', AggGroupNames.Buckets), + ]; + + const newSchemas = [ + getSchema('m2', AggGroupNames.Metrics, ['count']), + getSchema('m3', AggGroupNames.Metrics, ['!count']), + getSchema('b1', AggGroupNames.Buckets), + ]; + + const persistResult = getPersistedAggParams(aggConfigParams, oldSchemas, newSchemas); + + const expected: CreateAggConfigParams[] = [...aggConfigParams]; + expected[0].schema = 'm2'; + expected[1].schema = 'm3'; + expect(persistResult).toEqual(expected); + }); + + test('Should drop aggConfigParam when no compatible schema is found', () => { + const aggConfigParams: CreateAggConfigParams[] = [ + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'm1' }, + { type: 'avg', schema: 'b1' }, + { type: 'avg', schema: 'b1' }, + ]; + + const oldSchemas = [ + getSchema('m1', AggGroupNames.Metrics), + getSchema('b1', AggGroupNames.Buckets), + ]; + + const newSchemas = [ + getSchema('m2', AggGroupNames.Metrics, ['count']), + getSchema('m3', AggGroupNames.Metrics, ['!count'], 1), + getSchema('b1', AggGroupNames.Buckets), + ]; + + const persistResult = getPersistedAggParams(aggConfigParams, oldSchemas, newSchemas); + + expect(persistResult.length).toBe(3); + }); +}); diff --git a/src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.ts b/src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.ts new file mode 100644 index 000000000000..8d0f5369a7d0 --- /dev/null +++ b/src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.ts @@ -0,0 +1,78 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { CreateAggConfigParams, propFilter } from '../../../../data/common'; +import { Schema } from '../../../../vis_default_editor/public'; + +const filterByType = propFilter('type'); + +export const getPersistedAggParams = ( + aggConfigParams: CreateAggConfigParams[], + oldVisSchemas: Schema[] = [], + newVisSchemas: Schema[] = [] +): CreateAggConfigParams[] => { + const updatedAggConfigParams: CreateAggConfigParams[] = []; + const newVisSchemaCounts: Record = newVisSchemas.reduce((acc, schema: Schema) => { + acc[schema.name] = schema.max; + return acc; + }, {}); + + // For each aggConfigParam, check if a compatible schema exists in the new visualization type + aggConfigParams.forEach((aggConfigParam) => { + const currentSchema = oldVisSchemas.find((schema: Schema) => { + return schema.name === aggConfigParam.schema; + }); + + // see if a matching schma exists in the new visualization type + const matchingSchema = newVisSchemas.find((schema: Schema) => { + return schema.name === aggConfigParam.schema; + }); + + // if the matching schema is same as the current schema, add the aggConfigParam to the updatedAggConfigParams + if ( + isSchemaEqual(matchingSchema, currentSchema) && + newVisSchemaCounts[matchingSchema!.name] > 0 + ) { + updatedAggConfigParams.push(aggConfigParam); + newVisSchemaCounts[matchingSchema!.name] -= 1; + return; + } + + // if a matching schema does not exist, check if a compatible schema exists + for (const schema of newVisSchemas) { + // Check if the schema group is the same + if (schema.group !== currentSchema!.group) continue; + + const compatibleSchema = filterByType([aggConfigParam], schema.aggFilter).length !== 0; + + if (compatibleSchema && newVisSchemaCounts[schema.name] > 0) { + updatedAggConfigParams.push({ + ...aggConfigParam, + schema: schema.name, + }); + newVisSchemaCounts[schema.name] -= 1; + break; + } + } + }); + + return updatedAggConfigParams; +}; + +function isSchemaEqual(schema1?: Schema, schema2?: Schema) { + // Check if schema1 and schema2 exist + if (!schema1 || !schema2) return false; + + if (schema1.name !== schema2.name) return false; + if (schema1.group !== schema2.group) return false; + + // Check if aggFilter is the same + if (schema1.aggFilter.length !== schema2.aggFilter.length) return false; + for (let i = 0; i < schema1.aggFilter.length; i++) { + if (schema1.aggFilter[i] !== schema2.aggFilter[i]) return false; + } + + return true; +} diff --git a/src/plugins/vis_builder/public/application/utils/state_management/shared_actions.ts b/src/plugins/vis_builder/public/application/utils/state_management/shared_actions.ts index ebff4f91803c..be6cf7e4f41b 100644 --- a/src/plugins/vis_builder/public/application/utils/state_management/shared_actions.ts +++ b/src/plugins/vis_builder/public/application/utils/state_management/shared_actions.ts @@ -7,7 +7,7 @@ import { createAction } from '@reduxjs/toolkit'; import { CreateAggConfigParams } from '../../../../../data/common'; import { VisualizationType } from '../../../services/type_service/visualization_type'; -interface ActiveVisPayload { +export interface ActiveVisPayload { name: VisualizationType['name']; style: VisualizationType['ui']['containerConfig']['style']['defaults']; aggConfigParams: CreateAggConfigParams[]; diff --git a/src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.test.tsx b/src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.test.tsx deleted file mode 100644 index 6011ac33f71f..000000000000 --- a/src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.test.tsx +++ /dev/null @@ -1,352 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { CreateAggConfigParams } from '../../../../../data/common'; -import { AggGroupNames } from '../../../../../data/common'; -import { Schema } from '../../../../../vis_default_editor/public'; -import { VisBuilderServices } from '../../../types'; -import { createVisBuilderServicesMock } from '../mocks'; -import { - AggMapping, - getSchemaMapping, - updateAggParams, - usePersistedAggParams, -} from './use_persisted_agg_params'; - -describe('new usePersistedAggParams', () => { - const types = { - get: jest.fn(), - }; - let schemaMetricTemplate: Schema; - let schemaBucketTemplate: Schema; - let oldVisualizationType: Schema[]; - let newVisualizationType: Schema[]; - let aggConfigParam: CreateAggConfigParams; - let aggConfigParams: CreateAggConfigParams[]; - - beforeEach(() => { - schemaMetricTemplate = { - aggFilter: [], - editor: '', - group: AggGroupNames.Metrics, - max: 1, - min: 1, - name: '', - params: [], - title: '', - defaults: '', - }; - schemaBucketTemplate = { - aggFilter: [], - editor: '', - group: AggGroupNames.Buckets, - max: 1, - min: 1, - name: '', - params: [], - title: '', - defaults: '', - }; - aggConfigParam = { - type: '', - schema: '', - }; - }); - - test('return the correct metric-to-metric, bucket-to-bucket mapping and correct persisted aggregations', () => { - oldVisualizationType = [ - { ...schemaMetricTemplate, name: 'old metric 1' }, - { ...schemaBucketTemplate, name: 'old bucket 1' }, - { ...schemaMetricTemplate, name: 'old metric 2' }, - { ...schemaBucketTemplate, name: 'old bucket 2' }, - { ...schemaBucketTemplate, name: 'old bucket 3' }, - ]; - newVisualizationType = [ - { ...schemaMetricTemplate, name: 'new metric 1', max: 1 }, - { ...schemaMetricTemplate, name: 'new metric 2', max: 2 }, - { ...schemaBucketTemplate, name: 'new bucket 1', max: 4 }, - { ...schemaBucketTemplate, name: 'new bucket 2', max: 5 }, - { ...schemaBucketTemplate, name: 'new bucket 3', max: 6 }, - ]; - types.get - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: oldVisualizationType, - }, - }, - }, - }, - }) - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: newVisualizationType, - }, - }, - }, - }, - }); - aggConfigParams = [ - { ...aggConfigParam, schema: 'old metric 1' }, - { ...aggConfigParam, schema: 'old bucket 1' }, - { ...aggConfigParam, schema: 'old metric 2' }, - { ...aggConfigParam, schema: 'old bucket 2' }, - { ...aggConfigParam, schema: 'old bucket 3' }, - { ...aggConfigParam, schema: 'old metric 2' }, - ]; - - const mappingResult = getSchemaMapping(oldVisualizationType, newVisualizationType); - expect(mappingResult).toMatchInlineSnapshot(` - Map { - "old metric 1" => Object { - "currentCount": 0, - "maxCount": 1, - "name": "new metric 1", - }, - "old metric 2" => Object { - "currentCount": 0, - "maxCount": 2, - "name": "new metric 2", - }, - "old bucket 1" => Object { - "currentCount": 0, - "maxCount": 4, - "name": "new bucket 1", - }, - "old bucket 2" => Object { - "currentCount": 0, - "maxCount": 5, - "name": "new bucket 2", - }, - "old bucket 3" => Object { - "currentCount": 0, - "maxCount": 6, - "name": "new bucket 3", - }, - } - `); - const persistResult = usePersistedAggParams( - types, - aggConfigParams, - 'old vis type', - 'new vis type' - ); - expect(persistResult).toMatchInlineSnapshot(` - Array [ - Object { - "schema": "new metric 1", - "type": "", - }, - Object { - "schema": "new bucket 1", - "type": "", - }, - Object { - "schema": "new metric 2", - "type": "", - }, - Object { - "schema": "new bucket 2", - "type": "", - }, - Object { - "schema": "new bucket 3", - "type": "", - }, - Object { - "schema": "new metric 2", - "type": "", - }, - ] - `); - }); - - test('drop the schema fields when it can not be mapped or do not belong to either metric or bucket group', () => { - oldVisualizationType = [ - { ...schemaMetricTemplate, name: 'old metric 1' }, - { ...schemaMetricTemplate, name: 'old metric 2' }, - { ...schemaBucketTemplate, name: 'old bucket 1' }, - { ...schemaMetricTemplate, name: 'undefined group', group: AggGroupNames.None }, - ]; - newVisualizationType = [ - { ...schemaMetricTemplate, name: 'new metric 1', max: 1 }, - { ...schemaBucketTemplate, name: 'new bucket 2', max: 1 }, - ]; - types.get - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: oldVisualizationType, - }, - }, - }, - }, - }) - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: newVisualizationType, - }, - }, - }, - }, - }); - aggConfigParams = [ - { ...aggConfigParam, schema: 'old metric 1' }, - { ...aggConfigParam, schema: 'old bucket 1' }, - ]; - - const mappingResult = getSchemaMapping(oldVisualizationType, newVisualizationType); - expect(mappingResult).toMatchInlineSnapshot(` - Map { - "old metric 1" => Object { - "currentCount": 0, - "maxCount": 1, - "name": "new metric 1", - }, - "old bucket 1" => Object { - "currentCount": 0, - "maxCount": 1, - "name": "new bucket 2", - }, - } - `); - const persistResult = usePersistedAggParams( - types, - aggConfigParams, - 'old vis type', - 'new vis type' - ); - expect(persistResult).toMatchInlineSnapshot(` - Array [ - Object { - "schema": "new metric 1", - "type": "", - }, - Object { - "schema": "new bucket 2", - "type": "", - }, - ] - `); - }); - - test('aggregations with undefined schema remain undefined; schema will be set to undefined if aggregations that exceeds the max amount', () => { - oldVisualizationType = [{ ...schemaMetricTemplate, name: 'old metric 1' }]; - newVisualizationType = [{ ...schemaMetricTemplate, name: 'new metric 1', max: 1 }]; - types.get - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: oldVisualizationType, - }, - }, - }, - }, - }) - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: newVisualizationType, - }, - }, - }, - }, - }); - aggConfigParams = [ - { ...aggConfigParam, schema: undefined }, - { ...aggConfigParam, schema: 'old metric 1' }, - { ...aggConfigParam, schema: 'old metric 1' }, - ]; - - const mappingResult = getSchemaMapping(oldVisualizationType, newVisualizationType); - expect(mappingResult).toMatchInlineSnapshot(` - Map { - "old metric 1" => Object { - "currentCount": 0, - "maxCount": 1, - "name": "new metric 1", - }, - } - `); - const persistResult = usePersistedAggParams( - types, - aggConfigParams, - 'old vis type', - 'new vis type' - ); - expect(persistResult).toMatchInlineSnapshot(` - Array [ - Object { - "schema": undefined, - "type": "", - }, - Object { - "schema": "new metric 1", - "type": "", - }, - Object { - "schema": undefined, - "type": "", - }, - ] - `); - }); - - test('return an empty array when there are no aggregations for persistence', () => { - oldVisualizationType = [{ ...schemaMetricTemplate, name: 'old metric 1' }]; - newVisualizationType = [{ ...schemaMetricTemplate, name: 'new metric 1', max: 1 }]; - types.get - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: oldVisualizationType, - }, - }, - }, - }, - }) - .mockReturnValueOnce({ - ui: { - containerConfig: { - data: { - schemas: { - all: newVisualizationType, - }, - }, - }, - }, - }); - - aggConfigParams = []; - const persistResult = usePersistedAggParams( - types, - aggConfigParams, - 'old vis type', - 'new vis type' - ); - expect(persistResult).toMatchInlineSnapshot(`Array []`); - }); - - test('return an empty array when there are no new vis type or old vis type', () => { - const persistResult = usePersistedAggParams(types, aggConfigParams); - expect(persistResult).toMatchInlineSnapshot(`Array []`); - }); -}); diff --git a/src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.ts b/src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.ts deleted file mode 100644 index 980c5f3e9f38..000000000000 --- a/src/plugins/vis_builder/public/application/utils/use/use_persisted_agg_params.ts +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { AggGroupNames, CreateAggConfigParams } from '../../../../../data/common'; -import { Schema } from '../../../../../vis_default_editor/public'; - -export const usePersistedAggParams = ( - types, - aggConfigParams: CreateAggConfigParams[], - oldVisType?: string, - newVisType?: string -): CreateAggConfigParams[] => { - if (oldVisType && newVisType) { - const oldVisualizationType = types.get(oldVisType)?.ui.containerConfig.data.schemas.all; - const newVisualizationType = types.get(newVisType)?.ui.containerConfig.data.schemas.all; - const aggMapping = getSchemaMapping(oldVisualizationType, newVisualizationType); - const updatedAggConfigParams = aggConfigParams.map((aggConfigParam: CreateAggConfigParams) => - updateAggParams(aggConfigParam, aggMapping) - ); - return updatedAggConfigParams; - } - return []; -}; - -// Map metric fields to metric fields, bucket fields to bucket fields -export const getSchemaMapping = ( - oldVisualizationType: Schema[], - newVisualizationType: Schema[] -): Map => { - const aggMap = new Map(); - - // currently Metrics, Buckets, and None are the three groups. We simply drop the aggregations that belongs to the None group - mapAggParamsSchema(oldVisualizationType, newVisualizationType, AggGroupNames.Metrics, aggMap); - mapAggParamsSchema(oldVisualizationType, newVisualizationType, AggGroupNames.Buckets, aggMap); - - return aggMap; -}; - -export interface AggMapping { - name: string; - maxCount: number; - currentCount: number; -} - -export const mapAggParamsSchema = ( - oldVisualizationType: Schema[], - newVisualizationType: Schema[], - aggGroup: string, - map: Map -) => { - const oldSchemas = oldVisualizationType.filter((type) => type.group === aggGroup); - const newSchemas = newVisualizationType.filter((type) => type.group === aggGroup); - - oldSchemas.forEach((oldSchema, index) => { - if (newSchemas[index]) { - const mappedNewSchema = { - name: newSchemas[index].name, - maxCount: newSchemas[index].max, - currentCount: 0, - }; - map.set(oldSchema.name, mappedNewSchema); - } - }); -}; - -export const updateAggParams = ( - oldAggParam: CreateAggConfigParams, - aggMap: Map -) => { - const newAggParam = { ...oldAggParam }; - if (oldAggParam.schema) { - const newSchema = aggMap.get(oldAggParam.schema); - newAggParam.schema = newSchema - ? newSchema.currentCount < newSchema.maxCount - ? assignNewSchemaType(oldAggParam, aggMap, newSchema) - : undefined - : undefined; - } - return newAggParam; -}; - -export const assignNewSchemaType = ( - oldAggParam: any, - aggMap: Map, - newSchema: AggMapping -) => { - aggMap.set(oldAggParam.schema, { - name: newSchema.name, - maxCount: newSchema.maxCount, - currentCount: newSchema.currentCount + 1, - }); - return aggMap.get(oldAggParam.schema)?.name; -}; diff --git a/test/functional/apps/vis_builder/_base.ts b/test/functional/apps/vis_builder/_base.ts index 60dd1068347c..98d7efcf3d6c 100644 --- a/test/functional/apps/vis_builder/_base.ts +++ b/test/functional/apps/vis_builder/_base.ts @@ -6,11 +6,10 @@ import expect from '@osd/expect'; import { FtrProviderContext } from '../../ftr_provider_context'; +// TODO: Remove selenium functional tests since cypress tests exist export default function ({ getService, getPageObjects }: FtrProviderContext) { const PageObjects = getPageObjects(['visualize', 'visBuilder', 'visChart']); - const testSubjects = getService('testSubjects'); const log = getService('log'); - const retry = getService('retry'); describe('Basic tests for visBuilder app ', function () { before(async () => { @@ -42,19 +41,5 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const isEmptyWorkspace = await PageObjects.visBuilder.isEmptyWorkspace(); expect(isEmptyWorkspace).to.be(true); }); - - it('should show warning before changing visualization type', async () => { - await PageObjects.visBuilder.selectVisType('metric', false); - const confirmModalExists = await testSubjects.exists('confirmVisChangeModal'); - expect(confirmModalExists).to.be(true); - - await testSubjects.click('confirmModalCancelButton'); - }); - - it('should change visualization type', async () => { - const pickerValue = await PageObjects.visBuilder.selectVisType('metric'); - - expect(pickerValue).to.eql('Metric'); - }); }); }