Repository: microsoft/vscode-edge-devtools
Pull request #404 (2021-06-02 04:21:43)

[Telemetry]: Add telemetry for file extension types
This PR adds telemetry that sends the used file extension types in the user's currently active workspace.  The telemetry is sent upon the activation of the extension.

- `workspace/metadata`
    - example:
        - `workspace/metadata: undefined, {"css":1,"js":1,"html":1,"json":2,"total":5}`


Patch:
diff --git a/src/extension.ts b/src/extension.ts
index c7ec3c1..95efef1 100644
--- a/src/extension.ts
+++ b/src/extension.ts
@@ -27,6 +27,7 @@ import {
     SETTINGS_VIEW_NAME,
     getActiveDebugSessionId,
     getJsDebugCDPProxyWebsocketUrl,
+    reportFileExtensionTypes,
     reportUrlType,
 } from './utils';
 import { LaunchConfigManager } from './launchConfigManager';
@@ -164,6 +165,7 @@ export function activate(context: vscode.ExtensionContext): void {
             void vscode.env.openExternal(vscode.Uri.parse('https://github.com/microsoft/vscode-edge-devtools/blob/master/README.md'));
         }));
     void vscode.commands.executeCommand('setContext', 'titleCommandsRegistered', true);
+    void reportFileExtensionTypes(telemetryReporter);
 }
 
 export async function attach(
diff --git a/src/utils.ts b/src/utils.ts
index 8202c6b..5e67e00 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -630,6 +630,28 @@ export function reportUrlType(url: string, telemetryReporter: Readonly<Telemetry
     telemetryReporter.sendTelemetryEvent('user/browserNavigation', { 'urlType': urlType });
 }
 
+export async function reportFileExtensionTypes(telemetryReporter: Readonly<TelemetryReporter>): Promise<void> {
+    const files = await vscode.workspace.findFiles('**/*.*', '**/node_modules/**');
+    const extensionMap: Map<string, number> = new Map<string, number>();
+    for (const file of files) {
+        const extension: string | undefined = file.path.split('.').pop();
+        if (extension) {
+            const currentValue = extensionMap.get(extension);
+            if (currentValue) {
+                extensionMap.set(extension, currentValue + 1);
+            } else {
+                extensionMap.set(extension, 1);
+            }
+        }
+    }
+    extensionMap.set('total', files.length);
+
+    // Creates Object from map
+    const fileTypes: {[key: string]: number} = {};
+    Object.assign(fileTypes, ...[...extensionMap.entries()].map(([k, v]) => ({[k]: v})));
+    telemetryReporter.sendTelemetryEvent('workspace/metadata', undefined, fileTypes);
+}
+
 (function initialize() {
     // insertion order matters.
     msEdgeBrowserMapping.set('Stable', {


Test patch:
diff --git a/test/extension.test.ts b/test/extension.test.ts
index 3c2d784..f9821df 100644
--- a/test/extension.test.ts
+++ b/test/extension.test.ts
@@ -43,6 +43,7 @@ describe("extension", () => {
                 removeTrailingSlash: jest.fn(removeTrailingSlash),
                 getJsDebugCDPProxyWebsocketUrl: jest.fn(),
                 getActiveDebugSessionId: jest.fn(),
+                reportFileExtensionTypes: jest.fn(),
             };
             jest.doMock("../src/utils", () => mockUtils);
             jest.doMock("../src/launchDebugProvider");
@@ -402,6 +403,7 @@ describe("extension", () => {
                 getJsDebugCDPProxyWebsocketUrl: jest.fn(),
                 buttonCode: { launch: '' },
                 reportUrlType: jest.fn(),
+                reportFileExtensionTypes: jest.fn(),
             };
 
             mockPanel = {
diff --git a/test/helpers/helpers.ts b/test/helpers/helpers.ts
index b1c512c..a2ac960 100644
--- a/test/helpers/helpers.ts
+++ b/test/helpers/helpers.ts
@@ -62,6 +62,14 @@ export function createFakeVSCode() {
             showTextDocument: jest.fn(),
         },
         workspace: {
+            findFiles: jest.fn(() => {
+                return [
+                    {path: '/c:/test/main.js'},
+                    {path: '/c:/test/styles.css'},
+                    {path: '/c:/test/react/test.jsx'},
+                    {path: '/c:/.vscode/launch.json'}
+                ];
+            }),
             getConfiguration: jest.fn(() => {
                 return { get: (name: string) => {
                     switch(name) {
diff --git a/test/utils.test.ts b/test/utils.test.ts
index bda8a26..91e72bd 100644
--- a/test/utils.test.ts
+++ b/test/utils.test.ts
@@ -975,4 +975,12 @@ describe("utils", () => {
             }
         });
     });
+
+    describe('reportFileExtensionTypes', () => {
+        it('correctly lists extension types in the workspace', async () => {
+            const reporter = createFakeTelemetryReporter();
+            await utils.reportFileExtensionTypes(reporter);
+            expect(reporter.sendTelemetryEvent).toBeCalledWith('workspace/metadata', undefined, {"css": 1, "js": 1, "json": 1, "jsx": 1, "total": 4});
+        });
+    });
 });