2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
import Promise = require('bluebird');
|
2017-12-20 21:46:01 +00:00
|
|
|
import _ = require('lodash');
|
|
|
|
import Mixpanel = require('mixpanel');
|
|
|
|
import Raven = require('raven');
|
2019-03-12 22:07:57 +00:00
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
import packageJSON = require('../package.json');
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from './utils/lazy';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-02-22 07:59:36 +00:00
|
|
|
const getMixpanel = _.once<any>(() => {
|
2019-02-07 15:36:48 +00:00
|
|
|
const settings = require('balena-settings-client');
|
2019-09-18 15:00:22 +00:00
|
|
|
return Mixpanel.init('balena-main', {
|
2019-02-07 15:36:48 +00:00
|
|
|
host: `api.${settings.get('balenaUrl')}`,
|
|
|
|
path: '/mixpanel',
|
|
|
|
protocol: 'https',
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-08-28 01:14:19 +00:00
|
|
|
export function trackCommand(commandSignature: string) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2017-12-20 21:46:01 +00:00
|
|
|
return Promise.props({
|
2018-10-19 14:38:50 +00:00
|
|
|
balenaUrl: balena.settings.get('balenaUrl'),
|
|
|
|
username: balena.auth.whoami().catchReturn(undefined),
|
2018-01-04 14:07:55 +00:00
|
|
|
mixpanel: getMixpanel(),
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(({ username, balenaUrl, mixpanel }) => {
|
2019-08-28 01:14:19 +00:00
|
|
|
return Promise.try(() => {
|
2018-01-09 15:05:24 +00:00
|
|
|
Raven.mergeContext({
|
|
|
|
user: {
|
|
|
|
id: username,
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
});
|
2019-08-28 01:14:19 +00:00
|
|
|
// commandSignature is a string like, for example:
|
2019-07-22 12:27:32 +00:00
|
|
|
// "push <applicationOrDevice>"
|
|
|
|
// That's literally so: "applicationOrDevice" is NOT replaced with
|
|
|
|
// the actual application ID or device ID. The purpose is find out the
|
|
|
|
// most / least used command verbs, so we can focus our development
|
|
|
|
// effort where it is most beneficial to end users.
|
2019-08-28 01:14:19 +00:00
|
|
|
return mixpanel.track(`[CLI] ${commandSignature}`, {
|
2018-01-09 15:05:24 +00:00
|
|
|
distinct_id: username,
|
|
|
|
version: packageJSON.version,
|
|
|
|
node: process.version,
|
|
|
|
arch: process.arch,
|
2019-07-22 12:27:32 +00:00
|
|
|
balenaUrl, // e.g. 'balena-cloud.com' or 'balena-staging.com'
|
2018-01-09 15:05:24 +00:00
|
|
|
platform: process.platform,
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
|
|
|
.timeout(100)
|
|
|
|
.catchReturn(undefined);
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|