2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2020-03-04 16:31:54 +00:00
|
|
|
* Copyright 2019-2020 Balena Ltd.
|
2019-03-12 22:07:57 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2020-03-04 16:31:54 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
import * as Mixpanel from 'mixpanel';
|
2019-03-12 22:07:57 +00:00
|
|
|
|
2020-03-04 16:31:54 +00:00
|
|
|
import * as packageJSON from '../package.json';
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from './utils/lazy';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-02-29 21:14:01 +00:00
|
|
|
const getMixpanel = _.once((balenaUrl: string) => {
|
2019-09-18 15:00:22 +00:00
|
|
|
return Mixpanel.init('balena-main', {
|
2020-02-29 21:14:01 +00:00
|
|
|
host: `api.${balenaUrl}`,
|
2019-02-07 15:36:48 +00:00
|
|
|
path: '/mixpanel',
|
|
|
|
protocol: 'https',
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-05-04 13:40:12 +00:00
|
|
|
interface CachedUsername {
|
|
|
|
token: string;
|
|
|
|
username: string;
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:31:54 +00:00
|
|
|
/**
|
2020-10-20 21:11:17 +00:00
|
|
|
* Mixpanel.com analytics tracking (information on balena CLI usage).
|
2020-03-04 16:31:54 +00:00
|
|
|
*
|
|
|
|
* @param commandSignature A string like, for example:
|
2021-07-15 13:41:38 +00:00
|
|
|
* "push <fleetOrDevice>"
|
|
|
|
* That's literally so: "fleetOrDevice" is NOT replaced with the actual
|
|
|
|
* fleet ID or device ID. The purpose is to find out the most / least
|
2020-03-04 16:31:54 +00:00
|
|
|
* used command verbs, so we can focus our development effort where it is most
|
|
|
|
* beneficial to end users.
|
|
|
|
*
|
|
|
|
* The username and command signature are also added as extra context
|
|
|
|
* information in Sentry.io error reporting, for CLI debugging purposes
|
|
|
|
* (mainly unexpected/unhandled exceptions -- see also `lib/errors.ts`).
|
2020-10-20 21:11:17 +00:00
|
|
|
*
|
|
|
|
* For more details on the data collected by balena generally, check this page:
|
|
|
|
* https://www.balena.io/docs/learn/more/collected-data/
|
2020-03-04 16:31:54 +00:00
|
|
|
*/
|
2020-05-04 09:04:44 +00:00
|
|
|
export async function trackCommand(commandSignature: string) {
|
2020-07-01 14:26:40 +00:00
|
|
|
try {
|
2020-10-09 12:06:09 +00:00
|
|
|
let Sentry: typeof import('@sentry/node');
|
|
|
|
if (!process.env.BALENARC_NO_SENTRY) {
|
|
|
|
Sentry = await import('@sentry/node');
|
|
|
|
Sentry.configureScope((scope) => {
|
|
|
|
scope.setExtra('command', commandSignature);
|
|
|
|
});
|
|
|
|
}
|
2020-07-01 14:26:40 +00:00
|
|
|
const settings = await import('balena-settings-client');
|
2021-06-25 23:42:07 +00:00
|
|
|
const balenaUrl = settings.get<string>('balenaUrl');
|
2020-05-04 13:40:12 +00:00
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
const username = await (async () => {
|
2020-05-04 13:40:12 +00:00
|
|
|
const getStorage = await import('balena-settings-storage');
|
2021-06-25 23:42:07 +00:00
|
|
|
const dataDirectory = settings.get<string>('dataDirectory');
|
2020-05-04 13:40:12 +00:00
|
|
|
const storage = getStorage({ dataDirectory });
|
|
|
|
let token;
|
|
|
|
try {
|
|
|
|
token = await storage.get('token');
|
|
|
|
} catch {
|
|
|
|
// If we can't get a token then we can't get a username
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const result = (await storage.get('cachedUsername')) as CachedUsername;
|
|
|
|
if (result.token === token) {
|
|
|
|
return result.username;
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const balena = getBalenaSdk();
|
2020-07-01 14:26:40 +00:00
|
|
|
const $username = await balena.auth.whoami();
|
2020-11-18 00:27:11 +00:00
|
|
|
await storage.set('cachedUsername', {
|
2020-05-04 13:40:12 +00:00
|
|
|
token,
|
2020-07-01 14:26:40 +00:00
|
|
|
username: $username,
|
2020-05-04 13:40:12 +00:00
|
|
|
} as CachedUsername);
|
2020-07-01 14:26:40 +00:00
|
|
|
return $username;
|
2020-05-04 13:40:12 +00:00
|
|
|
} catch {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-01 14:26:40 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
const mixpanel = getMixpanel(balenaUrl);
|
|
|
|
|
2020-10-09 12:06:09 +00:00
|
|
|
if (!process.env.BALENARC_NO_SENTRY) {
|
|
|
|
Sentry!.configureScope((scope) => {
|
|
|
|
scope.setUser({
|
|
|
|
id: username,
|
|
|
|
username,
|
|
|
|
});
|
2020-03-04 16:31:54 +00:00
|
|
|
});
|
2020-10-09 12:06:09 +00:00
|
|
|
}
|
2020-11-26 12:47:48 +00:00
|
|
|
// Don't actually call mixpanel.track() while running test cases, or if suppressed
|
|
|
|
if (
|
|
|
|
!process.env.BALENA_CLI_TEST_TYPE &&
|
|
|
|
!process.env.BALENARC_NO_ANALYTICS
|
|
|
|
) {
|
2020-07-09 18:48:06 +00:00
|
|
|
await mixpanel.track(`[CLI] ${commandSignature}`, {
|
|
|
|
distinct_id: username,
|
|
|
|
version: packageJSON.version,
|
|
|
|
node: process.version,
|
|
|
|
arch: process.arch,
|
|
|
|
balenaUrl, // e.g. 'balena-cloud.com' or 'balena-staging.com'
|
|
|
|
platform: process.platform,
|
|
|
|
});
|
|
|
|
}
|
2020-07-01 14:26:40 +00:00
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|