Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, useMemo } from 'react';
import { connect } from 'react-redux';
import type { ConnectedProps } from 'react-redux';
import {
Expand All @@ -8,6 +8,7 @@ import {
spacing,
TextInput,
palette,
Tooltip,
} from '@mongodb-js/compass-components';

import type { RootState } from '../../../modules';
Expand Down Expand Up @@ -58,16 +59,48 @@ const PipelineCollation: React.FunctionComponent<PipelineCollationProps> = ({
maxTimeMSValue,
maxTimeMSChanged,
}) => {
const maxTimeMSEnvLimit = usePreference('maxTimeMSEnvLimit');

const onMaxTimeMSChanged = useCallback(
(evt: React.ChangeEvent<HTMLInputElement>) => {
if (maxTimeMSChanged) {
maxTimeMSChanged(parseInt(evt.currentTarget.value, 10));
const parsed = Number(evt.currentTarget.value);
const newValue = Number.isNaN(parsed) ? 0 : parsed;

// When environment limit is set (> 0), enforce it
if (maxTimeMSEnvLimit && newValue > maxTimeMSEnvLimit) {
maxTimeMSChanged(maxTimeMSEnvLimit);
} else {
maxTimeMSChanged(newValue);
}
}
},
[maxTimeMSChanged]
[maxTimeMSChanged, maxTimeMSEnvLimit]
);

const maxTimeMSLimit = usePreference('maxTimeMS');

// Determine the effective max limit when environment limit is set (> 0)
const effectiveMaxLimit = useMemo(() => {
if (maxTimeMSEnvLimit) {
return maxTimeMSLimit
? Math.min(maxTimeMSLimit, maxTimeMSEnvLimit)
: maxTimeMSEnvLimit;
}
return maxTimeMSLimit;
}, [maxTimeMSEnvLimit, maxTimeMSLimit]);

// Check if value exceeds the environment limit (when limit > 0)
const exceedsLimit = Boolean(
useMemo(() => {
return (
maxTimeMSEnvLimit &&
maxTimeMSValue &&
maxTimeMSValue >= maxTimeMSEnvLimit
);
}, [maxTimeMSEnvLimit, maxTimeMSValue])
);

return (
<div
className={pipelineOptionsContainerStyles}
Expand Down Expand Up @@ -107,27 +140,45 @@ const PipelineCollation: React.FunctionComponent<PipelineCollationProps> = ({
>
Max Time MS
</Label>
<TextInput
aria-labelledby={maxTimeMSLabelId}
id={maxTimeMSInputId}
data-testid="max-time-ms"
className={inputStyles}
placeholder={`${Math.min(
DEFAULT_MAX_TIME_MS,
maxTimeMSLimit || Infinity
)}`}
type="number"
min="0"
max={maxTimeMSLimit}
sizeVariant="small"
value={`${maxTimeMSValue ?? ''}`}
state={
maxTimeMSValue && maxTimeMSLimit && maxTimeMSValue > maxTimeMSLimit
? 'error'
: 'none'
}
onChange={onMaxTimeMSChanged}
/>
<Tooltip
enabled={exceedsLimit}
open={exceedsLimit}
trigger={({
children,
...triggerProps
}: React.HTMLProps<HTMLDivElement>) => (
<div {...triggerProps}>
<TextInput
aria-labelledby={maxTimeMSLabelId}
id={maxTimeMSInputId}
data-testid="max-time-ms"
className={inputStyles}
placeholder={`${Math.min(
DEFAULT_MAX_TIME_MS,
effectiveMaxLimit || Infinity
)}`}
type="number"
min="0"
max={effectiveMaxLimit}
sizeVariant="small"
value={`${maxTimeMSValue ?? ''}`}
state={
(maxTimeMSValue &&
effectiveMaxLimit &&
maxTimeMSValue > effectiveMaxLimit) ||
exceedsLimit
? 'error'
: 'none'
}
onChange={onMaxTimeMSChanged}
/>
{children}
</div>
)}
>
Operations longer than 5 minutes are not supported in the web
environment
</Tooltip>
</div>
);
};
Expand Down
13 changes: 13 additions & 0 deletions packages/compass-preferences-model/src/preferences-schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export type UserConfigurablePreferences = PermanentFeatureFlags &
enableProxySupport: boolean;
proxy: string;
inferNamespacesFromPrivileges?: boolean;
// Features that are enabled by default in Date Explorer, but are disabled in Compass
maxTimeMSEnvLimit?: number;
};

/**
Expand Down Expand Up @@ -1062,6 +1064,17 @@ export const storedUserPreferencesProps: Required<{
validator: z.boolean().default(true),
type: 'boolean',
},
maxTimeMSEnvLimit: {
ui: true,
cli: true,
global: true,
description: {
short:
'Maximum time limit for operations in environment (milliseconds). Set to 0 for no limit.',
},
validator: z.number().min(0).default(0),
type: 'number',
},

...allFeatureFlagsProps,
};
Expand Down
94 changes: 69 additions & 25 deletions packages/compass-query-bar/src/components/query-option.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useCallback, useRef } from 'react';
import React, { useCallback, useRef, useMemo } from 'react';
import {
Label,
TextInput,
Tooltip,
css,
cx,
spacing,
Expand All @@ -20,6 +21,7 @@ import type { QueryProperty } from '../constants/query-properties';
import type { RootState } from '../stores/query-bar-store';
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
import { useConnectionInfoRef } from '@mongodb-js/compass-connections/provider';
import { usePreference } from 'compass-preferences-model/provider';

const queryOptionStyles = css({
display: 'flex',
Expand Down Expand Up @@ -153,6 +155,22 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
}
}, [track, name, connectionInfoRef]);

// MaxTimeMS warning tooltip logic
const maxTimeMSEnvLimit = usePreference('maxTimeMSEnvLimit');
const numericValue = useMemo(() => {
if (!value) return 0;
const parsed = Number(value);
return Number.isNaN(parsed) ? 0 : parsed;
}, [value]);

const exceedsMaxTimeMSLimit = useMemo(() => {
return (
name === 'maxTimeMS' &&
maxTimeMSEnvLimit && // 0 is falsy, so no limit when 0
numericValue >= maxTimeMSEnvLimit
);
}, [name, maxTimeMSEnvLimit, numericValue]);

return (
<div
className={cx(
Expand Down Expand Up @@ -200,30 +218,56 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
/>
) : (
<WithOptionDefinitionTextInputProps definition={optionDefinition}>
{({ props }) => (
<TextInput
aria-labelledby={`query-bar-option-input-${name}-label`}
id={id}
data-testid={`query-bar-option-${name}-input`}
className={cx(
darkMode
? numericTextInputDarkStyles
: numericTextInputLightStyles,
hasError && optionInputWithErrorStyles
)}
type="text"
sizeVariant="small"
state={hasError ? 'error' : 'none'}
value={value}
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
onValueChange(evt.currentTarget.value)
}
onBlur={onBlurEditor}
placeholder={placeholder as string}
disabled={disabled}
{...props}
/>
)}
{({ props }) => {
const textInput = (
<TextInput
aria-labelledby={`query-bar-option-input-${name}-label`}
id={id}
data-testid={`query-bar-option-${name}-input`}
className={cx(
darkMode
? numericTextInputDarkStyles
: numericTextInputLightStyles,
hasError && optionInputWithErrorStyles
)}
type="text"
sizeVariant="small"
state={hasError ? 'error' : 'none'}
value={value}
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
onValueChange(evt.currentTarget.value)
}
onBlur={onBlurEditor}
placeholder={placeholder as string}
disabled={disabled}
{...props}
/>
);

// Wrap maxTimeMS field with tooltip in web environment when exceeding limit
if (exceedsMaxTimeMSLimit) {
return (
<Tooltip
enabled={true}
open={true}
trigger={({
children,
...triggerProps
}: React.HTMLProps<HTMLDivElement>) => (
<div {...triggerProps}>
{textInput}
{children}
</div>
)}
>
Operations longer than 5 minutes are not supported in the
web environment
</Tooltip>
);
}

return textInput;
}}
</WithOptionDefinitionTextInputProps>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,26 @@ export const OPTION_DEFINITION: {
link: 'https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/',
extraTextInputProps() {
const preferenceMaxTimeMS = usePreference('maxTimeMS');
const props: { max?: number; placeholder?: string } = {
max: preferenceMaxTimeMS,
const maxTimeMSEnvLimit = usePreference('maxTimeMSEnvLimit');

// Determine the effective max limit when environment limit is set (> 0)
const effectiveMaxLimit = maxTimeMSEnvLimit
? preferenceMaxTimeMS
? Math.min(preferenceMaxTimeMS, maxTimeMSEnvLimit)
: maxTimeMSEnvLimit
: preferenceMaxTimeMS;

const props: {
max?: number;
placeholder?: string;
} = {
max: effectiveMaxLimit,
};
if (preferenceMaxTimeMS !== undefined && preferenceMaxTimeMS < 60000) {
props.placeholder = String(preferenceMaxTimeMS);

if (effectiveMaxLimit && effectiveMaxLimit < 60000) {
props.placeholder = `${+effectiveMaxLimit}`;
}

return props;
},
},
Expand Down
23 changes: 9 additions & 14 deletions packages/compass-query-bar/src/stores/query-bar-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type QueryBarState = {

export const INITIAL_STATE: QueryBarState = {
isReadonlyConnection: false,
fields: mapQueryToFormFields({}, DEFAULT_FIELD_VALUES),
fields: mapQueryToFormFields({ maxTimeMSEnvLimit: 0 }, DEFAULT_FIELD_VALUES),
expanded: false,
serverVersion: '3.6.0',
lastAppliedQuery: { source: null, query: {} },
Expand Down Expand Up @@ -103,6 +103,7 @@ export const changeField = (
return (dispatch, getState, { preferences }) => {
const parsedValue = validateField(name, stringValue, {
maxTimeMS: preferences.getPreferences().maxTimeMS ?? undefined,
maxTimeMSEnvLimit: preferences.getPreferences().maxTimeMSEnvLimit,
});
const isValid = parsedValue !== false;
dispatch({
Expand Down Expand Up @@ -162,7 +163,7 @@ export const resetQuery = (
return false;
}
const fields = mapQueryToFormFields(
{ maxTimeMS: preferences.getPreferences().maxTimeMS },
preferences.getPreferences(),
DEFAULT_FIELD_VALUES
);
dispatch({ type: QueryBarActions.ResetQuery, fields, source });
Expand All @@ -179,10 +180,7 @@ export const setQuery = (
query: BaseQuery
): QueryBarThunkAction<void, SetQueryAction> => {
return (dispatch, getState, { preferences }) => {
const fields = mapQueryToFormFields(
{ maxTimeMS: preferences.getPreferences().maxTimeMS },
query
);
const fields = mapQueryToFormFields(preferences.getPreferences(), query);
dispatch({ type: QueryBarActions.SetQuery, fields });
};
};
Expand Down Expand Up @@ -238,14 +236,11 @@ export const applyFromHistory = (
}
return acc;
}, {});
const fields = mapQueryToFormFields(
{ maxTimeMS: preferences.getPreferences().maxTimeMS },
{
...DEFAULT_FIELD_VALUES,
...query,
...currentQuery,
}
);
const fields = mapQueryToFormFields(preferences.getPreferences(), {
...DEFAULT_FIELD_VALUES,
...query,
...currentQuery,
});
dispatch({
type: QueryBarActions.ApplyFromHistory,
fields,
Expand Down
Loading
Loading