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.
feat: filter out ADMIN application and add feature dependency logic (o…
…pensearch-project#49) * feat: filter out ADMIN application and add feature dependency logic Signed-off-by: Lin Wang <[email protected]> * feat: separate feature utils function Signed-off-by: Lin Wang <[email protected]> * feat: rename isFeatureDependBySelectedFeatures, separate generateFeatureDependencyMap and add annotation Signed-off-by: Lin Wang <[email protected]> --------- Signed-off-by: Lin Wang <[email protected]>
- Loading branch information
Showing
4 changed files
with
207 additions
and
24 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
53 changes: 53 additions & 0 deletions
53
src/plugins/workspace/public/components/utils/feature.test.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,53 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
isFeatureDependBySelectedFeatures, | ||
getFinalFeatureIdsByDependency, | ||
generateFeatureDependencyMap, | ||
} from './feature'; | ||
|
||
describe('feature utils', () => { | ||
describe('isFeatureDependBySelectedFeatures', () => { | ||
it('should return true', () => { | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], { b: ['a'] })).toBe(true); | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], { b: ['a', 'c'] })).toBe(true); | ||
}); | ||
it('should return false', () => { | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], { b: ['c'] })).toBe(false); | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], {})).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('getFinalFeatureIdsByDependency', () => { | ||
it('should return consistent feature ids', () => { | ||
expect(getFinalFeatureIdsByDependency(['a'], { a: ['b'] }, ['c', 'd'])).toStrictEqual([ | ||
'c', | ||
'd', | ||
'a', | ||
'b', | ||
]); | ||
expect(getFinalFeatureIdsByDependency(['a'], { a: ['b', 'e'] }, ['c', 'd'])).toStrictEqual([ | ||
'c', | ||
'd', | ||
'a', | ||
'b', | ||
'e', | ||
]); | ||
}); | ||
}); | ||
|
||
it('should generate consistent features dependency map', () => { | ||
expect( | ||
generateFeatureDependencyMap([ | ||
{ id: 'a', dependencies: { b: { type: 'required' }, c: { type: 'optional' } } }, | ||
{ id: 'b', dependencies: { c: { type: 'required' } } }, | ||
]) | ||
).toEqual({ | ||
a: ['b'], | ||
b: ['c'], | ||
}); | ||
}); | ||
}); |
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,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { App } from '../../../../../core/public'; | ||
|
||
export const isFeatureDependBySelectedFeatures = ( | ||
featureId: string, | ||
selectedFeatureIds: string[], | ||
featureDependencies: { [key: string]: string[] } | ||
) => | ||
selectedFeatureIds.some((selectedFeatureId) => | ||
(featureDependencies[selectedFeatureId] || []).some((dependencies) => | ||
dependencies.includes(featureId) | ||
) | ||
); | ||
|
||
/** | ||
* | ||
* Generate new feature id list based the old feature id list | ||
* and feature dependencies map. The feature dependency map may | ||
* has duplicate ids with old feature id list. Use set here to | ||
* get the unique feature ids. | ||
* | ||
* @param featureIds a feature id list need to add based old feature id list | ||
* @param featureDependencies a feature dependencies map to get depended feature ids | ||
* @param oldFeatureIds a feature id list that represent current feature id selection states | ||
*/ | ||
export const getFinalFeatureIdsByDependency = ( | ||
featureIds: string[], | ||
featureDependencies: { [key: string]: string[] }, | ||
oldFeatureIds: string[] = [] | ||
) => | ||
Array.from( | ||
new Set([ | ||
...oldFeatureIds, | ||
...featureIds.reduce( | ||
(pValue, featureId) => [...pValue, ...(featureDependencies[featureId] || [])], | ||
featureIds | ||
), | ||
]) | ||
); | ||
|
||
export const generateFeatureDependencyMap = ( | ||
allFeatures: Array<Pick<App, 'id' | 'dependencies'>> | ||
) => | ||
allFeatures.reduce<{ [key: string]: string[] }>( | ||
(pValue, { id, dependencies }) => | ||
dependencies | ||
? { | ||
...pValue, | ||
[id]: [ | ||
...(pValue[id] || []), | ||
...Object.keys(dependencies).filter((key) => dependencies[key].type === 'required'), | ||
], | ||
} | ||
: pValue, | ||
{} | ||
); |
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