Skip to content

Commit

Permalink
fix: make analytics typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjeffos committed Dec 15, 2020
1 parent 5c6d1b6 commit 714ba35
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 139 deletions.
4 changes: 2 additions & 2 deletions src/cli/index.ts
Expand Up @@ -52,7 +52,7 @@ const EXIT_CODES = {
async function runCommand(args: Args) {
const commandResult = await args.method(...args.options._);

const res = analytics({
const res = analytics.addDataAndSend({
args: args.options._,
command: args.command,
org: args.options.org,
Expand Down Expand Up @@ -156,7 +156,7 @@ async function handleError(args, error) {
analytics.add('command', args.command);
}

const res = analytics({
const res = analytics.addDataAndSend({
args: args.options._,
command,
org: args.options.org,
Expand Down
42 changes: 30 additions & 12 deletions src/lib/analytics.js → src/lib/analytics.ts
@@ -1,6 +1,3 @@
module.exports = analytics;
module.exports.single = postAnalytics;

const snyk = require('../lib');
const config = require('./config');
const version = require('./version');
Expand All @@ -19,13 +16,21 @@ const osName = require('os-name');
const crypto = require('crypto');
const uuid = require('uuid');
const stripAnsi = require('strip-ansi');
import * as needle from 'needle';
const { MetricsCollector } = require('./metrics');

const metadata = {};
// analytics module is required at the beginning of the CLI run cycle
const startTime = Date.now();

function analytics(data) {
/**
*
* @param data the data to merge into that data which has been staged thus far (with the {@link add} function)
* and then sent to the backend.
*/
export function addDataAndSend(
data,
): Promise<void | { res: needle.NeedleResponse; body: any }> {
if (!data) {
data = {};
}
Expand All @@ -43,18 +48,25 @@ function analytics(data) {
return postAnalytics(data);
}

analytics.allowAnalytics = () => {
export function allowAnalytics(): boolean {
if (snyk.config.get('disable-analytics') || config.DISABLE_ANALYTICS) {
return false;
} else {
return true;
}
};
}

function postAnalytics(data) {
/**
* Actually send the analytics to the backend. This can be used standalone to send only the data
* given by the data parameter, or called from {@link addDataAndSend}.
* @param data the analytics data to send to the backend.
*/
export function postAnalytics(
data,
): Promise<void | { res: needle.NeedleResponse; body: any }> {
// if the user opt'ed out of analytics, then let's bail out early
// ths applies to all sending to protect user's privacy
if (!analytics.allowAnalytics()) {
if (!allowAnalytics()) {
debug('analytics disabled');
return Promise.resolve();
}
Expand Down Expand Up @@ -82,7 +94,7 @@ function postAnalytics(data) {

const headers = {};
if (snyk.api) {
headers.authorization = 'token ' + snyk.api;
headers['authorization'] = 'token ' + snyk.api;
}

data.ci = isCI();
Expand All @@ -105,7 +117,7 @@ function postAnalytics(data) {

const queryStringParams = {};
if (data.org) {
queryStringParams.org = data.org;
queryStringParams['org'] = data.org;
}

debug('analytics', data);
Expand All @@ -131,7 +143,13 @@ function postAnalytics(data) {
});
}

analytics.add = function(key, value) {
/**
* Adds a key-value pair to the analytics data `metadata` field. This doesn't send the analytis, just stages it for
* sending later (via the {@link addDataAndSend} function).
* @param key
* @param value
*/
export function add(key, value) {
if (typeof value === 'string') {
value = stripAnsi(value);
}
Expand All @@ -143,4 +161,4 @@ analytics.add = function(key, value) {
} else {
metadata[key] = value;
}
};
}
2 changes: 1 addition & 1 deletion src/lib/protect/apply-patch.js
Expand Up @@ -7,7 +7,7 @@ const path = require('path');
const fs = require('fs');
const uuid = require('uuid/v4');
const semver = require('semver');
const errorAnalytics = require('../analytics').single;
const errorAnalytics = require('../analytics').postAnalytics;

function applyPatch(patchFileName, vuln, live, patchUrl) {
let cwd = vuln.source;
Expand Down
10 changes: 5 additions & 5 deletions test/analytics.spec.ts
Expand Up @@ -4,7 +4,7 @@ describe('Analytics basic testing', () => {
it('Has all analytics arguments', async () => {
analytics.add('foo', 'bar');
const data = { args: [], command: '__test__' };
const res = await analytics(data);
const res = await analytics.addDataAndSend(data);
if (!res) {
throw 'analytics creation failed!';
}
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('Analytics basic testing', () => {
it('Has all analytics arguments when org is specified', async () => {
analytics.add('foo', 'bar');
const data = { args: [], command: '__test__', org: '__snyk__' };
const res = await analytics(data);
const res = await analytics.addDataAndSend(data);
if (!res) {
throw 'analytics creation failed!';
}
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('Analytics basic testing', () => {
args: [{ integrationName: 'JENKINS', integrationVersion: '1.2.3' }],
command: '__test__',
};
const res = await analytics(data);
const res = await analytics.addDataAndSend(data);
if (!res) {
throw 'analytics creation failed!';
}
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('Analytics basic testing', () => {
command: '__test__',
org: '__snyk__',
};
const res = await analytics(data);
const res = await analytics.addDataAndSend(data);
if (!res) {
throw 'analytics creation failed!';
}
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('Analytics basic testing', () => {
command: '__test__',
org: '__snyk__',
};
const res = await analytics(data);
const res = await analytics.addDataAndSend(data);
if (!res) {
throw 'analytics creation failed!';
}
Expand Down

0 comments on commit 714ba35

Please sign in to comment.