@@ -4,6 +4,12 @@ import { enableEditRules } from './edit-mode';
44import type { MongoError } from 'mongodb' ;
55import type { Action , Reducer } from 'redux' ;
66import { 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' ;
713import { isAction } from '../util' ;
814
915const ERROR_CODE_MAX_TIME_MS_EXPIRED = 50 ;
@@ -141,6 +147,29 @@ export const stopRulesGeneration = (): SchemaValidationThunkAction<void> => {
141147 } ;
142148} ;
143149
150+ const _trackRulesGenerated = async ( {
151+ schemaAccessor,
152+ track,
153+ connectionInfoRef,
154+ } : {
155+ schemaAccessor : Awaited < ReturnType < typeof analyzeSchemaType > > ;
156+ track : TrackFunction ;
157+ connectionInfoRef : ConnectionInfoRef ;
158+ } ) => {
159+ const internalSchema = await schemaAccessor ?. getInternalSchema ( ) ;
160+ if ( ! internalSchema ) return ;
161+ const { variable_type_count, optional_field_count } =
162+ await calculateSchemaMetadata ( internalSchema ) ;
163+ track (
164+ 'Schema Validation Generated' ,
165+ {
166+ variable_type_count,
167+ optional_field_count,
168+ } ,
169+ connectionInfoRef . current
170+ ) ;
171+ } ;
172+
144173/**
145174 * Get $jsonSchema from schema analysis
146175 * @returns
@@ -157,6 +186,8 @@ export const generateValidationRules = (): SchemaValidationThunkAction<
157186 preferences,
158187 rulesGenerationAbortControllerRef,
159188 analyzeSchema,
189+ track,
190+ connectionInfoRef,
160191 }
161192 ) => {
162193 dispatch ( { type : RulesGenerationActions . generationStarted } ) ;
@@ -205,6 +236,12 @@ export const generateValidationRules = (): SchemaValidationThunkAction<
205236 dispatch ( enableEditRules ( ) ) ;
206237 dispatch ( { type : RulesGenerationActions . generationFinished } ) ;
207238 dispatch ( zeroStateChanged ( false ) ) ;
239+
240+ void _trackRulesGenerated ( {
241+ schemaAccessor,
242+ connectionInfoRef,
243+ track,
244+ } ) ;
208245 } catch ( error ) {
209246 if ( abortSignal . aborted ) {
210247 dispatch ( { type : RulesGenerationActions . generationFinished } ) ;
0 commit comments