UNPKG

liveperson-functions-cli

Version:
123 lines 5.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MetricsController = void 0; const errorCodes_1 = require("../shared/errorCodes"); const faasFactory_service_1 = require("../service/faasFactory.service"); const constants_1 = require("../shared/constants"); const metrics_view_1 = require("../view/metrics.view"); class MetricsController { constructor({ metricsView = new metrics_view_1.MetricsView() } = {}) { this.metricsView = metricsView; } /** * Gets the logs of the passed function * @param {IMetricsConfig} - lambda function and flags * @returns {Promise<void>} * @memberof MetricsController */ async getMetrics({ lambdaFunction, inputFlags, }) { const { start, end = Date.now(), last, output = '' } = inputFlags; let startTimestamp; const endTimestamp = Number(end); if (!start && !last) { throw new Error('Please define either start and end timestamp or define the period for the last 5m, 1h, 7d,... since now'); } if (start && !last) { startTimestamp = Number(start); } else { startTimestamp = MetricsController.calculateStartTimestamp({ end, last, }); } if (Number.isNaN(startTimestamp) || Number.isNaN(endTimestamp)) { throw new Error('Given timestamps are not valid'); } if (startTimestamp > endTimestamp) { throw new Error('Start timestamp has to be before end timestamp.'); } if (endTimestamp - startTimestamp > constants_1.THIRTY_DAYS) { throw new Error('Time period cannot exceed 30 days.'); } if (endTimestamp - startTimestamp < constants_1.FIFTEEN_MINUTES) { throw new Error('Time period cannot be shorter than 15 minutes.'); } const bucketSizeInMS = MetricsController.getAppropriateBucketSize({ startTimestamp, endTimestamp, }); const faasService = await faasFactory_service_1.factory.get(); const uuid = await MetricsController.getLambdaUUID(lambdaFunction); const res = await faasService.getLambdaInvocationMetrics({ uuid, startTimestamp, endTimestamp, bucketSize: bucketSizeInMS, }); if (!output) { this.metricsView.printMetricsTable(res.invocationStatistics); } if (output.toLowerCase() === 'csv') { this.metricsView.printMetricsTableAsCSV(res.invocationStatistics); } if (output.toLowerCase() === 'json') { this.metricsView.printMetricsTableAsJSON(res.invocationStatistics); } } /** * Returns bucket size 5m, 1h, 1d in ms depending on the size of the time period (1h, 7d, >7d) * @param period contains start and end timestamp * @returns the bucket size in ms */ static getAppropriateBucketSize({ startTimestamp, endTimestamp, }) { const period = endTimestamp - startTimestamp; const ONE_HOUR = 1000 * 60 * 60; const SEVEN_DAYS = 1000 * 60 * 60 * 24 * 7; if (period <= ONE_HOUR) { return constants_1.BUCKET_SIZES['5m']; } if (period <= SEVEN_DAYS) { return constants_1.BUCKET_SIZES['1h']; } return constants_1.BUCKET_SIZES['1d']; } static calculateStartTimestamp({ end, last }) { return end - this.periodStringToTimestamp(last); } static async getLambdaUUID(lambdaFunction) { const faasService = await faasFactory_service_1.factory.get(); const [currentLambda] = (await faasService.getLambdasByNames([ lambdaFunction, ])); /* istanbul ignore next */ if (!currentLambda) { const prettyError = { message: `Function ${lambdaFunction} were not found on the platform. Please make sure the function with the name ${lambdaFunction} was pushed to the LivePerson Functions platform`, suggestions: [ 'Use "lpf push exampleFunction" to push and "lpf deploy exampleFunction" to deploy a function', ], ref: 'https://github.com/LivePersonInc/faas-cli#invoke', code: errorCodes_1.CLIErrorCodes.NoLambdasFound, }; throw prettyError; } return currentLambda.uuid; } static periodStringToTimestamp(periodString) { const regex = /(\d{1,2})([dhm])/; const [, amount, unit] = periodString.match(regex) || []; switch (unit) { case 'm': return Number(amount) * 60 * 1000; case 'h': return Number(amount) * 60 * 60 * 1000; case 'd': return Number(amount) * 24 * 60 * 60 * 1000; default: throw new Error('"Last" flag unit has to be m (minutes), h (hours) or d (days) e.g. 33m, 12h or 7d'); } } } exports.MetricsController = MetricsController; //# sourceMappingURL=metrics.controller.js.map