Skip to content

Commit 238a79b

Browse files
authored
feat(data-modeling): add an option to disable auto infer when generating a new diagram COMPASS-9777 (#7328)
* feat(data-modeling): add an option to disable auto infer when generating a new diagram * chore(data-modeling): reword the description
1 parent 3566310 commit 238a79b

File tree

3 files changed

+118
-53
lines changed

3 files changed

+118
-53
lines changed

packages/compass-data-modeling/src/components/new-diagram-form.tsx

Lines changed: 99 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
selectCollections,
1717
selectConnection,
1818
selectDatabase,
19+
toggleInferRelationships,
1920
} from '../store/generate-diagram-wizard';
2021
import {
2122
Banner,
@@ -35,7 +36,9 @@ import {
3536
SearchInput,
3637
Combobox,
3738
ComboboxOption,
39+
Checkbox,
3840
} from '@mongodb-js/compass-components';
41+
import { usePreference } from 'compass-preferences-model/provider';
3942

4043
const footerStyles = css({
4144
flexDirection: 'row',
@@ -46,12 +49,6 @@ const footerTextStyles = css({ marginRight: 'auto' });
4649

4750
const footerActionsStyles = css({ display: 'flex', gap: spacing[200] });
4851

49-
const formContainerStyles = css({
50-
display: 'flex',
51-
flexDirection: 'column',
52-
gap: spacing[400],
53-
});
54-
5552
const FormStepContainer: React.FunctionComponent<{
5653
title: string;
5754
description?: string;
@@ -112,20 +109,27 @@ const FormStepContainer: React.FunctionComponent<{
112109
);
113110
};
114111

115-
const SelectListStyles = css({
116-
height: 300,
112+
const selectListStyles = css({
113+
maxHeight: 200,
117114
overflow: 'scroll',
118115
});
119116

120117
function SelectCollectionsStep({
121118
collections,
122119
selectedCollections,
120+
automaticallyInferRelationships,
123121
onCollectionsSelect,
122+
onAutomaticallyInferRelationshipsToggle,
124123
}: {
125124
collections: string[];
126125
selectedCollections: string[];
126+
automaticallyInferRelationships: boolean;
127127
onCollectionsSelect: (colls: string[]) => void;
128+
onAutomaticallyInferRelationshipsToggle: (newVal: boolean) => void;
128129
}) {
130+
const showAutoInferOption = usePreference(
131+
'enableAutomaticRelationshipInference'
132+
);
129133
const [searchTerm, setSearchTerm] = useState('');
130134
const filteredCollections = useMemo(() => {
131135
try {
@@ -136,50 +140,80 @@ function SelectCollectionsStep({
136140
}
137141
}, [collections, searchTerm]);
138142
return (
139-
<FormFieldContainer className={formContainerStyles}>
140-
<SearchInput
141-
aria-label="Search collections"
142-
value={searchTerm}
143-
data-testid="new-diagram-search-collections"
144-
onChange={(e) => {
145-
setSearchTerm(e.target.value);
146-
}}
147-
/>
148-
<SelectList
149-
className={SelectListStyles}
150-
items={filteredCollections.map((collName) => {
151-
return {
152-
id: collName,
153-
selected: selectedCollections.includes(collName),
154-
'data-testid': `new-diagram-collection-checkbox-${collName}`,
155-
};
156-
})}
157-
label={{ displayLabelKey: 'id', name: 'Collection Name' }}
158-
onChange={(items: { id: string; selected: boolean }[]) => {
159-
// When a user is searching, less collections are shown to the user
160-
// and we need to keep existing selected collections selected.
161-
const currentSelectedItems = selectedCollections.filter(
162-
(collName) => {
163-
const item = items.find((x) => x.id === collName);
164-
// The already selected item was not shown to the user (using search),
165-
// and we have to keep it selected.
166-
return item ? item.selected : true;
167-
}
168-
);
143+
<>
144+
<FormFieldContainer>
145+
<SearchInput
146+
aria-label="Search collections"
147+
value={searchTerm}
148+
data-testid="new-diagram-search-collections"
149+
onChange={(e) => {
150+
setSearchTerm(e.target.value);
151+
}}
152+
/>
153+
</FormFieldContainer>
154+
<FormFieldContainer>
155+
<SelectList
156+
className={selectListStyles}
157+
items={filteredCollections.map((collName) => {
158+
return {
159+
id: collName,
160+
selected: selectedCollections.includes(collName),
161+
'data-testid': `new-diagram-collection-checkbox-${collName}`,
162+
};
163+
})}
164+
label={{ displayLabelKey: 'id', name: 'Collection Name' }}
165+
onChange={(items) => {
166+
// When a user is searching, less collections are shown to the user
167+
// and we need to keep existing selected collections selected.
168+
const currentSelectedItems = selectedCollections.filter(
169+
(collName) => {
170+
const item = items.find((x) => x.id === collName);
171+
// The already selected item was not shown to the user (using search),
172+
// and we have to keep it selected.
173+
return item ? item.selected : true;
174+
}
175+
);
169176

170-
const newSelectedItems = items
171-
.filter((item) => {
172-
return item.selected;
173-
})
174-
.map((item) => {
175-
return item.id;
176-
});
177-
onCollectionsSelect(
178-
Array.from(new Set([...newSelectedItems, ...currentSelectedItems]))
179-
);
180-
}}
181-
></SelectList>
182-
</FormFieldContainer>
177+
const newSelectedItems = items
178+
.filter((item) => {
179+
return item.selected;
180+
})
181+
.map((item) => {
182+
return item.id;
183+
});
184+
onCollectionsSelect(
185+
Array.from(
186+
new Set([...newSelectedItems, ...currentSelectedItems])
187+
)
188+
);
189+
}}
190+
></SelectList>
191+
</FormFieldContainer>
192+
{showAutoInferOption && (
193+
<FormFieldContainer>
194+
<Checkbox
195+
checked={automaticallyInferRelationships}
196+
onChange={(evt) => {
197+
onAutomaticallyInferRelationshipsToggle(
198+
evt.currentTarget.checked
199+
);
200+
}}
201+
label="Automatically infer relationships"
202+
// @ts-expect-error Element is accepted, but not typed correctly
203+
description={
204+
<>
205+
Compass will try to automatically discover relationships in
206+
selected collections. This operation will run multiple find
207+
requests against indexed fields of the collections and{' '}
208+
<strong>
209+
will take additional time per collection being analyzed.
210+
</strong>
211+
</>
212+
}
213+
></Checkbox>
214+
</FormFieldContainer>
215+
)}
216+
</>
183217
);
184218
}
185219

@@ -199,6 +233,7 @@ type NewDiagramFormProps = {
199233
selectedCollections: string[];
200234
error: Error | null;
201235
analysisInProgress: boolean;
236+
automaticallyInferRelationships: boolean;
202237

203238
onCancel: () => void;
204239
onNameChange: (name: string) => void;
@@ -212,6 +247,7 @@ type NewDiagramFormProps = {
212247
onDatabaseConfirmSelection: () => void;
213248
onCollectionsSelect: (collections: string[]) => void;
214249
onCollectionsSelectionConfirm: () => void;
250+
onAutomaticallyInferRelationshipsToggle: (newVal: boolean) => void;
215251
};
216252

217253
const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
@@ -226,6 +262,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
226262
selectedCollections,
227263
error,
228264
analysisInProgress,
265+
automaticallyInferRelationships,
229266
onCancel,
230267
onNameChange,
231268
onNameConfirm,
@@ -238,6 +275,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
238275
onDatabaseConfirmSelection,
239276
onCollectionsSelect,
240277
onCollectionsSelectionConfirm,
278+
onAutomaticallyInferRelationshipsToggle,
241279
}) => {
242280
const connections = useConnectionsList();
243281
const [activeConnections, otherConnections] = useMemo(() => {
@@ -349,7 +387,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
349387
);
350388
case 'select-connection':
351389
return (
352-
<FormFieldContainer className={formContainerStyles}>
390+
<FormFieldContainer>
353391
<Combobox
354392
label=""
355393
aria-label="Select connection"
@@ -419,16 +457,22 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
419457
collections={collections}
420458
onCollectionsSelect={onCollectionsSelect}
421459
selectedCollections={selectedCollections}
460+
automaticallyInferRelationships={automaticallyInferRelationships}
461+
onAutomaticallyInferRelationshipsToggle={
462+
onAutomaticallyInferRelationshipsToggle
463+
}
422464
/>
423465
);
424466
}
425467
}, [
426468
activeConnections,
469+
automaticallyInferRelationships,
427470
collections,
428471
connections.length,
429472
currentStep,
430473
databases,
431474
diagramName,
475+
onAutomaticallyInferRelationshipsToggle,
432476
onCollectionsSelect,
433477
onConnectionSelect,
434478
onDatabaseSelect,
@@ -516,6 +560,8 @@ export default connect(
516560
error,
517561
analysisInProgress:
518562
state.analysisProgress.analysisProcessStatus === 'in-progress',
563+
automaticallyInferRelationships:
564+
state.generateDiagramWizard.automaticallyInferRelations,
519565
};
520566
},
521567
{
@@ -531,5 +577,6 @@ export default connect(
531577
onDatabaseConfirmSelection: confirmSelectDatabase,
532578
onCollectionsSelect: selectCollections,
533579
onCollectionsSelectionConfirm: confirmSelectedCollections,
580+
onAutomaticallyInferRelationshipsToggle: toggleInferRelationships,
534581
}
535582
)(NewDiagramForm);

packages/compass-data-modeling/src/store/generate-diagram-wizard.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export type SelectCollectionsAction = {
138138

139139
export type ToggleInferRelationsAction = {
140140
type: GenerateDiagramWizardActionTypes.TOGGLE_INFER_RELATIONS;
141+
newVal: boolean;
141142
};
142143

143144
export type ConfirmSelectedCollectionsAction = {
@@ -330,6 +331,14 @@ export const generateDiagramWizardReducer: Reducer<
330331
step: 'LOADING_COLLECTIONS',
331332
};
332333
}
334+
if (
335+
isAction(action, GenerateDiagramWizardActionTypes.TOGGLE_INFER_RELATIONS)
336+
) {
337+
return {
338+
...state,
339+
automaticallyInferRelations: action.newVal,
340+
};
341+
}
333342
return state;
334343
};
335344

@@ -546,3 +555,12 @@ export function cancelSelectedConnection(): CancelSelectedConnectionAction {
546555
export function cancelSelectedDatabase(): CancelSelectedDatabaseAction {
547556
return { type: GenerateDiagramWizardActionTypes.CANCEL_SELECTED_DATABASE };
548557
}
558+
559+
export function toggleInferRelationships(
560+
newVal: boolean
561+
): ToggleInferRelationsAction {
562+
return {
563+
type: GenerateDiagramWizardActionTypes.TOGGLE_INFER_RELATIONS,
564+
newVal,
565+
};
566+
}

packages/compass-preferences-model/src/feature-flags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export const featureFlags: Required<{
192192
},
193193

194194
enableAutomaticRelationshipInference: {
195-
stage: 'development',
195+
stage: 'released',
196196
description: {
197197
short:
198198
'Enable automatic relationship inference during data model generation',

0 commit comments

Comments
 (0)