UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

134 lines (133 loc) 6.87 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()); }); }; var ShowLogFlowCommand_1; 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 ContinuousPollingHelper = require("../../util/continuous-polling/continuous-polling-helper"); const debug = require("debug")("appcenter-cli:commands:analytics:log-flow"); let ShowLogFlowCommand = ShowLogFlowCommand_1 = class ShowLogFlowCommand extends commandline_1.AppCommand { run(client) { return __awaiter(this, void 0, void 0, function* () { const app = this.app; this.validateParameters(); const logsCount = Number(this.logsCount) || 100; const streamingOutput = new interaction_1.StreamingArrayOutput(); streamingOutput.start(); let options = null; yield ContinuousPollingHelper.pollContinuously(() => __awaiter(this, void 0, void 0, function* () { try { debug("Loading logs"); // start time is not specified for the first request return yield client.analytics.genericLogFlow(app.ownerName, app.appName, options); } catch (error) { debug(`Failed to load the logs - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to load the logs"); } }), (response, responsesProcessed) => { // processing http response const result = response; if (result.logs.length) { // new logs were received options = { start: result.lastReceivedLogTimestamp }; // take no more than specified number of logs from the first request response const filteredLogs = responsesProcessed ? this.filterLogs(result.logs, this.installationId) : _.takeRight(this.filterLogs(result.logs, this.installationId), logsCount); for (const logEntry of filteredLogs) { this.showLogEntry(streamingOutput, logEntry); } } }, this.showContinuously, ShowLogFlowCommand_1.delayBetweenRequests, "Loading logs..."); streamingOutput.finish(); return commandline_1.success(); }); } validateParameters() { if (!_.isNil(this.logsCount)) { const parsedNumberOfLogs = Number(this.logsCount); if (!Number.isSafeInteger(parsedNumberOfLogs) || parsedNumberOfLogs < 1 || parsedNumberOfLogs > 100) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `${this.logsCount} is not a valid number of logs to show`); } } if (!_.isNil(this.installationId)) { if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/g.test(this.installationId)) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `${this.installationId} is not a valid installation identifier`); } } } filterLogs(logs, installId) { if (!_.isNil(installId)) { return logs.filter((logEntry) => logEntry.installId === installId); } else { return logs; } } showLogEntry(output, logEntry) { // setting common properties let logStringArray = [logEntry.timestamp.toString(), logEntry.installId, logEntry.type]; const jsonObject = { date: logEntry.timestamp, installId: logEntry.installId, logType: logEntry.type, }; // adding log id if (logEntry.type === "event") { // event name for event log logStringArray.push(logEntry.eventName); jsonObject.logId = logEntry.eventName; } else if (logEntry.sessionId != null) { // session id for logs with such property logStringArray.push(logEntry.sessionId); jsonObject.logId = logEntry.sessionId; } // adding properties if (logEntry.properties != null) { const logProperties = logEntry.properties; logStringArray = logStringArray.concat(_.toPairs(logProperties).map((pair) => pair.join(": "))); jsonObject.properties = logProperties; } output.text(() => logStringArray.join(", "), jsonObject); } }; ShowLogFlowCommand.delayBetweenRequests = 3000; __decorate([ commandline_1.help("Introduce the number of logs (max 100) that are being displayed, default number is 100"), commandline_1.shortName("l"), commandline_1.longName("num-logs"), commandline_1.hasArg ], ShowLogFlowCommand.prototype, "logsCount", void 0); __decorate([ commandline_1.help("Filter the logs by install ID"), commandline_1.shortName("i"), commandline_1.longName("install-id"), commandline_1.hasArg ], ShowLogFlowCommand.prototype, "installationId", void 0); __decorate([ commandline_1.help("Continue to return logs, press Ctrl+C to exit"), commandline_1.shortName("c"), commandline_1.longName("continue") ], ShowLogFlowCommand.prototype, "showContinuously", void 0); ShowLogFlowCommand = ShowLogFlowCommand_1 = __decorate([ commandline_1.help("Command to see the incoming logs in real time") ], ShowLogFlowCommand); exports.default = ShowLogFlowCommand;