Skip to content

Commit

Permalink
chore: extract event types COMPASS-8058 (#6132)
Browse files Browse the repository at this point in the history
* chore(telemetry): setup telemetry event types

* chore: add event types for telemetry

* remove script

* document events

* sort by category

* revert shell events

* fix types

* pass check

* fix check

* fix check

* fix check
  • Loading branch information
mcasimir authored Aug 29, 2024
1 parent 3aed795 commit d281a87
Show file tree
Hide file tree
Showing 11 changed files with 1,631 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ export const runAIPipelineGeneration = (
const provideSampleDocuments =
preferences.getPreferences().enableGenAISampleDocumentPassing;

const editor_view_type = pipelineMode === 'builder-ui' ? 'stages' : 'text';
const editor_view_type: 'stages' | 'text' =
pipelineMode === 'builder-ui' ? 'stages' : 'text';

if (existingRequestId !== null) {
// Cancel the active request as this one will override.
abort(existingRequestId);
Expand Down
6 changes: 3 additions & 3 deletions packages/compass-query-bar/src/stores/ai-query-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function trackAndLogFailed({
track(
'AI Response Failed',
() => ({
editor_view_type: 'find',
editor_view_type: 'find' as const,
error_name: errorName,
status_code: statusCode,
error_code: errorCode ?? '',
Expand Down Expand Up @@ -178,7 +178,7 @@ export const runAIQuery = (
track(
'AI Prompt Submitted',
() => ({
editor_view_type: 'find',
editor_view_type: 'find' as const,
user_input_length: userInput.length,
has_sample_documents: provideSampleDocuments,
request_id: requestId,
Expand Down Expand Up @@ -372,7 +372,7 @@ export const runAIQuery = (
track(
'AI Response Generated',
() => ({
editor_view_type: 'find',
editor_view_type: 'find' as const,
query_shape: Object.keys(generatedFields),
request_id: requestId,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ function useSearchFilter(): [React.ReactElement, string] {
}}
onBlur={() => {
if (search.length > 0) {
track('My Queries Search');
track('My Queries Search', {
//
});
}
}}
spellCheck={false}
Expand Down
21 changes: 14 additions & 7 deletions packages/compass-telemetry/src/generic-track.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Logger, mongoLogId } from '@mongodb-js/compass-logging/provider';
import type { TrackFunction, AsyncTrackFunction } from './types';
import type { TrackFunction, TrackFunctionPayload } from './types';

export interface TelemetryPreferences {
getPreferences(): { trackUsageStatistics: boolean };
Expand All @@ -14,14 +14,18 @@ export interface TelemetryServiceOptions {
useConnectionInfo?: TelemetryConnectionInfoHook;
}

type AsyncFn<T extends (...args: any[]) => any> = (
...args: Parameters<T>
) => Promise<ReturnType<T>>;

export const createTrack = ({
sendTrack,
logger: { log, debug },
preferences,
}: TelemetryServiceOptions & { logger: Logger }) => {
const trackAsync: AsyncTrackFunction = async (
const trackAsync: AsyncFn<TrackFunction> = async (
event,
parameters,
parametersOrFn,
connectionInfo
) => {
// Note that this preferences check is mainly a performance optimization,
Expand All @@ -33,9 +37,12 @@ export const createTrack = ({
return;
}

if (typeof parameters === 'function') {
let parameters: TrackFunctionPayload<Record<string, unknown>> =
parametersOrFn;

if (typeof parametersOrFn === 'function') {
try {
parameters = await parameters();
parameters = await parametersOrFn();
} catch (error) {
// When an error arises during the fetching of properties,
// for instance if we can't fetch host information,
Expand Down Expand Up @@ -66,11 +73,11 @@ export const createTrack = ({
sendTrack(event, parameters || {});
};

const track: TrackFunction = (...args) => {
const track = (...args: Parameters<typeof trackAsync>): void => {
void Promise.resolve()
.then(() => trackAsync(...args))
.catch((error) => debug('track failed', error));
};

return track;
return track as TrackFunction;
};
6 changes: 3 additions & 3 deletions packages/compass-telemetry/src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Telemetry', () => {
const trackingLogs: any[] = [];
process.on('compass:track', (event) => trackingLogs.push(event));

track('Test Event1', {
track('Test Event1' as any, {
some_attribute: 123,
});

Expand All @@ -30,7 +30,7 @@ describe('Telemetry', () => {
const trackingLogs: any[] = [];
process.on('compass:track', (event) => trackingLogs.push(event));

track('Test Event2', async () => {
track('Test Event2' as any, async () => {
await new Promise((resolve) => setTimeout(resolve, 3));

return {
Expand All @@ -57,7 +57,7 @@ describe('Telemetry', () => {
const trackingLogs: any[] = [];
process.on('compass:track', (event) => trackingLogs.push(event));

track('Test Event3', () => {
track('Test Event3' as any, () => {
throw new Error('test error');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/compass-telemetry/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createIpcTrack, createIpcSendTrack } from './ipc-track';
export type { TelemetryServiceOptions } from './generic-track';
export type { TrackFunction, TrackParameters } from './types';
export type { TrackFunction } from './types';
8 changes: 5 additions & 3 deletions packages/compass-telemetry/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useRef } from 'react';
import { createServiceLocator } from 'hadron-app-registry';
import { createTrack, type TelemetryServiceOptions } from './generic-track';
import { useLogger } from '@mongodb-js/compass-logging/provider';
import type { TrackFunction, TrackParameters } from './types';
import type { TrackFunction } from './types';

const noop = () => {
// noop
Expand Down Expand Up @@ -38,7 +38,9 @@ export const telemetryLocator = createServiceLocator(
'telemetryLocator'
);

export function useTelemetry(): TrackFunction {
export function useTelemetry(): (
...args: Parameters<TrackFunction>
) => ReturnType<TrackFunction> {
const track = React.useContext(TelemetryContext);
if (!track) {
throw new Error('Telemetry service is missing from React context');
Expand Down Expand Up @@ -97,4 +99,4 @@ export function useTrackOnChange(
}, [...dependencies, track, options.skipOnMount]);
}

export type { TrackFunction, TrackParameters };
export type { TrackFunction };
Loading

0 comments on commit d281a87

Please sign in to comment.