Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
refactor: Cleans notification & analytics systems (#23)
Browse files Browse the repository at this point in the history
* feat: Improves notification flow

* refactor: Updates debug info logic

* refactor: Removes legacy sections

* docs: Fixes error messages

* feat: Adds action to warning message

* docs: Updates README

* style: Fixes formatting

* refactor: Switches .env vars of PostHog to config

* chore: Updates PostHog key

* fix: Fixes JSON

* chore: Updates PostHog analytics key

* docs: Updates posthog message

* feat: Cleans up analytics

* style: Fixes formatting
  • Loading branch information
frgfm authored Dec 13, 2023
1 parent 57f9a11 commit c417eb3
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Either:
The ultimate goal for this extension is to offer a smooth contribution experience for any developer.
The development efforts will be focused on achieving the following milestones:

- 🤝 Turn contribution guidelines into a live pair coding experience
- 🤝 Turn contribution guidelines into a live pair coding experience (code completion & code chat)
- 🐣 Help any developer find a starter contribution opportunity
- ⚖️ Ensure alignment between the contribution goal and the project's priorities

Expand Down
25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
"type": "string",
"default": "https://api.quackai.com",
"description": "Quack API endpoint"
},
"analytics.host": {
"type": "string",
"default": "https://eu.posthog.com",
"description": "The PostHog endpoint"
},
"analytics.key": {
"type": "string",
"default": "phc_mU5797H4W1GELY5wmpxxSK9EWZyW4Eev5WZOtuqXMPE",
"description": "The project key for PostHog"
}
}
}
Expand Down Expand Up @@ -317,6 +327,13 @@
"format:check": "npx prettier . --check",
"format:fix": "npx prettier . --write"
},
"dependencies": {
"axios": "^1.6.0",
"clipboardy": "^3.0.0",
"posthog-node": "^3.1.1",
"typescript": "^5.1.6",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/glob": "^8.1.0",
"@types/node": "20.2.5",
Expand All @@ -330,13 +347,5 @@
"ts-loader": "^9.4.3",
"webpack": "^5.85.0",
"webpack-cli": "^5.1.1"
},
"dependencies": {
"axios": "^1.6.0",
"clipboardy": "^3.0.0",
"dotenv": "^16.3.1",
"posthog-node": "^3.1.1",
"typescript": "^5.1.6",
"uuid": "^9.0.0"
}
}
13 changes: 5 additions & 8 deletions src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@

import * as vscode from "vscode";
import { PostHog } from "posthog-node";
import * as path from "path";
import * as dotenv from "dotenv";

let analyticsClient: PostHog | null = null;
const telemetryLevel: string = vscode.workspace
.getConfiguration("telemetry")
.get("telemetryLevel", "all");

// Env variables
const envPath = path.join(path.dirname(__dirname), ".env");
dotenv.config({ path: envPath });
let config = vscode.workspace.getConfiguration("analytics");

