Skip to content

Commit 95dee2c

Browse files
authored
feat: add telemetry for rules generation COMPASS-8863 (#6794)
1 parent 14fe15b commit 95dee2c

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

docs/tracking-plan.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Generated on Mon, Mar 10, 2025
162162
- [Schema Validation Added](#event--SchemaValidationAddedEvent)
163163
- [Schema Validation Edited](#event--SchemaValidationEditedEvent)
164164
- [Schema Validation Updated](#event--SchemaValidationUpdatedEvent)
165+
- [Schema Validation Generated](#event--SchemaValidationGeneratedEvent)
165166

166167
### Settings
167168
- [Theme Changed](#event--ThemeChangedEvent)
@@ -1981,6 +1982,22 @@ This event is fired when user edits validation rules (without saving them).
19811982

19821983
<a name="event--SchemaValidationUpdatedEvent"></a>
19831984

1985+
### Schema Validation Generated
1986+
1987+
This event is fired when user generates validation rules.
1988+
1989+
**Properties**:
1990+
1991+
- **is_compass_web** (optional): `true | undefined`
1992+
- **connection_id** (optional): `string | undefined`
1993+
- The id of the connection associated to this event.
1994+
- **variable_type_count** (required): `number`
1995+
- The count of fields with multiple types in the generated schema (not counting undefined).
1996+
This is only calculated for the top level fields, not nested fields and arrays.
1997+
- **optional_field_count** (required): `number`
1998+
- The count of fields that don't appear on all documents. This is only calculated for the top level fields, not nested fields and arrays.
1999+
2000+
19842001
### Schema Validation Updated
19852002

19862003
This event is fired when user saves validation rules.

packages/compass-schema-validation/src/modules/rules-generation.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { enableEditRules } from './edit-mode';
44
import type { MongoError } from 'mongodb';
55
import type { Action, AnyAction, Reducer } from 'redux';
66
import { validationLevelChanged, validatorChanged } from './validation';
7+
import {
8+
type analyzeSchema as analyzeSchemaType,
9+
calculateSchemaMetadata,
10+
} from '@mongodb-js/compass-schema';
11+
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
12+
import type { ConnectionInfoRef } from '@mongodb-js/compass-connections/provider';
713

814
export function isAction<A extends AnyAction>(
915
action: AnyAction,
@@ -150,6 +156,29 @@ export const stopRulesGeneration = (): SchemaValidationThunkAction<void> => {
150156
};
151157
};
152158

159+
const _trackRulesGenerated = async ({
160+
schemaAccessor,
161+
track,
162+
connectionInfoRef,
163+
}: {
164+
schemaAccessor: Awaited<ReturnType<typeof analyzeSchemaType>>;
165+
track: TrackFunction;
166+
connectionInfoRef: ConnectionInfoRef;
167+
}) => {
168+
const internalSchema = await schemaAccessor?.getInternalSchema();
169+
if (!internalSchema) return;
170+
const { variable_type_count, optional_field_count } =
171+
await calculateSchemaMetadata(internalSchema);
172+
track(
173+
'Schema Validation Generated',
174+
{
175+
variable_type_count,
176+
optional_field_count,
177+
},
178+
connectionInfoRef.current
179+
);
180+
};
181+
153182
/**
154183
* Get $jsonSchema from schema analysis
155184
* @returns
@@ -166,6 +195,8 @@ export const generateValidationRules = (): SchemaValidationThunkAction<
166195
preferences,
167196
rulesGenerationAbortControllerRef,
168197
analyzeSchema,
198+
track,
199+
connectionInfoRef,
169200
}
170201
) => {
171202
dispatch({ type: RulesGenerationActions.generationStarted });
@@ -214,6 +245,12 @@ export const generateValidationRules = (): SchemaValidationThunkAction<
214245
dispatch(enableEditRules());
215246
dispatch({ type: RulesGenerationActions.generationFinished });
216247
dispatch(zeroStateChanged(false));
248+
249+
void _trackRulesGenerated({
250+
schemaAccessor,
251+
connectionInfoRef,
252+
track,
253+
});
217254
} catch (error) {
218255
if (abortSignal.aborted) {
219256
dispatch({ type: RulesGenerationActions.generationFinished });

packages/compass-schema-validation/src/stores/store.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ const schemaAccessor = {
8282
setTimeout(() => resolve({ required: ['prop1'] }), 100); // waiting to give abort a chance
8383
});
8484
},
85+
getInternalSchema: () => {
86+
return new Promise((resolve) => {
87+
setTimeout(() => resolve({ required: ['prop1'] }), 100); // waiting to give abort a chance
88+
});
89+
},
8590
};
8691

8792
describe('Schema Validation Store', function () {

packages/compass-telemetry/src/telemetry-events.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,27 @@ type SchemaValidationUpdatedEvent = ConnectionScopedEvent<{
19061906
};
19071907
}>;
19081908

1909+
/**
1910+
* This event is fired when user generates validation rules.
1911+
*
1912+
* @category Schema Validation
1913+
*/
1914+
type SchemaValidationGeneratedEvent = ConnectionScopedEvent<{
1915+
name: 'Schema Validation Generated';
1916+
payload: {
1917+
/* The count of fields with multiple types in a given schema (not counting undefined).
1918+
* This is only calculated for the top level fields, not nested fields and arrays.
1919+
*/
1920+
variable_type_count: number;
1921+
1922+
/**
1923+
* The count of fields that don't appear on all documents.
1924+
* This is only calculated for the top level fields, not nested fields and arrays.
1925+
*/
1926+
optional_field_count: number;
1927+
};
1928+
}>;
1929+
19091930
/**
19101931
* This event is fired when user adds validation rules.
19111932
*
@@ -2754,6 +2775,7 @@ export type TelemetryEvent =
27542775
| SchemaValidationAddedEvent
27552776
| SchemaValidationEditedEvent
27562777
| SchemaValidationUpdatedEvent
2778+
| SchemaValidationGeneratedEvent
27572779
| ScreenEvent
27582780
| ShellEvent
27592781
| SignalActionButtonClickedEvent

0 commit comments

Comments
 (0)