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 Opt-Out Feature - Fixes #62 #65

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
42 changes: 24 additions & 18 deletions src/bin/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const {PYTHAGORA_METADATA_DIR, CONFIG_FILENAME, PYTHAGORA_API_SERVER} = require("@pythagora.io/js-code-processing").common;
const { PYTHAGORA_METADATA_DIR, CONFIG_FILENAME, PYTHAGORA_API_SERVER } = require("@pythagora.io/js-code-processing").common;
const packageJson = require('../../package.json');

const pythagoraVersion = packageJson.version;
const configPath = path.join(process.cwd(), '../..', PYTHAGORA_METADATA_DIR, CONFIG_FILENAME);

// Check if telemetry is enabled using an environment variable
const telemetryEnabled = process.env.PYTHAGORA_TELEMETRY_ENABLED !== 'false';
adarsh-jha-dev marked this conversation as resolved.
Show resolved Hide resolved

// Calculate the SHA-256 hash of the installation directory
const installationDirectory = path.join(process.cwd(), '../..');
const hash = crypto.createHash('sha256');
Expand All @@ -19,11 +22,10 @@ let config;
try {
config = JSON.parse(fs.readFileSync(configPath));
} catch (err) {
// Config file doesn't exist or is not valid JSON
console.error('Config file does not exist or is not valid JSON. Creating a new one.');
adarsh-jha-dev marked this conversation as resolved.
Show resolved Hide resolved
config = {};
}

if (!config) config = {};

// If there's no userId, generate one and save it to the config file
if (!config.userId) {
config.userId = uuidv4();
Expand All @@ -34,18 +36,22 @@ if (!config.userId) {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
}

// Send the request to the telemetry endpoint
const telemetryData = {
userId: config.userId,
pathId,
event: 'install',
pythagoraVersion
};
// Send telemetry data only if telemetry is enabled
if (telemetryEnabled) {
const telemetryData = {
userId: config.userId,
pathId,
event: 'install',
pythagoraVersion
};

axios.post(`${PYTHAGORA_API_SERVER}/telemetry`, telemetryData)
.then((res) => {
console.log('Telemetry data sent successfully');
})
.catch((err) => {
console.error(`Failed to send telemetry data: ${err.message}`);
});
axios.post(`${PYTHAGORA_API_SERVER}/telemetry`, telemetryData)
.then((res) => {
console.log('Telemetry data sent successfully');
})
.catch((err) => {
console.error(`Failed to send telemetry data: ${err.message}`);
});
} else {
console.log('Telemetry is disabled. Data will not be sent.');
}
4 changes: 2 additions & 2 deletions src/templates/jest-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
testEnvironment: "node",
globalSetup: './pythagora_tests/exported_tests/global-setup.js',
globalSetup: './src/templates/jest-global-setup.js',
roots: [
"<rootDir>/pythagora_tests/exported_tests"
"<rootDir>/pythagora_tests/unit/src"
],
};
13 changes: 6 additions & 7 deletions src/templates/jest-global-setup.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = async () => {
// Check if the developer has opted out of telemetry and set the environment variable accordingly
const optOutTelemetry = process.env.PYTHAGORA_TELEMETRY_ENABLED === 'false';

// function that sets up Mongo to be used in tests
// global.setUpDb = () => {};
if (optOutTelemetry) {
process.env.PYTHAGORA_TELEMETRY_ENABLED = 'false';
adarsh-jha-dev marked this conversation as resolved.
Show resolved Hide resolved
}

// function that cleans up the database after tests are done
// global.cleanUpDb = () => {};

// function that returns a Mongo collection so that tests can query the database
// global.getMongoCollection = (collection) => {};
console.log(`Telemetry is ${optOutTelemetry ? 'disabled' : 'enabled'}`);
};