Skip to content

Commit

Permalink
feat: add playground created telemetry event VSCODE-379 (#508)
Browse files Browse the repository at this point in the history
* feat: add playground created telemetry event VSCODE-379

* feat: add playground file type to playground loaded and saved telemetry
  • Loading branch information
alenakhineika authored Apr 5, 2023
1 parent 271a312 commit f881b13
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 55 deletions.
22 changes: 19 additions & 3 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
import playgroundTemplate from '../templates/playgroundTemplate';
import { StatusView } from '../views';
import TelemetryService from '../telemetry/telemetryService';
import { isPlayground } from '../utils/playground';
import {
isPlayground,
getPlaygroundExtensionForTelemetry,
} from '../utils/playground';

const log = createLogger('playground controller');

Expand Down Expand Up @@ -188,14 +191,18 @@ export default class PlaygroundController {

vscode.workspace.onDidOpenTextDocument(async (document) => {
if (isPlayground(document.uri)) {
this._telemetryService.trackPlaygroundLoaded();
this._telemetryService.trackPlaygroundLoaded(
getPlaygroundExtensionForTelemetry(document.uri)
);
await vscode.languages.setTextDocumentLanguage(document, 'javascript');
}
});

vscode.workspace.onDidSaveTextDocument((document) => {
if (isPlayground(document.uri)) {
this._telemetryService.trackPlaygroundSaved();
this._telemetryService.trackPlaygroundSaved(
getPlaygroundExtensionForTelemetry(document.uri)
);
}
});

Expand Down Expand Up @@ -327,6 +334,7 @@ export default class PlaygroundController {
.replace('CURRENT_DATABASE', databaseName)
.replace('CURRENT_COLLECTION', collectionName);

this._telemetryService.trackPlaygroundCreated('search');
return this._createPlaygroundFileWithContent(content);
}

Expand All @@ -341,6 +349,9 @@ export default class PlaygroundController {
content = content
.replace('NEW_DATABASE_NAME', element.databaseName)
.replace('Create a new database', 'The current database to use');
this._telemetryService.trackPlaygroundCreated('createCollection');
} else {
this._telemetryService.trackPlaygroundCreated('createDatabase');
}

return this._createPlaygroundFileWithContent(content);
Expand All @@ -354,6 +365,7 @@ export default class PlaygroundController {
.replace('CURRENT_DATABASE', databaseName)
.replace('CURRENT_COLLECTION', collectionName);

this._telemetryService.trackPlaygroundCreated('index');
return this._createPlaygroundFileWithContent(content);
}

Expand All @@ -367,6 +379,7 @@ export default class PlaygroundController {
.replace('CURRENT_COLLECTION', collectionName)
.replace('DOCUMENT_CONTENTS', documentContents);

this._telemetryService.trackPlaygroundCreated('cloneDocument');
return this._createPlaygroundFileWithContent(content);
}

Expand All @@ -378,6 +391,7 @@ export default class PlaygroundController {
.replace('CURRENT_DATABASE', databaseName)
.replace('CURRENT_COLLECTION', collectionName);

this._telemetryService.trackPlaygroundCreated('insertDocument');
return this._createPlaygroundFileWithContent(content);
}

Expand All @@ -386,6 +400,8 @@ export default class PlaygroundController {
.getConfiguration('mdb')
.get('useDefaultTemplateForPlayground');
const content = useDefaultTemplate ? playgroundTemplate : '';

this._telemetryService.trackPlaygroundCreated('crud');
return this._createPlaygroundFileWithContent(content);
}

Expand Down
46 changes: 35 additions & 11 deletions src/telemetry/telemetryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type SegmentProperties = {

type LinkClickedTelemetryEventProperties = {
screen: string;
link_id: string; // eslint-disable-line camelcase
link_id: string;
};

type ExtensionCommandRunTelemetryEventProperties = {
Expand All @@ -48,15 +48,25 @@ type DocumentEditedTelemetryEventProperties = {
source: DocumentSource;
};

/* eslint-disable camelcase */
type QueryExportedTelemetryEventProperties = {
language: string;
num_stages?: number;
with_import_statements: boolean;
with_builders: boolean;
with_driver_syntax: boolean;
};
/* eslint-enable camelcase */

type PlaygroundCreatedTelemetryEventProperties = {
playground_type: string;
};

type PlaygroundSavedTelemetryEventProperties = {
file_type?: string;
};

type PlaygroundLoadedTelemetryEventProperties = {
file_type?: string;
};

export type TelemetryEventProperties =
| PlaygroundTelemetryEventProperties
Expand All @@ -65,7 +75,10 @@ export type TelemetryEventProperties =
| NewConnectionTelemetryEventProperties
| DocumentUpdatedTelemetryEventProperties
| DocumentEditedTelemetryEventProperties
| QueryExportedTelemetryEventProperties;
| QueryExportedTelemetryEventProperties
| PlaygroundCreatedTelemetryEventProperties
| PlaygroundSavedTelemetryEventProperties
| PlaygroundLoadedTelemetryEventProperties;

export enum TelemetryEventTypes {
PLAYGROUND_CODE_EXECUTED = 'Playground Code Executed',
Expand All @@ -78,6 +91,7 @@ export enum TelemetryEventTypes {
DOCUMENT_EDITED = 'Document Edited',
QUERY_EXPORTED = 'Query Exported',
AGGREGATION_EXPORTED = 'Aggregation Exported',
PLAYGROUND_CREATED = 'Playground Created',
}

/**
Expand Down Expand Up @@ -117,11 +131,11 @@ export default class TelemetryService {
const constantsFile = fs.readFileSync(segmentKeyFileLocation, 'utf8');
const constants = JSON.parse(constantsFile) as { segmentKey: string };

log.info('SegmentKey received', { type: typeof constants.segmentKey });
log.info('SegmentKey was found', { type: typeof constants.segmentKey });

return constants.segmentKey;
} catch (error) {
log.error('Reading SegmentKey failed', error);
log.error('SegmentKey was not found', error);
return;
}
}
Expand Down Expand Up @@ -183,7 +197,7 @@ export default class TelemetryService {
if (error) {
log.error('Failed to track telemetry', error);
} else {
log.info('Telemetry sent', error);
log.info('Telemetry sent', segmentProperties);
}
});
}
Expand Down Expand Up @@ -285,12 +299,16 @@ export default class TelemetryService {
});
}

trackPlaygroundLoaded(): void {
this.track(TelemetryEventTypes.PLAYGROUND_LOADED);
trackPlaygroundLoaded(fileType?: string): void {
this.track(TelemetryEventTypes.PLAYGROUND_LOADED, {
file_type: fileType,
});
}

trackPlaygroundSaved(): void {
this.track(TelemetryEventTypes.PLAYGROUND_SAVED);
trackPlaygroundSaved(fileType?: string): void {
this.track(TelemetryEventTypes.PLAYGROUND_SAVED, {
file_type: fileType,
});
}

trackDocumentUpdated(source: DocumentSource, success: boolean): void {
Expand All @@ -312,4 +330,10 @@ export default class TelemetryService {
): void {
this.track(TelemetryEventTypes.AGGREGATION_EXPORTED, aggExportedProps);
}

trackPlaygroundCreated(playgroundType: string): void {
this.track(TelemetryEventTypes.PLAYGROUND_CREATED, {
playground_type: playgroundType,
});
}
}
2 changes: 1 addition & 1 deletion src/test/fixture/testSaving.mongodb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
show dbs
use('test');
3 changes: 3 additions & 0 deletions src/test/fixture/testSaving.mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* global use, db */

use('test');
8 changes: 6 additions & 2 deletions src/test/suite/explorer/playgroundsExplorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ suite('Playgrounds Controller Test Suite', function () {
try {
const rootPath = path.resolve(__dirname, '../../../..');
const children = await treeController.getPlaygrounds(rootPath);
assert.strictEqual(Object.keys(children).length, 7);

// The number of playground files in the project repository.
assert.strictEqual(Object.keys(children).length, 8);
} catch (error) {
assert(false, error as Error);
}
Expand All @@ -69,7 +71,9 @@ suite('Playgrounds Controller Test Suite', function () {
const rootPath = path.resolve(__dirname, '../../../..');
const children = await treeController.getPlaygrounds(rootPath);

assert.strictEqual(Object.keys(children).length, 3);
// The number of playground files in the project repository,
// excluding the ./playgrounds directory.
assert.strictEqual(Object.keys(children).length, 4);
} catch (error) {
assert(false, error as Error);
}
Expand Down
Loading

0 comments on commit f881b13

Please sign in to comment.