Skip to content

Commit fa13c94

Browse files
committed
use .sample and add more precise error typing and handling
1 parent 4397540 commit fa13c94

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

package-lock.json

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-collection/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@mongodb-js/mongodb-constants": "^0.12.2",
6060
"compass-preferences-model": "^2.49.0",
6161
"hadron-document": "^8.9.4",
62+
"mongodb": "^6.18.0",
6263
"mongodb-collection-model": "^5.31.0",
6364
"mongodb-ns": "^2.4.2",
6465
"mongodb-schema": "^12.6.2",

packages/compass-collection/src/modules/collection-tab.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import {
1717
SCHEMA_ANALYSIS_STATE_COMPLETE,
1818
SCHEMA_ANALYSIS_STATE_ERROR,
1919
SCHEMA_ANALYSIS_STATE_INITIAL,
20-
type SchemaAnalysis,
20+
type SchemaAnalysisError,
21+
type SchemaAnalysisState,
2122
} from '../schema-analysis-types';
2223
import { calculateSchemaDepth } from '../calculate-schema-depth';
24+
import type { MongoError } from 'mongodb';
2325

2426
const DEFAULT_SAMPLE_SIZE = 100;
2527

@@ -32,6 +34,24 @@ function isAction<A extends AnyAction>(
3234
return action.type === type;
3335
}
3436

37+
const ERROR_CODE_MAX_TIME_MS_EXPIRED = 50;
38+
39+
function getErrorDetails(error: Error): SchemaAnalysisError {
40+
const errorCode = (error as MongoError).code;
41+
const errorMessage = error.message || 'Unknown error';
42+
let errorType: SchemaAnalysisError['errorType'] = 'general';
43+
if (errorCode === ERROR_CODE_MAX_TIME_MS_EXPIRED) {
44+
errorType = 'timeout';
45+
} else if (error.message.includes('Schema analysis aborted: Fields count')) {
46+
errorType = 'highComplexity';
47+
}
48+
49+
return {
50+
errorType,
51+
errorMessage,
52+
};
53+
}
54+
3555
type CollectionThunkAction<R, A extends AnyAction = AnyAction> = ThunkAction<
3656
R,
3757
CollectionState,
@@ -51,7 +71,7 @@ export type CollectionState = {
5171
namespace: string;
5272
metadata: CollectionMetadata | null;
5373
editViewName?: string;
54-
schemaAnalysis: SchemaAnalysis;
74+
schemaAnalysis: SchemaAnalysisState;
5575
};
5676

5777
enum CollectionActions {
@@ -154,7 +174,7 @@ const reducer: Reducer<CollectionState, Action> = (
154174
...state,
155175
schemaAnalysis: {
156176
status: SCHEMA_ANALYSIS_STATE_ERROR,
157-
error: action.error,
177+
error: getErrorDetails(action.error),
158178
},
159179
};
160180
}
@@ -204,15 +224,14 @@ export const analyzeCollectionSchema = (): CollectionThunkAction<
204224
const driverOptions = {
205225
maxTimeMS: preferences.getPreferences().maxTimeMS,
206226
};
207-
const sampleCursor = dataService.sampleCursor(
227+
const sampleDocuments = await dataService.sample(
208228
namespace,
209229
samplingOptions,
210230
driverOptions,
211231
{
212232
fallbackReadPreference: 'secondaryPreferred',
213233
}
214234
);
215-
const sampleDocuments: Array<Document> = await sampleCursor.toArray();
216235
if (sampleDocuments.length === 0) {
217236
logger.debug(NO_DOCUMENTS_ERROR);
218237
dispatch({

packages/compass-collection/src/schema-analysis-types.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ export type SchemaAnalysisStatus =
1111
| typeof SCHEMA_ANALYSIS_STATE_COMPLETE
1212
| typeof SCHEMA_ANALYSIS_STATE_ERROR;
1313

14-
export type SchemaAnalysisInitial = {
14+
export type SchemaAnalysisInitialState = {
1515
status: typeof SCHEMA_ANALYSIS_STATE_INITIAL;
1616
};
1717

18-
export type SchemaAnalysisStarted = {
18+
export type SchemaAnalysisStartedState = {
1919
status: typeof SCHEMA_ANALYSIS_STATE_ANALYZING;
2020
};
2121

2222
export type SchemaAnalysisError = {
23+
errorMessage: string;
24+
errorType: 'timeout' | 'highComplexity' | 'general';
25+
};
26+
27+
export type SchemaAnalysisErrorState = {
2328
status: typeof SCHEMA_ANALYSIS_STATE_ERROR;
24-
error: Error;
29+
error: SchemaAnalysisError;
2530
};
2631

27-
export type SchemaAnalysisCompleted = {
32+
export type SchemaAnalysisCompletedState = {
2833
status: typeof SCHEMA_ANALYSIS_STATE_COMPLETE;
2934
schema: Schema;
3035
sampleDocument: Document;
@@ -34,8 +39,8 @@ export type SchemaAnalysisCompleted = {
3439
};
3540
};
3641

37-
export type SchemaAnalysis =
38-
| SchemaAnalysisError
39-
| SchemaAnalysisInitial
40-
| SchemaAnalysisStarted
41-
| SchemaAnalysisCompleted;
42+
export type SchemaAnalysisState =
43+
| SchemaAnalysisErrorState
44+
| SchemaAnalysisInitialState
45+
| SchemaAnalysisStartedState
46+
| SchemaAnalysisCompletedState;

0 commit comments

Comments
 (0)