Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React, { useCallback } from 'react';
import { withPreferences } from 'compass-preferences-model/provider';
import {
usePreference,
withPreferences,
} from 'compass-preferences-model/provider';
import { connect } from 'react-redux';
import { VIEW_PIPELINE_UTILS } from '@mongodb-js/mongodb-constants';

import {
Combobox,
Expand All @@ -13,9 +17,10 @@ import type { RootState } from '../../modules';
import { changeStageOperator } from '../../modules/pipeline-builder/stage-editor';
import type { StoreStage } from '../../modules/pipeline-builder/stage-editor';

import { filterStageOperators } from '../../utils/stage';
import { filterStageOperators, isSearchStage } from '../../utils/stage';
import { isAtlasOnly } from '../../utils/stage';
import type { ServerEnvironment } from '../../modules/env';
import type { CollectionStats } from '../../modules/collection-stats';

const inputWidth = spacing[1400] * 3;
// width of options popover
Expand Down Expand Up @@ -43,6 +48,47 @@ type StageOperatorSelectProps = {
env: ServerEnvironment[];
description: string;
}[];
serverVersion: string;
isReadonlyView: boolean;
collectionStats: CollectionStats;
};

type Stage = {
name: string;
env: ServerEnvironment[];
description: string;
};

export const getStageDescription = (
stage: Stage,
isReadonlyView: boolean,
versionIncompatibleCompass: boolean,
versionIncompatibleDE: boolean,
isPipelineSearchQueryable: boolean
) => {
if (isReadonlyView && isSearchStage(stage.name)) {
const minViewCompatibilityVersion = versionIncompatibleCompass
? VIEW_PIPELINE_UTILS.MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
: VIEW_PIPELINE_UTILS.MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE;
const minMajorMinorVersion = minViewCompatibilityVersion
.split('.')
.slice(0, 2)
.join('.');
if (versionIncompatibleCompass || versionIncompatibleDE) {
return (
`Atlas only. Requires MongoDB ${minMajorMinorVersion}+ to run on a view. ` +
stage.description
);
}

if (!isPipelineSearchQueryable) {
return (
`Atlas only. Only views containing $addFields, $set or $match stages with the $expr operator are compatible with search indexes. ` +
stage.description
);
}
}
return (isAtlasOnly(stage.env) ? 'Atlas only. ' : '') + stage.description;
};

// exported for tests
Expand All @@ -51,6 +97,9 @@ export const StageOperatorSelect = ({
index,
selectedStage,
isDisabled,
serverVersion,
isReadonlyView,
collectionStats,
stages,
}: StageOperatorSelectProps) => {
const onStageOperatorSelected = useCallback(
Expand All @@ -59,6 +108,29 @@ export const StageOperatorSelect = ({
},
[onChange, index]
);

const enableAtlasSearchIndexes = usePreference('enableAtlasSearchIndexes');
const versionIncompatibleCompass =
enableAtlasSearchIndexes &&
!VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass(
serverVersion
);
const versionIncompatibleDE =
!enableAtlasSearchIndexes &&
!VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer(
serverVersion
);
const pipelineIsSearchQueryable = collectionStats?.pipeline
? VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(
collectionStats.pipeline as Document[]
)
: true;
const disableSearchStage =
isReadonlyView &&
(!pipelineIsSearchQueryable ||
versionIncompatibleCompass ||
versionIncompatibleDE);

return (
<Combobox
value={selectedStage}
Expand All @@ -70,14 +142,19 @@ export const StageOperatorSelect = ({
data-testid="stage-operator-combobox"
className={comboboxStyles}
>
{stages.map((stage, index) => (
{stages.map((stage: Stage, index) => (
<ComboboxOption
data-testid={`combobox-option-stage-${stage.name}`}
key={`combobox-option-stage-${index}`}
value={stage.name}
description={
(isAtlasOnly(stage.env) ? 'Atlas only. ' : '') + stage.description
}
disabled={isSearchStage(stage.name) && disableSearchStage}
description={getStageDescription(
stage,
isReadonlyView,
versionIncompatibleCompass,
versionIncompatibleDE,
pipelineIsSearchQueryable
)}
/>
))}
</Combobox>
Expand Down Expand Up @@ -113,6 +190,9 @@ export default withPreferences(
selectedStage: stage.stageOperator,
isDisabled: stage.disabled,
stages: stages,
serverVersion: state.serverVersion,
isReadonlyView: !!state.sourceName,
collectionStats: state.collectionStats,
};
},
(dispatch: any, ownProps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import { isAction } from '../utils/is-action';

export type CollectionStats = {
document_count?: number;
pipeline?: unknown[];
};

export const INITIAL_STATE: CollectionStats = {
document_count: undefined,
pipeline: undefined,
};

export function pickCollectionStats(collection: Collection): CollectionStats {
const { document_count } = collection.toJSON();
const { document_count, pipeline } = collection.toJSON();
return {
document_count,
pipeline,
};
}

Expand Down
Loading