This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] VisType switching persistence and selectively show warning (ope…
…nsearch-project#3715) * Refactor persistnece for simpler logic Signed-off-by: Ashwin Pc <[email protected]> * renamed files Signed-off-by: Ashwin Pc <[email protected]> * only show warning when dropping fields Signed-off-by: Ashwin P Chandran <[email protected]> * delete unnecessary tests Signed-off-by: Ashwin P Chandran <[email protected]> * addresses PR feedback Signed-off-by: Ashwin P Chandran <[email protected]> * Fixes comparison --------- Signed-off-by: Ashwin Pc <[email protected]> Signed-off-by: Ashwin P Chandran <[email protected]> Signed-off-by: David Sinclair <[email protected]>
- Loading branch information
Showing
7 changed files
with
261 additions
and
488 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
src/plugins/vis_builder/public/application/utils/get_persisted_agg_params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, number> = 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.