Skip to content

Commit

Permalink
Add @vscode/extension-telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawal-d committed Nov 24, 2023
1 parent 348f7a2 commit 4468ddb
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 18 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
[![Downloads](https://img.shields.io/visual-studio-marketplace/d/DivyanshuAgrawal.competitive-programming-helper)](https://marketplace.visualstudio.com/items?itemName=DivyanshuAgrawal.competitive-programming-helper)

Quickly compile, run and judge competitive programming problems in VS Code.
Automatically download testcases , or write & test your own problems.
Once you are done, easily your solutions directly with the click of a button!
Automatically download testcases , or write & test your own problems. Once you
are done, easily your solutions directly with the click of a button!

Cph supports a large number of popular platforms like Codeforces, Codechef, TopCoder etc.
with the help of competitive companion browser extension
Cph supports a large number of popular platforms like Codeforces, Codechef,
TopCoder etc. with the help of competitive companion browser extension

![Screenshot](screenshots/screenshot-main.png)

Expand Down Expand Up @@ -66,6 +66,12 @@ You can contribute to this extension in many ways:
**Before creating a Pull Request, please create an issue to discuss the
approach. It makes reviewing and accepting the PR much easier.**

## Telemetry

The extension collects basic events defined in `src/telmetry.ts`. To disable,
modify the setting `telemetry.telemetryLevel` (applies to all VSCode
extensions).

## License

Copyright (C) 2023 Divyanshu Agrawal
Expand Down
131 changes: 129 additions & 2 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-3.0-or-later",
"icon": "icon.png",
"publisher": "DivyanshuAgrawal",
"version": "5.16.0",
"version": "6.0.0",
"engines": {
"vscode": "^1.52.0"
},
Expand Down Expand Up @@ -318,7 +318,8 @@
"python-shell": "^5.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-textarea-autosize": "^8.5.3"
"react-textarea-autosize": "^8.5.3",
"@vscode/extension-telemetry": "^0.9.0"
},
"repository": {
"type": "git",
Expand Down
22 changes: 14 additions & 8 deletions src/companion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import { getProblemName } from './submit';
import { spawn } from 'child_process';
import { getJudgeViewProvider } from './extension';
import { words_in_text } from './utilsPure';
import telmetry from './telmetry';

const emptyResponse: CphEmptyResponse = { empty: true };
let savedResponse: CphEmptyResponse | CphSubmitResponse = emptyResponse;
const COMPANION_LOGGING = false;

export const submitKattisProblem = (problem: Problem) => {
globalThis.reporter.sendTelemetryEvent(telmetry.SUBMIT_TO_KATTIS);
const srcPath = problem.srcPath;
const homedir = require('os').homedir();

Check failure on line 29 in src/companion.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Require statement not part of import statement
let submitPath = `${homedir}/.kattis/submit.py`;
Expand Down Expand Up @@ -85,7 +87,7 @@ export const storeSubmitProblem = (problem: Problem) => {
sourceCode,
languageId,
};

globalThis.reporter.sendTelemetryEvent(telmetry.SUBMIT_TO_CODEFORCES);
console.log('Stored savedResponse', savedResponse);
};

Expand All @@ -103,10 +105,14 @@ export const setupCompanionServer = () => {
}
});
req.on('close', function () {
const problem: Problem = JSON.parse(rawProblem);
handleNewProblem(problem);
COMPANION_LOGGING &&
console.log('Companion server closed connection.');
try {
const problem: Problem = JSON.parse(rawProblem);
handleNewProblem(problem);
COMPANION_LOGGING &&
console.log('Companion server closed connection.');
} catch (e) {
// Ignore
}
});
res.write(JSON.stringify(savedResponse));
if (headers['cph-submit'] == 'true') {
Expand Down Expand Up @@ -158,6 +164,7 @@ export const getProblemFileName = (problem: Problem, ext: string) => {

/** Handle the `problem` sent by Competitive Companion, such as showing the webview, opening an editor, managing layout etc. */
const handleNewProblem = async (problem: Problem) => {
globalThis.reporter.sendTelemetryEvent(telmetry.GET_PROBLEM_FROM_COMPANION);
// If webview may be focused, close it, to prevent layout bug.
if (vscode.window.activeTextEditor == undefined) {
getJudgeViewProvider().extensionToJudgeViewMessage({
Expand Down Expand Up @@ -225,9 +232,8 @@ const handleNewProblem = async (problem: Problem) => {
`Template file does not exist: ${templateLocation}`,
);
} else {
const templateContents = readFileSync(
templateLocation,
).toString();
const templateContents =
readFileSync(templateLocation).toString();
writeFileSync(srcPath, templateContents);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
} from './preferences';
import * as vscode from 'vscode';
import { getJudgeViewProvider } from './extension';
import telmetry from './telmetry';
export let onlineJudgeEnv = false;

export const setOnlineJudgeEnv = (value: boolean) => {
onlineJudgeEnv = value;
globalThis.reporter.sendTelemetryEvent(telmetry.ONLINE_JUDGE_ENV);
console.log('online judge env:', onlineJudgeEnv);
};

Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export default {
telemetryKey: 'e57deb32-2bd3-4c02-89a5-d1f1ed0bc0d6',
port: 27121, // companion listener server
timeout: 10000, // for a testcase run
extensions: {
Expand Down
2 changes: 2 additions & 0 deletions src/executions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getTimeOutPref } from './preferences';
import * as vscode from 'vscode';
import path from 'path';
import { onlineJudgeEnv } from './compiler';
import telmetry from './telmetry';

const runningBinaries: ChildProcessWithoutNullStreams[] = [];

Expand Down Expand Up @@ -160,6 +161,7 @@ export const deleteBinary = (language: Language, binPath: string) => {

/** Kill all running binaries. Usually, only one should be running at a time. */
export const killRunning = () => {
globalThis.reporter.sendTelemetryEvent(telmetry.KILL_RUNNING);
console.log('Killling binaries');
runningBinaries.forEach((process) => process.kill());
};
10 changes: 8 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
import { submitToCodeForces, submitToKattis } from './submit';
import JudgeViewProvider from './webview/JudgeView';
import { getRetainWebviewContextPref } from './preferences';
import TelemetryReporter from '@vscode/extension-telemetry';
import config from './config';
import telmetry from './telmetry';

let judgeViewProvider: JudgeViewProvider;

Expand Down Expand Up @@ -62,13 +65,16 @@ const registerCommands = (context: vscode.ExtensionContext) => {
context.subscriptions.push(disposable2);
context.subscriptions.push(disposable3);
context.subscriptions.push(disposable4);
globalThis.reporter = new TelemetryReporter(config.telemetryKey);
context.subscriptions.push(globalThis.reporter);

globalThis.reporter.sendTelemetryEvent(telmetry.EXTENSION_ACTIVATED);
};

// This method is called when the extension is activated
export function activate(context: vscode.ExtensionContext) {
console.log('cph: activate() execution started');

(globalThis as any).context = context;
globalThis.context = context;

const statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
Expand Down
3 changes: 3 additions & 0 deletions src/runTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { compileFile } from './compiler';
import runAllAndSave from './webview/processRunAll';
import path from 'path';
import { getJudgeViewProvider } from './extension';
import telmetry from './telmetry';

/**
* Execution for the run testcases command. Runs all testcases for the active
Expand All @@ -14,6 +15,7 @@ import { getJudgeViewProvider } from './extension';
* create an empty testcases file and show it in the results section.
*/
export default async () => {
globalThis.reporter.sendTelemetryEvent(telmetry.RUN_ALL_TESTCASES);
console.log('Running command "runTestCases"');
const editor = vscode.window.activeTextEditor;
if (editor === undefined) {
Expand Down Expand Up @@ -50,6 +52,7 @@ export default async () => {
};

const createLocalProblem = async (editor: vscode.TextEditor) => {
globalThis.reporter.sendTelemetryEvent(telmetry.NEW_LOCAL_PROBLEM);
console.log('Creating local problem');
const srcPath = editor.document.fileName;
if (checkUnsupported(srcPath)) {
Expand Down
2 changes: 2 additions & 0 deletions src/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { getProblem } from './parser';
import * as vscode from 'vscode';
import { storeSubmitProblem, submitKattisProblem } from './companion';
import { getJudgeViewProvider } from './extension';
import telmetry from './telmetry';

export const submitToKattis = async () => {
globalThis.reporter.sendTelemetryEvent(telmetry.SUBMIT_TO_KATTIS);
const srcPath = vscode.window.activeTextEditor?.document.fileName;
if (!srcPath) {
vscode.window.showErrorMessage(
Expand Down
13 changes: 13 additions & 0 deletions src/telmetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
EXTENSION_ACTIVATED: 'extension-activated',
RUN_TESTCASE: 'run-testcase',
RUN_ALL_TESTCASES: 'run-all-testcases',
SUBMIT_TO_CODEFORCES: 'submit-to-codeforces',
SUBMIT_TO_KATTIS: 'submit-to-kattis',
GET_PROBLEM_FROM_COMPANION: 'get-problem-from-companion',
NEW_LOCAL_PROBLEM: 'new-local-problem',
KILL_RUNNING: 'kill-running',
DELETE_ALL_TESTCASES: 'delete-all-testcases',
ONLINE_JUDGE_ENV: 'online-judge-env',
USE_EXTENSION: 'use-extension',
};
Loading

0 comments on commit 4468ddb

Please sign in to comment.