UNPKG

3.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.sendCommand = exports.Telemetry = void 0;
4const Debug = require("debug");
5const lodash = require("lodash");
6const helper_1 = require("./helper");
7const uuid_1 = require("./utils/uuid");
8const debug = Debug('ionic:lib:telemetry');
9const GA_CODE = 'UA-44023830-30';
10let _gaTracker;
11class Telemetry {
12 constructor({ config, client, getInfo, ctx, project, session }) {
13 this.client = client;
14 this.config = config;
15 this.getInfo = getInfo;
16 this.ctx = ctx;
17 this.project = project;
18 this.session = session;
19 }
20 async sendCommand(command, args) {
21 debug('Sending telemetry for command: %O %O', command, args);
22 await helper_1.sendMessage({ config: this.config, ctx: this.ctx }, { type: 'telemetry', data: { command, args } });
23 }
24}
25exports.Telemetry = Telemetry;
26async function getLeek({ config, version }) {
27 if (!_gaTracker) {
28 const Leek = await Promise.resolve().then(() => require('leek'));
29 let telemetryToken = config.get('tokens.telemetry');
30 if (!telemetryToken) {
31 telemetryToken = uuid_1.generateUUID();
32 config.set('tokens.telemetry', telemetryToken);
33 debug(`setting telemetry token to ${telemetryToken}`);
34 }
35 _gaTracker = new Leek({
36 name: telemetryToken,
37 trackingCode: GA_CODE,
38 globalName: 'ionic',
39 version,
40 silent: !config.get('telemetry'),
41 });
42 }
43 return _gaTracker;
44}
45async function sendCommand({ config, client, getInfo, ctx, session, project }, command, args) {
46 const messageList = [];
47 const name = 'command execution';
48 const prettyArgs = args.map(a => a.includes(' ') ? `"${a}"` : a);
49 const message = messageList.concat([command], prettyArgs).join(' ');
50 await Promise.all([
51 (async () => {
52 const leek = await getLeek({ config, version: ctx.version });
53 try {
54 await leek.track({ name, message });
55 }
56 catch (e) {
57 debug(`leek track error: ${e.stack ? e.stack : e}`);
58 }
59 })(),
60 (async () => {
61 const now = new Date().toISOString();
62 const appflowId = project ? project.config.get('id') : undefined;
63 const info = await getInfo();
64 const results = info.map(r => r.key ? { [r.key]: r.value } : undefined).filter(r => !!r);
65 const { req } = await client.make('POST', '/events/metrics');
66 const metric = {
67 'name': 'cli_command_metrics',
68 'timestamp': now,
69 'session_id': config.get('tokens.telemetry'),
70 'source': 'cli',
71 'value': {
72 'command': command,
73 'arguments': prettyArgs.join(' '),
74 'app_id': appflowId,
75 'backend': 'pro',
76 ...lodash.extend({}, ...results),
77 },
78 };
79 const isLoggedIn = session.isLoggedIn();
80 if (isLoggedIn) {
81 const token = await session.getUserToken();
82 req.set('Authorization', `Bearer ${token}`);
83 }
84 debug('metric: %o', metric);
85 req.send({
86 'metrics': [metric],
87 'sent_at': now,
88 });
89 try {
90 await client.do(req);
91 }
92 catch (e) {
93 debug(`metric send error: ${e.stack ? e.stack : e}`);
94 }
95 })(),
96 ]);
97}
98exports.sendCommand = sendCommand;