Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry #82

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ src/**
package-lock.json
tsconfig.json
esbuild.js
**/*.map
**/*.map
build
!build/extension.js
!build/bin
34 changes: 18 additions & 16 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const { build } = require('esbuild');
const { copy } = require('esbuild-plugin-copy');
const path = require("path");

const production = process.argv.includes('--production');

async function main() {
const files = {
'docs/**': './docs',
'images/**': './images',
'snippets/**': './snippets',
'syntaxes/**': './syntaxes',
'CHANGELOG.md': './CHANGELOG.md',
'LICENSE.md': './LICENSE.md',
'README.md': './README.md',
'language-configuration.json': './language-configuration.json',
'package.json': './package.json',
'node_modules/mermaid/dist/mermaid.min.js': 'media',
'images/**': 'images',
'snippets/**': 'snippets',
'syntaxes/**': 'syntaxes',
'CHANGELOG.md': 'CHANGELOG.md',
'LICENSE.md': 'LICENSE.md',
'README.md': 'README.md',
'language-configuration.json': 'language-configuration.json',
'package.json': 'package.json',
'node_modules/mermaid/dist/mermaid.min.js': 'media/mermaid.min.js'
};
if( !production )
files['language-server/build/libs/language-server-all.jar'] = 'bin'
if (!production)
files['language-server/build/libs/language-server-all.jar'] = 'bin/language-server-all.jar';
await build({
entryPoints: [
'src/extension.ts'
Expand All @@ -33,10 +33,12 @@ async function main() {
logLevel: 'silent',
plugins: [
copy({
assets: Object.entries(files).map(([from, to]) => {
return { from, to };
}),
}),
resolveFrom: 'cwd',
assets: Object.entries(files).map(([from, to]) => ({
from,
to: path.join('build', to)
}))
})
],
});
}
Expand Down
54 changes: 52 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"Programming Languages"
],
"activationEvents": [],
"main": "./extension.js",
"main": "./build/extension.js",
"contributes": {
"languages": [
{
Expand Down Expand Up @@ -124,6 +124,11 @@
"command": "nextflow.writeTest",
"title": "Write nf-test with Seqera",
"category": "Nextflow"
},
{
"command": "nextflow.updateTelemetryConsent",
"title": "Update telemetry consent",
"category": "Nextflow"
}
],
"configuration": {
Expand Down Expand Up @@ -209,7 +214,7 @@
"scripts": {
"check-types": "tsc --noEmit",
"compile": "npm run check-types && node esbuild.js",
"package": "npm run check-types && node esbuild.js --production && (cd build ; vsce package -o nextflow.vsix)"
"package": "npm run check-types && node esbuild.js --production && vsce package -o build/nextflow.vsix"
},
"devDependencies": {
"@types/node": "^20",
Expand All @@ -222,6 +227,7 @@
"node-fetch": "^3.3.2",
"typescript": "^5.7.2",
"vscode-jsonrpc": "^8.0.2",
"vscode-languageclient": "^8.0.2"
"vscode-languageclient": "^8.0.2",
"posthog-node": "^4.4.1"
}
}
12 changes: 10 additions & 2 deletions src/activateChatbot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as vscode from "vscode";
import { createHandler } from "./chatbot";
import type { TrackEvent } from "./activateTelemetry";

export function activateChatbot(context: vscode.ExtensionContext) {
// Don't activate chatbot in Cursor
export function activateChatbot(
context: vscode.ExtensionContext,
trackEvent: TrackEvent
) {
console.log("🟢 Seqera extension activated");

// Handle Cursor
if (vscode.env.appName.includes("Cursor")) {
return;
}
Expand All @@ -25,6 +31,7 @@ export function activateChatbot(context: vscode.ExtensionContext) {
query: "@Seqera ",
isPartialQuery: true,
});
trackEvent("openChat", { someData: 123 });
}
);

Expand All @@ -34,6 +41,7 @@ export function activateChatbot(context: vscode.ExtensionContext) {
await vscode.commands.executeCommand("workbench.action.chat.open", {
query: "@Seqera /nf-test",
});
trackEvent("writeTest", { someData: 123 });
}
);

Expand Down
9 changes: 8 additions & 1 deletion src/activateLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Executable,
} from "vscode-languageclient/node";

import type { TrackEvent } from "./activateTelemetry";

const LABEL_RELOAD_WINDOW = "Reload Window";
let extensionContext: vscode.ExtensionContext | null = null;
let languageClient: LanguageClient | null = null;
Expand Down Expand Up @@ -55,6 +57,7 @@ async function getLanguageServerPath() {
// use development build if present
const devPath = path.resolve(
extensionContext.extensionPath,
"build",
"bin",
"language-server-all.jar"
);
Expand Down Expand Up @@ -263,7 +266,10 @@ function onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
}
}

function activateLanguageServer(context: vscode.ExtensionContext) {
function activateLanguageServer(
context: vscode.ExtensionContext,
trackEvent: TrackEvent
) {
javaPath = findJava();
extensionContext = context;
vscode.workspace.onDidChangeConfiguration(onDidChangeConfiguration);
Expand All @@ -280,6 +286,7 @@ function activateLanguageServer(context: vscode.ExtensionContext) {
vscode.window.showErrorMessage("Missing file URI.");
return;
}
trackEvent("openFileFromWebview", { uri: uriString });
const uri = vscode.Uri.parse(uriString);
await vscode.window.showTextDocument(uri, {
viewColumn: vscode.ViewColumn.One,
Expand Down
124 changes: 124 additions & 0 deletions src/activateTelemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import * as vscode from "vscode";
import { PostHog } from "posthog-node";
import { randomBytes } from "crypto";
import promptForTelemetryConsent from "./utils/promptForTelemetryConsent";
export type TrackEvent = (
eventName: string,
properties?: { [key: string]: any }
) => void;

const key = "phc_pCt2zPQylp5x5dEKMB3TLM2hKBp7aLajUBgAfysPnpd";
const host = "https://eu.i.posthog.com";

let posthogClient: PostHog | undefined;
let hasGlobalConsent = false;
let userConsentState: "accepted" | "declined" | undefined;
let hasUserAccepted = false;
let consentChanged = false;

export async function activateTelemetry(
context: vscode.ExtensionContext
): Promise<TrackEvent> {
// Get config & basic info
const config = vscode.workspace.getConfiguration("telemetry");
const vscodeVersion = vscode.version;
const osPlatform = process.platform;
const extension = vscode.extensions.getExtension("nextflow.nextflow");
const extensionVersion = extension?.packageJSON.version ?? "unknown";

// Get consent state
hasGlobalConsent = config.get<boolean>("enableTelemetry", true);
userConsentState = context.globalState.get("telemetryConsent");
hasUserAccepted = userConsentState === "accepted";

// Add command to update consent
const updateConsent = vscode.commands.registerCommand(
"nextflow.updateTelemetryConsent",
() => {
context.globalState.update("telemetryConsent", undefined);
promptForTelemetryConsent(context);
}
);
context.subscriptions.push(updateConsent);

// If declined already, return a noop
if (!hasGlobalConsent) return () => {};

// If unknown, show the prompt
if (!userConsentState) {
const consentGiven = await promptForTelemetryConsent(context);
hasUserAccepted = consentGiven === true;
consentChanged = true;
}

// If declined, return a noop
if (!hasUserAccepted) return () => {};

// Otherwise proceed with tracking
posthogClient = new PostHog(key, { host });
const trackEvent = createTrackEvent(context);

// Track consent change
if (consentChanged) {
trackEvent("telemetryConsent", {
accepted: hasUserAccepted,
});
}

// Track extension activation
trackEvent("extensionActivated", {
extensionVersion,
vscodeVersion,
osPlatform,
});

// Track file open
const fileOpenEvent = vscode.workspace.onDidOpenTextDocument((document) => {
const filePath = document.fileName;
trackEvent("fileOpened", {
fileName: filePath,
});
});

context.subscriptions.push(fileOpenEvent);

return trackEvent;
}

function createTrackEvent(context: vscode.ExtensionContext) {
return async (eventName: string, properties = {}) => {
try {
if (!posthogClient) return;
posthogClient.capture({
distinctId: getUserID(context),
event: eventName,
properties: {
...properties,
time: new Date().toISOString(),
},
});
} catch (err) {
console.error("Track event failed", err);
}
};
}

function getUserID(context: vscode.ExtensionContext): string {
let anonId = context.globalState.get<string>("anonId");
if (!anonId) {
anonId = randomBytes(6).toString("hex");
context.globalState.update("anonId", anonId);
}
return anonId;
}

export function deactivateTelemetry(context: vscode.ExtensionContext) {
if (!hasUserAccepted || !posthogClient) return;

posthogClient.capture({
distinctId: getUserID(context),
event: "extensionDeactivated",
});

return posthogClient.shutdown();
}
Loading