if (
process.env.POSTHOG_KEY &&
config.get("key") &&
vscode.env.isTelemetryEnabled &&
telemetryLevel === "all"
) {
analyticsClient = new PostHog(process.env.POSTHOG_KEY, {
host: process.env.POSTHOG_HOST,
analyticsClient = new PostHog(config.get("key") as string, {
host: config.get("host") || "https://app.posthog.com",
});
console.log("Collecting anonymized usage data");
console.log("Sending usage data");
}

export default analyticsClient;
145 changes: 105 additions & 40 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getSelectionRange,
getEditor,
} from "./util/session";
import { getRepoDetails, GitHubRepo } from "./util/github";
import { getUser, getRepoDetails, GitHubRepo } from "./util/github";
import {
analyzeSnippet,
checkSnippet,
Expand All @@ -42,11 +42,13 @@ function updateContext(context: vscode.ExtensionContext) {
}

export async function activate(context: vscode.ExtensionContext) {
// Generate or retrieve the user's UUID from storage
let stateId: string | undefined = context.workspaceState.get("userId");
// Fallback for analytics identifier: Generate or retrieve the user's UUID from storage
let stateId: string | undefined = context.workspaceState.get(
"quack-companion.userId",
);
const userId: string = stateId || uuidv4();
if (!stateId) {
context.workspaceState.update("userId", userId);
context.workspaceState.update("quack-companion.userId", userId);
}

// Config check
Expand All @@ -56,7 +58,13 @@ export async function activate(context: vscode.ExtensionContext) {
config.get("endpoint") as string,
);
if (!isValidEndpoint) {
vscode.window.showErrorMessage("Invalid API endpoint");
vscode.window
.showErrorMessage("Invalid API endpoint", "Configure endpoint")
.then((choice) => {
if (choice === "Configure endpoint") {
vscode.commands.executeCommand("quack-companion.setEndpoint");
}
});
}

// Side bar
Expand Down Expand Up @@ -91,7 +99,13 @@ export async function activate(context: vscode.ExtensionContext) {
"quack-companion.quackToken",
);
if (!quackToken) {
vscode.window.showErrorMessage("Please authenticate");
vscode.window
.showErrorMessage("Please authenticate", "Authenticate")
.then((choice) => {
if (choice === "Authenticate") {
vscode.commands.executeCommand("quack-companion.logIn");
}
});
return;
}
const guidelines: QuackGuideline[] = await fetchRepoGuidelines(
Expand All @@ -114,25 +128,29 @@ export async function activate(context: vscode.ExtensionContext) {

// If no guidelines exists, say it in the console
if (guidelines.length === 0) {
vscode.window.showInformationMessage("No guidelines specified yet.");
const answer = await vscode.window.showInformationMessage(
"No guidelines specified yet. Do you wish to request some?",
"yes",
"no",
);
if (answer === "yes") {
// add to waitlist
await addRepoToWaitlist(
ghRepo.id,
config.get("endpoint") as string,
quackToken,
);
}
vscode.window
.showInformationMessage(
"No guidelines specified yet. Do you wish to request some?",
"Request guidelines",
)
.then((choice) => {
if (choice === "Request guidelines") {
addRepoToWaitlist(
ghRepo.id,
config.get("endpoint") as string,
quackToken,
);
vscode.window.showInformationMessage(
"Request sent (automatic guideline extraction has been queued).",
);
}
});
}

// Telemetry
analyticsClient?.capture({
distinctId: userId,
distinctId:
context.workspaceState.get("quack-companion.userId") || userId,
event: "vscode-fetch-guidelines",
properties: {
repository: repoName,
Expand All @@ -151,16 +169,22 @@ export async function activate(context: vscode.ExtensionContext) {
const vscodeVersion = vscode.version;
const osName = os.platform();
const osVersion = os.release();
const info = `OS: ${osName} ${osVersion}\nVSCode Version: ${vscodeVersion}\nExtension Version: ${extensionVersion}\nEndpoint: ${config.get(
"endpoint",
)}`;
clipboardy.writeSync(info);
vscode.window.showInformationMessage("Version info copied to clipboard.");
if (extensionVersion === "N/A") {
vscode.window.showWarningMessage(
"Could not retrieve extension version.",
);
}
const info = `OS: ${osName} ${osVersion}\nVSCode Version: ${vscodeVersion}\nExtension Version: ${extensionVersion}\nEndpoint: ${config.get(
"endpoint",
)}`;
clipboardy.writeSync(info);
vscode.window.showInformationMessage("Version info copied to clipboard.");
// Telemetry
analyticsClient?.capture({
distinctId:
context.workspaceState.get("quack-companion.userId") || userId,
event: "vscode-debug-info",
});
}),
);

Expand All @@ -184,7 +208,14 @@ export async function activate(context: vscode.ExtensionContext) {
if (cachedGuidelines) {
guidelines = cachedGuidelines;
} else {
vscode.window.showErrorMessage("Please refresh guidelines");
vscode.window
.showErrorMessage("Please refresh guidelines", "Fetch guidelines")
.then((choice) => {
if (choice === "Fetch guidelines") {
vscode.commands.executeCommand("quack-companion.fetchGuidelines");
}
});

return;
}

Expand All @@ -193,7 +224,13 @@ export async function activate(context: vscode.ExtensionContext) {
"quack-companion.quackToken",
);
if (!quackToken) {
vscode.window.showErrorMessage("Please authenticate");
vscode.window
.showErrorMessage("Please authenticate", "Authenticate")
.then((choice) => {
if (choice === "Authenticate") {
vscode.commands.executeCommand("quack-companion.logIn");
}
});
return;
}
// Check compliance
Expand Down Expand Up @@ -225,11 +262,6 @@ export async function activate(context: vscode.ExtensionContext) {
});
complianceStatus.forEach((item: ComplianceResult, index: number) => {
if (!item.is_compliant) {
vscode.window.showWarningMessage(
guidelines[guidelineIndexMap[item.guideline_id]].title +
". " +
item.comment,
);
const diagnostic = new vscode.Diagnostic(
selectionRange,
guidelines[guidelineIndexMap[item.guideline_id]].title +
Expand Down Expand Up @@ -258,7 +290,8 @@ export async function activate(context: vscode.ExtensionContext) {

// Telemetry
analyticsClient?.capture({
distinctId: userId,
distinctId:
context.workspaceState.get("quack-companion.userId") || userId,
event: "vscode-analyze-code",
properties: {
repository: repoName,
Expand All @@ -279,7 +312,13 @@ export async function activate(context: vscode.ExtensionContext) {
"quack-companion.quackToken",
);
if (!quackToken) {
vscode.window.showErrorMessage("Please authenticate");
vscode.window
.showErrorMessage("Please authenticate", "Authenticate")
.then((choice) => {
if (choice === "Authenticate") {
vscode.commands.executeCommand("quack-companion.logIn");
}
});
return;
}
// Status bar
Expand All @@ -303,7 +342,15 @@ export async function activate(context: vscode.ExtensionContext) {
if (cachedGuidelines) {
guidelines = cachedGuidelines;
} else {
vscode.window.showErrorMessage("Please refresh guidelines");
vscode.window
.showErrorMessage("Please refresh guidelines", "Fetch guidelines")
.then((choice) => {
if (choice === "Fetch guidelines") {
vscode.commands.executeCommand(
"quack-companion.fetchGuidelines",
);
}
});
return;
}
// Notify the webview to update its content
Expand All @@ -315,9 +362,6 @@ export async function activate(context: vscode.ExtensionContext) {
const selectionRange = getSelectionRange();
var diagnostics: vscode.Diagnostic[] = [];
if (!complianceStatus.is_compliant) {
vscode.window.showWarningMessage(
item.guideline.title + ". " + complianceStatus.comment,
);
const diagnostic = new vscode.Diagnostic(
selectionRange,
item.guideline.title + "\n\n" + complianceStatus.comment,
Expand All @@ -330,7 +374,8 @@ export async function activate(context: vscode.ExtensionContext) {
statusBarItem.dispose();
// Telemetry
analyticsClient?.capture({
distinctId: userId,
distinctId:
context.workspaceState.get("quack-companion.userId") || userId,
event: "vscode-analyze-code-mono",
properties: {
repository: await getCurrentRepoName(),
Expand Down Expand Up @@ -382,6 +427,11 @@ export async function activate(context: vscode.ExtensionContext) {
} else {
vscode.window.showErrorMessage("Quack endpoint URL is required");
}
analyticsClient?.capture({
distinctId:
context.workspaceState.get("quack-companion.userId") || userId,
event: "vscode-set-endpoint",
});
}),
);

Expand All @@ -397,6 +447,12 @@ export async function activate(context: vscode.ExtensionContext) {
"quack-companion.githubToken",
session.accessToken,
);
// Analytics identifier
const githubUser = await getUser(session.accessToken);
await context.workspaceState.update(
"quack-companion.userId",
githubUser.id,
);
// Quack login
const quackToken = await authenticate(
session.accessToken,
Expand All @@ -411,6 +467,10 @@ export async function activate(context: vscode.ExtensionContext) {
// Make state available to viewsWelcome
updateContext(context);
}
analyticsClient?.capture({
distinctId: githubUser.id,
event: "vscode-login",
});
}),
);
context.subscriptions.push(
Expand All @@ -427,6 +487,11 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage("Logout successful");
// Make state available to viewsWelcome
updateContext(context);
analyticsClient?.capture({
distinctId:
context.workspaceState.get("quack-companion.userId") || userId,
event: "vscode-logout",
});
}),
);
// Update context
Expand Down
Loading

0 comments on commit c417eb3

Please sign in to comment.