Skip to content

Commit fa480a3

Browse files
import view pipeline utils
1 parent d49df70 commit fa480a3

File tree

1 file changed

+20
-71
lines changed

1 file changed

+20
-71
lines changed

packages/compass-aggregations/src/components/stage-toolbar/stage-operator-select.tsx

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
withPreferences,
55
} from 'compass-preferences-model/provider';
66
import { connect } from 'react-redux';
7+
import { VIEW_PIPELINE_UTILS } from '@mongodb-js/mongodb-constants';
78

89
import {
910
Combobox,
@@ -20,70 +21,12 @@ import { filterStageOperators, isSearchStage } from '../../utils/stage';
2021
import { isAtlasOnly } from '../../utils/stage';
2122
import type { ServerEnvironment } from '../../modules/env';
2223
import type { CollectionStats } from '../../modules/collection-stats';
23-
import semver from 'semver'; //import from mongodb-js/constants
2424

25-
const inputWidth = spacing[1400] * 3;
25+
const inputWidth = spacing[1400] * 5;
2626
// width of options popover
2727
const comboxboxOptionsWidth = spacing[1200] * 10;
2828
// left position of options popover wrt input. this aligns it with the start of input
2929
const comboboxOptionsLeft = (comboxboxOptionsWidth - inputWidth) / 2;
30-
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE = '8.0.0';
31-
const isVersionSearchCompatibleForViewsDataExplorer = (
32-
serverVersion: string
33-
) => {
34-
try {
35-
return semver.gte(
36-
serverVersion,
37-
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE
38-
);
39-
} catch {
40-
return false;
41-
}
42-
};
43-
44-
// START: ALL OF THESE WILL BE IMPORTED FROM mongodb-constants
45-
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
46-
const isVersionSearchCompatibleForViews = (serverVersion: string) => {
47-
try {
48-
return semver.gte(
49-
serverVersion,
50-
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
51-
);
52-
} catch {
53-
return false;
54-
}
55-
};
56-
57-
const isPipelineSearchQueryable = (
58-
//import from mongodb-js/constants
59-
pipeline: Array<Record<string, any>>
60-
): boolean => {
61-
for (const stage of pipeline) {
62-
const stageKey = Object.keys(stage)[0];
63-
64-
if (
65-
!(
66-
stageKey === '$addFields' ||
67-
stageKey === '$set' ||
68-
stageKey === '$match'
69-
)
70-
) {
71-
return false;
72-
}
73-
74-
if (stageKey === '$match') {
75-
const matchStage = stage['$match'];
76-
const allKeys = Object.keys(matchStage);
77-
78-
if (!(allKeys.length === 1 && allKeys.includes('$expr'))) {
79-
return false; // Not searchable
80-
}
81-
}
82-
}
83-
84-
return true;
85-
};
86-
// END: ALL OF THESE WILL BE IMPORTED FROM mongodb-constants
8730

8831
const comboboxStyles = css({
8932
width: inputWidth,
@@ -124,16 +67,9 @@ export const getStageDescription = (
12467
isPipelineSearchQueryable: boolean
12568
) => {
12669
if (isReadonlyView && isSearchStage(stage.name)) {
127-
if (!isPipelineSearchQueryable) {
128-
return (
129-
`Atlas only. Only views containing $addFields, $set or $match stages with the $expr operator are compatible with search indexes.` +
130-
stage.description
131-
);
132-
}
133-
13470
const minViewCompatibilityVersion = versionIncompatibleCompass
135-
? MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
136-
: MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE;
71+
? VIEW_PIPELINE_UTILS.MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
72+
: VIEW_PIPELINE_UTILS.MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE;
13773
const minMajorMinorVersion = minViewCompatibilityVersion
13874
.split('.')
13975
.slice(0, 2)
@@ -144,6 +80,13 @@ export const getStageDescription = (
14480
stage.description
14581
);
14682
}
83+
84+
if (!isPipelineSearchQueryable) {
85+
return (
86+
`Atlas only. Only views containing $addFields, $set or $match stages with the $expr operator are compatible with search indexes. ` +
87+
stage.description
88+
);
89+
}
14790
}
14891
return (isAtlasOnly(stage.env) ? 'Atlas only. ' : '') + stage.description;
14992
};
@@ -169,12 +112,18 @@ export const StageOperatorSelect = ({
169112
const enableAtlasSearchIndexes = usePreference('enableAtlasSearchIndexes');
170113
const versionIncompatibleCompass =
171114
enableAtlasSearchIndexes &&
172-
!isVersionSearchCompatibleForViews(serverVersion);
115+
!VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass(
116+
serverVersion
117+
);
173118
const versionIncompatibleDE =
174119
!enableAtlasSearchIndexes &&
175-
!isVersionSearchCompatibleForViewsDataExplorer(serverVersion);
120+
!VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer(
121+
serverVersion
122+
);
176123
const pipelineIsSearchQueryable = collectionStats?.pipeline
177-
? isPipelineSearchQueryable(collectionStats.pipeline as Document[])
124+
? VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(
125+
collectionStats.pipeline as Document[]
126+
)
178127
: true;
179128
const disableSearchStage =
180129
isReadonlyView &&

0 commit comments

Comments
 (0)