UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

237 lines (236 loc) 11.7 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const commandline_1 = require("../../util/commandline"); const interaction_1 = require("../../util/interaction"); const util_1 = require("util"); const _ = require("lodash"); const date_parsing_helper_1 = require("./lib/date-parsing-helper"); const analytics_constants_1 = require("./lib/analytics-constants"); const debug = require("debug")("appcenter-cli:commands:analytics:audience"); let AudienceCommand = class AudienceCommand extends commandline_1.AppCommand { constructor(args) { super(args); interaction_1.supportsCsv(this.additionalSupportedOutputFormats); } run(client) { return __awaiter(this, void 0, void 0, function* () { const app = this.app; const appVersion = this.getAppVersion(); const appBuild = this.getAppBuild(); const startDate = date_parsing_helper_1.parseDate(this.startDate, new Date(new Date().setHours(0, 0, 0, 0)), `start date value ${this.startDate} is not a valid date string`); const endDate = date_parsing_helper_1.parseDate(this.endDate, new Date(), `end date value ${this.endDate} is not a valid date string`); if (!this.devices && !this.countries && !this.languages && !this.activeUsers) { // when no switches are specified, all the data should be shown this.devices = this.countries = this.languages = this.activeUsers = true; } const promises = []; const statistics = {}; if (this.devices) { promises.push(this.loadDevicesStatistics(statistics, client, app, startDate, endDate, appVersion)); } if (this.countries) { promises.push(this.loadCountriesStatistics(statistics, client, app, startDate, endDate, appVersion)); } if (this.languages) { promises.push(this.loadLanguagesStatistics(statistics, client, app, startDate, endDate, appVersion)); } if (this.activeUsers && appBuild) { promises.push(this.loadActiveUsersStatistics(statistics, client, app, startDate, endDate, appVersion, appBuild)); } else if (this.activeUsers) { interaction_1.out.text("Warning: Please provide app version to get active users statistics."); } yield interaction_1.out.progress("Loading statistics...", Promise.all(promises)); this.outputStatistics(statistics); return commandline_1.success(); }); } getAppVersion() { return !_.isNil(this.appVersion) ? [this.appVersion] : undefined; } getAppBuild() { return !_.isNil(this.appVersion) ? this.appVersion : undefined; } loadDevicesStatistics(statisticsObject, client, app, startDate, endDate, appVersion) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield client.analytics.modelCounts(startDate, app.ownerName, app.appName, { end: endDate, versions: appVersion, }); statisticsObject.devices = result.models.map((model) => ({ count: model.count, value: model.modelName, percentage: calculatePercentage(model.count, result.total), })); } catch (error) { debug(`Failed to get devices count statistics - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get devices count statistics"); } }); } loadCountriesStatistics(statisticsObject, client, app, startDate, endDate, appVersion) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield client.analytics.placeCounts(startDate, app.ownerName, app.appName, { end: endDate, versions: appVersion, }); statisticsObject.countries = result.places.map((place) => ({ count: place.count, value: place.code, percentage: calculatePercentage(place.count, result.total), })); } catch (error) { debug(`Failed to get countries statistics - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get countries statistics"); } }); } loadLanguagesStatistics(statisticsObject, client, app, startDate, endDate, appVersion) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield client.analytics.languageCounts(startDate, app.ownerName, app.appName, { end: endDate, versions: appVersion, }); statisticsObject.languages = result.languages.map((language) => ({ count: language.count, value: language.languageName, percentage: calculatePercentage(language.count, result.total), })); } catch (error) { debug(`Failed to get languages statistics - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get languages statistics"); } }); } loadActiveUsersStatistics(statisticsObject, client, app, startDate, endDate, appVersion, appBuild) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield client.analytics.deviceCounts(startDate, appBuild, app.ownerName, app.appName, { end: endDate, versions: appVersion, }); statisticsObject.activeUsers = result.daily.map((dailyData, index) => ({ date: new Date(dailyData.datetime), daily: dailyData.count, weekly: result.weekly[index].count, monthly: result.monthly[index].count, })); } catch (error) { debug(`Failed to get active users statistics - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to get active users statistics"); } }); } outputStatistics(statisticsObject) { interaction_1.out.reportObjectAsTitledTables((stats, numberFormatter, dateFormatter, percentageFormatter) => { const tableArray = []; if (stats.devices) { tableArray.push({ name: "Devices", content: [["", "Count", "Change"]].concat(stats.devices.map((device) => toArray(device, numberFormatter, percentageFormatter))), }); } if (stats.countries) { tableArray.push({ name: "Countries", content: [["", "Count", "Change"]].concat(stats.countries.map((country) => toArray(country, numberFormatter, percentageFormatter))), }); } if (stats.languages) { tableArray.push({ name: "Languages", content: [["", "Count", "Change"]].concat(stats.languages.map((language) => toArray(language, numberFormatter, percentageFormatter))), }); } if (stats.activeUsers) { tableArray.push({ name: "Active Users", content: [["Date", "Monthly", "Weekly", "Daily"]].concat(stats.activeUsers.map((activeUsersStatistics) => [ dateFormatter(activeUsersStatistics.date), numberFormatter(activeUsersStatistics.monthly), numberFormatter(activeUsersStatistics.weekly), numberFormatter(activeUsersStatistics.daily), ])), }); } return tableArray; }, statisticsObject); } }; __decorate([ commandline_1.help(analytics_constants_1.startDateHelpMessage), commandline_1.shortName("s"), commandline_1.longName("start"), commandline_1.hasArg ], AudienceCommand.prototype, "startDate", void 0); __decorate([ commandline_1.help(analytics_constants_1.endDateHelpMessage), commandline_1.shortName("e"), commandline_1.longName("end"), commandline_1.hasArg ], AudienceCommand.prototype, "endDate", void 0); __decorate([ commandline_1.help("Specify app version to show statistics for"), commandline_1.shortName("V"), commandline_1.longName("app-version"), commandline_1.hasArg ], AudienceCommand.prototype, "appVersion", void 0); __decorate([ commandline_1.help("Specify app build to show statistics for"), commandline_1.shortName("b"), commandline_1.longName("app-build"), commandline_1.hasArg ], AudienceCommand.prototype, "appBuild", void 0); __decorate([ commandline_1.help("Show devices statistics"), commandline_1.longName("devices") ], AudienceCommand.prototype, "devices", void 0); __decorate([ commandline_1.help("Show country statistics"), commandline_1.longName("countries") ], AudienceCommand.prototype, "countries", void 0); __decorate([ commandline_1.help("Show languages statistics"), commandline_1.longName("languages") ], AudienceCommand.prototype, "languages", void 0); __decorate([ commandline_1.help("Show active users statistics"), commandline_1.longName("active-users") ], AudienceCommand.prototype, "activeUsers", void 0); __decorate([ commandline_1.longName("output"), commandline_1.help("Format of output for this command: json, csv"), commandline_1.hasArg ], AudienceCommand.prototype, "format", void 0); AudienceCommand = __decorate([ commandline_1.help("Show audience statistics") ], AudienceCommand); exports.default = AudienceCommand; function toArray(stats, numberFormatter, percentageFormatter) { return [stats.value, numberFormatter(stats.count), percentageFormatter(stats.percentage)]; } function calculatePercentage(count, total) { return (count / total) * 100; }