|
| 1 | +import type { SchemaValidationThunkAction } from '.'; |
| 2 | +import { zeroStateChanged } from './zero-state'; |
| 3 | +import { enableEditRules } from './edit-mode'; |
| 4 | +import { analyzeSchema } from '@mongodb-js/compass-schema'; |
| 5 | +import type { MongoError } from 'mongodb'; |
| 6 | +import type { Action, AnyAction, Reducer } from 'redux'; |
| 7 | +import { validationLevelChanged, validatorChanged } from './validation'; |
| 8 | + |
| 9 | +export function isAction<A extends AnyAction>( |
| 10 | + action: AnyAction, |
| 11 | + type: A['type'] |
| 12 | +): action is A { |
| 13 | + return action.type === type; |
| 14 | +} |
| 15 | + |
| 16 | +export type ValidationServerAction = 'error' | 'warn'; |
| 17 | +export type ValidationLevel = 'off' | 'moderate' | 'strict'; |
| 18 | + |
| 19 | +const ERROR_CODE_MAX_TIME_MS_EXPIRED = 50; |
| 20 | + |
| 21 | +const SAMPLE_SIZE = 1000; |
| 22 | +const ABORT_MESSAGE = 'Operation cancelled'; |
| 23 | + |
| 24 | +export const enum RulesGenerationActions { |
| 25 | + generationStarted = 'schema-validation/rules-generation/generationStarted', |
| 26 | + generationFailed = 'schema-validation/rules-generation/generationFailed', |
| 27 | + generationFinished = 'schema-validation/rules-generation/generationFinished', |
| 28 | + generationErrorCleared = 'schema-validation/rules-generation/generationErrorCleared', |
| 29 | +} |
| 30 | + |
| 31 | +export type RulesGenerationStarted = { |
| 32 | + type: RulesGenerationActions.generationStarted; |
| 33 | +}; |
| 34 | + |
| 35 | +export type RulesGenerationFailed = { |
| 36 | + type: RulesGenerationActions.generationFailed; |
| 37 | + error: Error; |
| 38 | +}; |
| 39 | + |
| 40 | +export type RulesGenerationErrorCleared = { |
| 41 | + type: RulesGenerationActions.generationErrorCleared; |
| 42 | +}; |
| 43 | + |
| 44 | +export type RulesGenerationFinished = { |
| 45 | + type: RulesGenerationActions.generationFinished; |
| 46 | +}; |
| 47 | + |
| 48 | +export type RulesGenerationError = { |
| 49 | + errorMessage: string; |
| 50 | + errorType: 'timeout' | 'highComplexity' | 'general'; |
| 51 | +}; |
| 52 | + |
| 53 | +export interface RulesGenerationState { |
| 54 | + isInProgress: boolean; |
| 55 | + error?: RulesGenerationError; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * The initial state. |
| 60 | + */ |
| 61 | +export const INITIAL_STATE: RulesGenerationState = { |
| 62 | + isInProgress: false, |
| 63 | +}; |
| 64 | + |
| 65 | +function getErrorDetails(error: Error): RulesGenerationError { |
| 66 | + const errorCode = (error as MongoError).code; |
| 67 | + const errorMessage = error.message || 'Unknown error'; |
| 68 | + let errorType: RulesGenerationError['errorType'] = 'general'; |
| 69 | + if (errorCode === ERROR_CODE_MAX_TIME_MS_EXPIRED) { |
| 70 | + errorType = 'timeout'; |
| 71 | + } else if (error.message.includes('Schema analysis aborted: Fields count')) { |
| 72 | + errorType = 'highComplexity'; |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + errorType, |
| 77 | + errorMessage, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Reducer function for handle state changes to status. |
| 83 | + */ |
| 84 | +export const rulesGenerationReducer: Reducer<RulesGenerationState, Action> = ( |
| 85 | + state = INITIAL_STATE, |
| 86 | + action |
| 87 | +) => { |
| 88 | + if ( |
| 89 | + isAction<RulesGenerationStarted>( |
| 90 | + action, |
| 91 | + RulesGenerationActions.generationStarted |
| 92 | + ) |
| 93 | + ) { |
| 94 | + return { |
| 95 | + ...state, |
| 96 | + isInProgress: true, |
| 97 | + error: undefined, |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + if ( |
| 102 | + isAction<RulesGenerationFinished>( |
| 103 | + action, |
| 104 | + RulesGenerationActions.generationFinished |
| 105 | + ) |
| 106 | + ) { |
| 107 | + return { |
| 108 | + ...state, |
| 109 | + isInProgress: false, |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + if ( |
| 114 | + isAction<RulesGenerationFailed>( |
| 115 | + action, |
| 116 | + RulesGenerationActions.generationFailed |
| 117 | + ) |
| 118 | + ) { |
| 119 | + return { |
| 120 | + ...state, |
| 121 | + isInProgress: false, |
| 122 | + error: getErrorDetails(action.error), |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + if ( |
| 127 | + isAction<RulesGenerationErrorCleared>( |
| 128 | + action, |
| 129 | + RulesGenerationActions.generationErrorCleared |
| 130 | + ) |
| 131 | + ) { |
| 132 | + return { |
| 133 | + ...state, |
| 134 | + error: undefined, |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + return state; |
| 139 | +}; |
| 140 | + |
| 141 | +export const clearRulesGenerationError = |
| 142 | + (): SchemaValidationThunkAction<RulesGenerationErrorCleared> => { |
| 143 | + return (dispatch) => |
| 144 | + dispatch({ type: RulesGenerationActions.generationErrorCleared }); |
| 145 | + }; |
| 146 | + |
| 147 | +export const stopRulesGeneration = (): SchemaValidationThunkAction<void> => { |
| 148 | + return (dispatch, getState, { rulesGenerationAbortControllerRef }) => { |
| 149 | + if (!rulesGenerationAbortControllerRef.current) return; |
| 150 | + rulesGenerationAbortControllerRef.current?.abort(ABORT_MESSAGE); |
| 151 | + }; |
| 152 | +}; |
| 153 | + |
| 154 | +/** |
| 155 | + * Get $jsonSchema from schema analysis |
| 156 | + * @returns |
| 157 | + */ |
| 158 | +export const generateValidationRules = (): SchemaValidationThunkAction< |
| 159 | + Promise<void> |
| 160 | +> => { |
| 161 | + return async ( |
| 162 | + dispatch, |
| 163 | + getState, |
| 164 | + { dataService, logger, preferences, rulesGenerationAbortControllerRef } |
| 165 | + ) => { |
| 166 | + dispatch({ type: RulesGenerationActions.generationStarted }); |
| 167 | + |
| 168 | + rulesGenerationAbortControllerRef.current = new AbortController(); |
| 169 | + const abortSignal = rulesGenerationAbortControllerRef.current.signal; |
| 170 | + |
| 171 | + const { namespace } = getState(); |
| 172 | + const { maxTimeMS } = preferences.getPreferences(); |
| 173 | + |
| 174 | + try { |
| 175 | + const samplingOptions = { |
| 176 | + query: {}, |
| 177 | + size: SAMPLE_SIZE, |
| 178 | + fields: undefined, |
| 179 | + }; |
| 180 | + const driverOptions = { |
| 181 | + maxTimeMS, |
| 182 | + }; |
| 183 | + const schemaAccessor = await analyzeSchema( |
| 184 | + dataService, |
| 185 | + abortSignal, |
| 186 | + namespace.toString(), |
| 187 | + samplingOptions, |
| 188 | + driverOptions, |
| 189 | + logger, |
| 190 | + preferences |
| 191 | + ); |
| 192 | + if (abortSignal?.aborted) { |
| 193 | + throw new Error(ABORT_MESSAGE); |
| 194 | + } |
| 195 | + |
| 196 | + const jsonSchema = await schemaAccessor?.getMongoDBJsonSchema({ |
| 197 | + signal: abortSignal, |
| 198 | + }); |
| 199 | + if (abortSignal?.aborted) { |
| 200 | + throw new Error(ABORT_MESSAGE); |
| 201 | + } |
| 202 | + const validator = JSON.stringify( |
| 203 | + { $jsonSchema: jsonSchema }, |
| 204 | + undefined, |
| 205 | + 2 |
| 206 | + ); |
| 207 | + dispatch(validationLevelChanged('moderate')); |
| 208 | + dispatch(validatorChanged(validator)); |
| 209 | + dispatch(enableEditRules()); |
| 210 | + dispatch({ type: RulesGenerationActions.generationFinished }); |
| 211 | + dispatch(zeroStateChanged(false)); |
| 212 | + } catch (error) { |
| 213 | + if (abortSignal.aborted) { |
| 214 | + dispatch({ type: RulesGenerationActions.generationFinished }); |
| 215 | + return; |
| 216 | + } |
| 217 | + dispatch({ |
| 218 | + type: RulesGenerationActions.generationFailed, |
| 219 | + error, |
| 220 | + }); |
| 221 | + } |
| 222 | + }; |
| 223 | +}; |
0 commit comments