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 BalenaSdk = require('balena-sdk');
|
|
|
|
import Promise = require('bluebird');
|
2017-12-20 21:46:01 +00:00
|
|
|
import * as Capitano from 'capitano';
|
|
|
|
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');
|
|
|
|
|
2019-01-12 21:20:19 +00:00
|
|
|
const getBalenaSdk = _.once(() => BalenaSdk.fromSharedOptions());
|
2017-12-20 21:46:01 +00:00
|
|
|
const getMatchCommandAsync = Promise.promisify(Capitano.state.getMatchCommand);
|
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');
|
|
|
|
return Mixpanel.init('00000000000000000000000000000000', {
|
|
|
|
host: `api.${settings.get('balenaUrl')}`,
|
|
|
|
path: '/mixpanel',
|
|
|
|
protocol: 'https',
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
export function trackCommand(capitanoCli: Capitano.Cli) {
|
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 }) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
return getMatchCommandAsync(capitanoCli.command).then(command => {
|
|
|
|
Raven.mergeContext({
|
|
|
|
user: {
|
|
|
|
id: username,
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
});
|
2019-07-22 12:27:32 +00:00
|
|
|
// `command.signature.toString()` results in a string like, for example:
|
|
|
|
// "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.
|
2018-01-09 15:05:24 +00:00
|
|
|
return mixpanel.track(`[CLI] ${command.signature.toString()}`, {
|
|
|
|
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
|
|
|
}
|