Skip to content

Commit 51836ea

Browse files
authored
chore(data-modeling): add telemetry events COMPASS-9785 (#7335)
1 parent 9936ea1 commit 51836ea

File tree

3 files changed

+174
-16
lines changed

3 files changed

+174
-16
lines changed

packages/compass-data-modeling/src/store/diagram.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
applyEdit,
55
getCurrentDiagramFromState,
66
getCurrentModel,
7+
getTypeNameForTelemetry,
78
openDiagram,
89
redoEdit,
910
undoEdit,
@@ -574,3 +575,30 @@ describe('Data Modeling store', function () {
574575
});
575576
});
576577
});
578+
579+
describe('getTypeNameForTelemetry', () => {
580+
it('should return undefined when bsonType is undefined', () => {
581+
const result = getTypeNameForTelemetry(undefined);
582+
expect(result).to.be.undefined;
583+
});
584+
585+
it('should return undefined when bsonType is an empty array', () => {
586+
const result = getTypeNameForTelemetry([]);
587+
expect(result).to.be.undefined;
588+
});
589+
590+
it('should return the string when bsonType is a string', () => {
591+
const result = getTypeNameForTelemetry('string');
592+
expect(result).to.equal('string');
593+
});
594+
595+
it('should return the single element when bsonType is an array with one element', () => {
596+
const result = getTypeNameForTelemetry(['string']);
597+
expect(result).to.equal('string');
598+
});
599+
600+
it('should return "mixed" when bsonType is an array with multiple elements', () => {
601+
const result = getTypeNameForTelemetry(['string', 'number']);
602+
expect(result).to.equal('mixed');
603+
});
604+
});

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -696,38 +696,78 @@ export function deleteRelationship(
696696

697697
export function deleteCollection(
698698
ns: string
699-
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
700-
return applyEdit({ type: 'RemoveCollection', ns });
699+
): DataModelingThunkAction<void, ApplyEditAction | ApplyEditFailedAction> {
700+
return (dispatch, getState, { track }) => {
701+
track('Data Modeling Collection Removed', {
702+
source: 'side_panel',
703+
});
704+
705+
dispatch(applyEdit({ type: 'RemoveCollection', ns }));
706+
};
701707
}
702708

703709
export function updateCollectionNote(
704710
ns: string,
705711
note: string
706-
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
712+
): DataModelingThunkAction<void, ApplyEditAction | ApplyEditFailedAction> {
707713
return applyEdit({ type: 'UpdateCollectionNote', ns, note });
708714
}
709715

710716
export function removeField(
711717
ns: string,
712718
field: FieldPath
713-
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
714-
return applyEdit({ type: 'RemoveField', ns, field });
719+
): DataModelingThunkAction<void, ApplyEditAction | ApplyEditFailedAction> {
720+
return (dispatch, getState, { track }) => {
721+
track('Data Modeling Field Removed', {
722+
source: 'side_panel',
723+
});
724+
725+
dispatch(applyEdit({ type: 'RemoveField', ns, field }));
726+
};
715727
}
716728

717729
export function renameField(
718730
ns: string,
719731
field: FieldPath,
720732
newName: string
721-
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
722-
return applyEdit({ type: 'RenameField', ns, field, newName });
733+
): DataModelingThunkAction<void, ApplyEditAction | ApplyEditFailedAction> {
734+
return (dispatch, getState, { track }) => {
735+
track('Data Modeling Field Renamed', {
736+
source: 'side_panel',
737+
});
738+
739+
dispatch(applyEdit({ type: 'RenameField', ns, field, newName }));
740+
};
741+
}
742+
743+
/**
744+
* @internal Exported for testing purposes only.
745+
* If the field had a single type, we return that, otherwise 'mixed'.
746+
*/
747+
export function getTypeNameForTelemetry(
748+
bsonType: string | string[] | undefined
749+
): string | undefined {
750+
if (!bsonType) {
751+
return;
752+
}
753+
if (Array.isArray(bsonType)) {
754+
if (bsonType.length === 0) {
755+
return undefined;
756+
}
757+
if (bsonType.length === 1) {
758+
return bsonType[0];
759+
}
760+
return 'mixed';
761+
}
762+
return bsonType;
723763
}
724764

725765
export function changeFieldType(
726766
ns: string,
727767
fieldPath: FieldPath,
728768
newTypes: string[]
729769
): DataModelingThunkAction<void, ApplyEditAction | ApplyEditFailedAction> {
730-
return (dispatch, getState) => {
770+
return (dispatch, getState, { track }) => {
731771
const collectionSchema = selectCurrentModelFromState(
732772
getState()
733773
).collections.find((collection) => collection.ns === ns)?.jsonSchema;
@@ -738,6 +778,13 @@ export function changeFieldType(
738778
});
739779
if (!field) throw new Error('Field not found in schema');
740780
const to = getSchemaWithNewTypes(field.jsonSchema, newTypes);
781+
782+
track('Data Modeling Field Type Changed', {
783+
source: 'side_panel',
784+
from: getTypeNameForTelemetry(field.jsonSchema.bsonType),
785+
to: getTypeNameForTelemetry(to.bsonType),
786+
});
787+
741788
dispatch(
742789
applyEdit({
743790
type: 'ChangeFieldType',
@@ -786,10 +833,10 @@ export function addCollection(
786833
ns?: string,
787834
position?: [number, number]
788835
): DataModelingThunkAction<
789-
boolean,
836+
void,
790837
ApplyEditAction | ApplyEditFailedAction | CollectionSelectedAction
791838
> {
792-
return (dispatch, getState) => {
839+
return (dispatch, getState, { track }) => {
793840
const existingCollections = selectCurrentModelFromState(
794841
getState()
795842
).collections;
@@ -802,6 +849,10 @@ export function addCollection(
802849
});
803850
}
804851

852+
track('Data Modeling Collection Added', {
853+
source: 'toolbar',
854+
});
855+
805856
const edit: Omit<
806857
Extract<Edit, { type: 'AddCollection' }>,
807858
'id' | 'timestamp'
@@ -820,7 +871,6 @@ export function addCollection(
820871
position,
821872
};
822873
dispatch(applyEdit(edit));
823-
return true;
824874
};
825875
}
826876

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

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,6 +2933,42 @@ type CreateIndexStrategiesDocumentationClicked = CommonEvent<{
29332933
};
29342934
}>;
29352935

2936+
/**
2937+
* This event is fired when user adds a collection in a data modeling diagram.
2938+
*
2939+
* @category Data Modeling
2940+
*/
2941+
type DataModelingDiagramCollectionAdded = CommonEvent<{
2942+
name: 'Data Modeling Collection Added';
2943+
payload: {
2944+
source: 'toolbar';
2945+
};
2946+
}>;
2947+
2948+
/**
2949+
* This event is fired when user removes a collection in a data modeling diagram.
2950+
*
2951+
* @category Data Modeling
2952+
*/
2953+
type DataModelingDiagramCollectionRemoved = CommonEvent<{
2954+
name: 'Data Modeling Collection Removed';
2955+
payload: {
2956+
source: 'side_panel';
2957+
};
2958+
}>;
2959+
2960+
/**
2961+
* This event is fired when user renames a collection in a data modeling diagram.
2962+
*
2963+
* @category Data Modeling
2964+
*/
2965+
type DataModelingDiagramCollectionRenamed = CommonEvent<{
2966+
name: 'Data Modeling Collection Renamed';
2967+
payload: {
2968+
source: 'side_panel';
2969+
};
2970+
}>;
2971+
29362972
/**
29372973
* This event is fired when a new data modeling diagram is created
29382974
*
@@ -2957,6 +2993,44 @@ type DataModelingDiagramExported = CommonEvent<{
29572993
};
29582994
}>;
29592995

2996+
/**
2997+
* This event is fired when user removes a field in a data modeling diagram.
2998+
*
2999+
* @category Data Modeling
3000+
*/
3001+
type DataModelingDiagramFieldRemoved = CommonEvent<{
3002+
name: 'Data Modeling Field Removed';
3003+
payload: {
3004+
source: 'side_panel';
3005+
};
3006+
}>;
3007+
3008+
/**
3009+
* This event is fired when user renames a field in a data modeling diagram.
3010+
*
3011+
* @category Data Modeling
3012+
*/
3013+
type DataModelingDiagramFieldRenamed = CommonEvent<{
3014+
name: 'Data Modeling Field Renamed';
3015+
payload: {
3016+
source: 'side_panel';
3017+
};
3018+
}>;
3019+
3020+
/**
3021+
* This event is fired when user changes a field type in a data modeling diagram.
3022+
*
3023+
* @category Data Modeling
3024+
*/
3025+
type DataModelingDiagramFieldTypeChanged = CommonEvent<{
3026+
name: 'Data Modeling Field Type Changed';
3027+
payload: {
3028+
source: 'side_panel';
3029+
from?: string;
3030+
to?: string;
3031+
};
3032+
}>;
3033+
29603034
/**
29613035
* This event is fired when user imports data modeling diagram.
29623036
*
@@ -3079,7 +3153,18 @@ export type TelemetryEvent =
30793153
| ConnectionRemovedEvent
30803154
| CurrentOpShowOperationDetailsEvent
30813155
| DatabaseCreatedEvent
3156+
| DataModelingDiagramCollectionAdded
3157+
| DataModelingDiagramCollectionRemoved
3158+
| DataModelingDiagramCollectionRenamed
30823159
| DataModelingDiagramCreated
3160+
| DataModelingDiagramExported
3161+
| DataModelingDiagramFieldRemoved
3162+
| DataModelingDiagramFieldRenamed
3163+
| DataModelingDiagramFieldTypeChanged
3164+
| DataModelingDiagramImported
3165+
| DataModelingDiagramRelationshipAdded
3166+
| DataModelingDiagramRelationshipEdited
3167+
| DataModelingDiagramRelationshipDeleted
30833168
| DeleteExportedEvent
30843169
| DeleteExportOpenedEvent
30853170
| DetailViewHideOperationDetailsEvent
@@ -3176,10 +3261,5 @@ export type TelemetryEvent =
31763261
| CreateIndexIndexSuggestionsCopied
31773262
| CreateIndexStrategiesDocumentationClicked
31783263
| UUIDEncounteredEvent
3179-
| DataModelingDiagramExported
3180-
| DataModelingDiagramImported
3181-
| DataModelingDiagramRelationshipAdded
3182-
| DataModelingDiagramRelationshipEdited
3183-
| DataModelingDiagramRelationshipDeleted
31843264
| ContextMenuOpened
31853265
| ContextMenuItemClicked;

0 commit comments

Comments
 (0)