Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filter out ADMIN application and add feature dependency logic #49

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/core/public/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ export interface App<HistoryLocationState = unknown> {
* ```
*/
exactRoute?: boolean;

/**
* The feature group of workspace, won't be displayed as feature if feature set is ADMIN.
*/
featureGroup?: Array<'WORKSPACE' | 'ADMIN'>;

/**
* The dependencies of one application, required feature will be automatic select and can't
* be unselect in the workspace configuration.
*/
dependencies?: {
[key: string]: {
type: 'required' | 'optional';
};
};
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/plugins/workspace/public/components/utils/feature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { isFeatureDependOnSelectedFeatures, getFinalFeatureIdsByDependency } from './feature';

describe('feature utils', () => {
describe('isFeatureDependOnSelectedFeatures', () => {
it('should return true', () => {
expect(isFeatureDependOnSelectedFeatures('a', ['b'], { b: ['a'] })).toBe(true);
expect(isFeatureDependOnSelectedFeatures('a', ['b'], { b: ['a', 'c'] })).toBe(true);
});
it('should return false', () => {
expect(isFeatureDependOnSelectedFeatures('a', ['b'], { b: ['c'] })).toBe(false);
expect(isFeatureDependOnSelectedFeatures('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',
]);
});
});
});
30 changes: 30 additions & 0 deletions src/plugins/workspace/public/components/utils/feature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const isFeatureDependOnSelectedFeatures = (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this function is checking if featureId is depend by selected features? So we want to know if selected selectedFeatureIds, should featureId been selected. Am I right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I think we can rename isFeatureDependOnSelectedFeatures to isFeatureDependBySelectedFeatures

featureId: string,
selectedFeatureIds: string[],
featureDependencies: { [key: string]: string[] }
) =>
selectedFeatureIds.some((selectedFeatureId) =>
(featureDependencies[selectedFeatureId] || []).some((dependencies) =>
dependencies.includes(featureId)
)
);

export const getFinalFeatureIdsByDependency = (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's oldFeatureIds? Maybe you could leave some comments on this function?

featureIds: string[],
featureDependencies: { [key: string]: string[] },
oldFeatureIds: string[] = []
) =>
Array.from(
new Set([
...oldFeatureIds,
...featureIds.reduce(
(pValue, featureId) => [...pValue, ...(featureDependencies[featureId] || [])],
featureIds
),
])
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a little bit large(about 300 lines), maybe we can extract some filter functions, isFeatureDependOnSelectedFeatures for example, to other files?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can separate these util function to a single file.

Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ import {
} from '@elastic/eui';

import { WorkspaceTemplate } from '../../../../../core/types';
import { AppNavLinkStatus, ApplicationStart } from '../../../../../core/public';
import { App, AppNavLinkStatus, ApplicationStart } from '../../../../../core/public';
import { useApplications, useWorkspaceTemplate } from '../../hooks';
import { WORKSPACE_OP_TYPE_CREATE, WORKSPACE_OP_TYPE_UPDATE } from '../../../common/constants';
import {
isFeatureDependOnSelectedFeatures,
getFinalFeatureIdsByDependency,
} from '../utils/feature';

import { WorkspaceIconSelector } from './workspace_icon_selector';

interface WorkspaceFeature {
interface WorkspaceFeature extends Pick<App, 'dependencies'> {
id: string;
name: string;
templates: WorkspaceTemplate[];
Expand Down Expand Up @@ -74,6 +79,7 @@ interface WorkspaceFormProps {
defaultValues?: WorkspaceFormData;
opType?: string;
}

export const WorkspaceForm = ({
application,
onSubmit,
Expand Down Expand Up @@ -115,13 +121,16 @@ export const WorkspaceForm = ({
const apps = category2Applications[currentKey];
const features = apps
.filter(
({ navLinkStatus, chromeless }) =>
navLinkStatus !== AppNavLinkStatus.hidden && !chromeless
({ navLinkStatus, chromeless, featureGroup }) =>
navLinkStatus !== AppNavLinkStatus.hidden &&
!chromeless &&
!featureGroup?.includes('ADMIN')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it check featureGroup?.includes('WORKSPACE')? because one feature could be 'WORKSPACE' and 'ADMIN' at the same time. Also one question, if a feature didn't have featureGroup specified, do we consider it as workspace feature?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, the application doesn't include featureGroup field will be treated as a workspace feature. Do we need to remove them from the workspace feature list?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, the application doesn't include featureGroup field will be treated as a workspace feature. Do we need to remove them from the workspace feature list?

Makes sense to me, then we can check if featureGroup is empty or contains WORKSPACE

)
.map(({ id, title, workspaceTemplate }) => ({
.map(({ id, title, workspaceTemplate, dependencies }) => ({
id,
name: title,
templates: workspaceTemplate || [],
dependencies,
}));
if (features.length === 1 || currentKey === 'undefined') {
return [...previousValue, ...features];
Expand All @@ -141,6 +150,38 @@ export const WorkspaceForm = ({
[defaultVISTheme]
);

const allFeatures = useMemo(
() =>
featureOrGroups.reduce<WorkspaceFeature[]>(
(previousData, currentData) => [
...previousData,
...(isWorkspaceFeatureGroup(currentData) ? currentData.features : [currentData]),
],
[]
),
[featureOrGroups]
);

const featureDependencies = useMemo(
() =>
allFeatures.reduce<{ [key: string]: string[] }>(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave a comment here? It's hard to understand the code. Or maybe extract it to a function? function agoodname(allFeatures) {...}

(pValue, { id, dependencies }) =>
dependencies
? {
...pValue,
[id]: [
...(pValue[id] || []),
...Object.keys(dependencies).filter(
(key) => dependencies[key].type === 'required'
),
],
}
: pValue,
{}
),
[allFeatures]
);

if (!formIdRef.current) {
formIdRef.current = workspaceHtmlIdGenerator();
}
Expand All @@ -150,27 +191,33 @@ export const WorkspaceForm = ({
const templateId = e.target.value;
setSelectedTemplateId(templateId);
setSelectedFeatureIds(
featureOrGroups.reduce<string[]>(
(previousData, currentData) => [
...previousData,
...(isWorkspaceFeatureGroup(currentData) ? currentData.features : [currentData])
.filter(({ templates }) => !!templates.find((template) => template.id === templateId))
.map((feature) => feature.id),
],
[]
getFinalFeatureIdsByDependency(
allFeatures
.filter(({ templates }) => !!templates.find((template) => template.id === templateId))
.map((feature) => feature.id),
featureDependencies
)
);
},
[featureOrGroups]
[allFeatures, featureDependencies]
);

const handleFeatureChange = useCallback<EuiCheckboxGroupProps['onChange']>((featureId) => {
setSelectedFeatureIds((previousData) =>
previousData.includes(featureId)
? previousData.filter((id) => id !== featureId)
: [...previousData, featureId]
);
}, []);
const handleFeatureChange = useCallback<EuiCheckboxGroupProps['onChange']>(
(featureId) => {
setSelectedFeatureIds((previousData) => {
if (!previousData.includes(featureId)) {
return getFinalFeatureIdsByDependency([featureId], featureDependencies, previousData);
}

if (isFeatureDependOnSelectedFeatures(featureId, previousData, featureDependencies)) {
return previousData;
}

return previousData.filter((selectedId) => selectedId !== featureId);
});
},
[featureDependencies]
);

const handleFeatureCheckboxChange = useCallback<EuiCheckboxProps['onChange']>(
(e) => {
Expand All @@ -187,14 +234,37 @@ export const WorkspaceForm = ({
setSelectedFeatureIds((previousData) => {
const notExistsIds = groupFeatureIds.filter((id) => !previousData.includes(id));
if (notExistsIds.length > 0) {
return [...previousData, ...notExistsIds];
return getFinalFeatureIdsByDependency(
notExistsIds,
featureDependencies,
previousData
);
}
return previousData.filter((id) => !groupFeatureIds.includes(id));
let groupRemainFeatureIds = groupFeatureIds;
const outGroupFeatureIds = previousData.filter(
(featureId) => !groupFeatureIds.includes(featureId)
);

while (true) {
const lastRemainFeatures = groupRemainFeatureIds.length;
groupRemainFeatureIds = groupRemainFeatureIds.filter((featureId) =>
isFeatureDependOnSelectedFeatures(
featureId,
[...outGroupFeatureIds, ...groupRemainFeatureIds],
featureDependencies
)
);
if (lastRemainFeatures === groupRemainFeatureIds.length) {
break;
}
}

return [...outGroupFeatureIds, ...groupRemainFeatureIds];
});
}
}
},
[featureOrGroups]
[featureOrGroups, featureDependencies]
);

const handleFormSubmit = useCallback<FormEventHandler>(
Expand Down
Loading