From 592596c73bfe0a9b6ef6bdff5d898ae9ef5e2788 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Mon, 4 Oct 2021 16:30:27 +0200 Subject: [PATCH] refactor(CLI): Modern logs for `metrics` command --- lib/plugins/aws/metrics.js | 15 +- test/unit/lib/plugins/aws/metrics.test.js | 194 ---------------------- 2 files changed, 14 insertions(+), 195 deletions(-) diff --git a/lib/plugins/aws/metrics.js b/lib/plugins/aws/metrics.js index cd68973ca3d..bf402b4b433 100644 --- a/lib/plugins/aws/metrics.js +++ b/lib/plugins/aws/metrics.js @@ -5,6 +5,7 @@ const chalk = require('chalk'); const _ = require('lodash'); const dayjs = require('dayjs'); const validate = require('./lib/validate'); +const { legacy, writeText, style } = require('@serverless/utils/log'); const LocalizedFormat = require('dayjs/plugin/localizedFormat'); @@ -100,16 +101,20 @@ class AwsMetrics { showMetrics(metrics) { let message = ''; + const modernMessageTokens = []; if (this.options.function) { message += `${chalk.yellow.underline(this.options.function)}\n`; + modernMessageTokens.push(this.options.function); } else { message += `${chalk.yellow.underline('Service wide metrics')}\n`; + modernMessageTokens.push('Service wide metrics'); } const formattedStartTime = dayjs(this.options.startTime).format('LLL'); const formattedEndTime = dayjs(this.options.endTime).format('LLL'); message += `${formattedStartTime} - ${formattedEndTime}\n\n`; + modernMessageTokens.push(`${formattedStartTime} - ${formattedEndTime}\n`); if (metrics && metrics.length > 0) { const getDatapointsByLabel = (Label) => @@ -129,11 +134,19 @@ class AwsMetrics { message += `${chalk.yellow('Throttles:', throttlesCount, '\n')}`; message += `${chalk.yellow('Errors:', errorsCount, '\n')}`; message += `${chalk.yellow('Duration (avg.):', `${Number(durationAverage.toFixed(2))}ms`)}`; + modernMessageTokens.push( + `${style.aside('invocations')}: ${invocationsCount}`, + `${style.aside('throttles')}: ${throttlesCount}`, + `${style.aside('errors')}: ${errorsCount}`, + `${style.aside('duration (avg.)')}: ${Number(durationAverage.toFixed(2))}ms` + ); } else { message += `${chalk.yellow('There are no metrics to show for these options')}`; + modernMessageTokens.push('There are no metrics to show for these options'); } - this.serverless.cli.consoleLog(message); + legacy.consoleLog(message); + writeText(modernMessageTokens); } } diff --git a/test/unit/lib/plugins/aws/metrics.test.js b/test/unit/lib/plugins/aws/metrics.test.js index 19d0beece82..43466946e27 100644 --- a/test/unit/lib/plugins/aws/metrics.test.js +++ b/test/unit/lib/plugins/aws/metrics.test.js @@ -6,7 +6,6 @@ const AwsProvider = require('../../../../../lib/plugins/aws/provider'); const AwsMetrics = require('../../../../../lib/plugins/aws/metrics'); const Serverless = require('../../../../../lib/Serverless'); const CLI = require('../../../../../lib/classes/CLI'); -const chalk = require('chalk'); const dayjs = require('dayjs'); const LocalizedFormat = require('dayjs/plugin/localizedFormat'); @@ -371,197 +370,4 @@ describe('AwsMetrics', () => { ).to.equal(true); }); }); - - describe('#showMetrics()', () => { - let consoleLogStub; - - beforeEach(() => { - awsMetrics.serverless.service.functions = { - function1: { - name: 'func1', - }, - function2: { - name: 'func2', - }, - }; - awsMetrics.options.startTime = '1970-01-01'; - awsMetrics.options.endTime = '1970-01-02'; - consoleLogStub = sinon.stub(serverless.cli, 'consoleLog').returns(); - }); - - afterEach(() => { - serverless.cli.consoleLog.restore(); - }); - - it('should display service wide metrics if no function option is specified', () => { - const metrics = [ - [ - { - ResponseMetadata: { - RequestId: '1f50045b-b569-11e6-86c6-eb54d1aaa755-func1', - }, - Label: 'Invocations', - Datapoints: [{ Sum: 12 }, { Sum: 8 }], - }, - { - ResponseMetadata: { - RequestId: '1f59059b-b569-11e6-aa18-c7bab68810d2-func1', - }, - Label: 'Throttles', - Datapoints: [{ Sum: 15 }, { Sum: 15 }], - }, - { - ResponseMetadata: { - RequestId: '1f50c7b1-b569-11e6-b1b6-ab86694b617b-func1', - }, - Label: 'Errors', - Datapoints: [{ Sum: 0 }], - }, - { - ResponseMetadata: { - RequestId: '1f63db14-b569-11e6-8501-d98a275ce164-func1', - }, - Label: 'Duration', - Datapoints: [{ Average: 1000 }], - }, - ], - [ - { - ResponseMetadata: { - RequestId: '1f50045b-b569-11e6-86c6-eb54d1aaa755-func2', - }, - Label: 'Invocations', - Datapoints: [{ Sum: 12 }, { Sum: 8 }], - }, - { - ResponseMetadata: { - RequestId: '1f59059b-b569-11e6-aa18-c7bab68810d2-func2', - }, - Label: 'Throttles', - Datapoints: [{ Sum: 15 }, { Sum: 15 }], - }, - { - ResponseMetadata: { - RequestId: '1f50c7b1-b569-11e6-b1b6-ab86694b617b-func2', - }, - Label: 'Errors', - Datapoints: [{ Sum: 0 }], - }, - { - ResponseMetadata: { - RequestId: '1f63db14-b569-11e6-8501-d98a275ce164-func2', - }, - Label: 'Duration', - Datapoints: [{ Average: 1000 }], - }, - ], - ]; - - let expectedMessage = ''; - expectedMessage += `${chalk.yellow.underline('Service wide metrics')}\n`; - expectedMessage += 'January 1, 1970 12:00 AM - January 2, 1970 12:00 AM\n\n'; - expectedMessage += `${chalk.yellow('Invocations: 40 \n')}`; - expectedMessage += `${chalk.yellow('Throttles: 60 \n')}`; - expectedMessage += `${chalk.yellow('Errors: 0 \n')}`; - expectedMessage += `${chalk.yellow('Duration (avg.): 1000ms')}`; - - awsMetrics.showMetrics(metrics); - - expect(consoleLogStub.calledOnce).to.equal(true); - expect(consoleLogStub.getCall(0).args[0]).to.equal(expectedMessage); - }); - - it('should display correct average of service wide average function duration', () => { - const metrics = [ - [ - { - Label: 'Duration', - Datapoints: [{ Average: 100 }, { Average: 200 }, { Average: 300 }], - }, - ], - [ - { - Label: 'Duration', - Datapoints: [{ Average: 400 }, { Average: 500 }], - }, - ], - ]; - - awsMetrics.showMetrics(metrics); - - expect(consoleLogStub.getCall(0).args[0]).to.include('Duration (avg.): 300ms'); - }); - - it('should display 0 as average function duration if no data by given period', () => { - const metrics = [[], []]; - - awsMetrics.showMetrics(metrics); - - expect(consoleLogStub.getCall(0).args[0]).to.include('Duration (avg.): 0ms'); - }); - - it('should display function metrics if function option is specified', () => { - awsMetrics.options.function = 'function1'; - - const metrics = [ - [ - { - ResponseMetadata: { - RequestId: '1f50045b-b569-11e6-86c6-eb54d1aaa755-func1', - }, - Label: 'Invocations', - Datapoints: [{ Sum: 12 }, { Sum: 8 }], - }, - { - ResponseMetadata: { - RequestId: '1f59059b-b569-11e6-aa18-c7bab68810d2-func1', - }, - Label: 'Throttles', - Datapoints: [{ Sum: 15 }, { Sum: 15 }], - }, - { - ResponseMetadata: { - RequestId: '1f50c7b1-b569-11e6-b1b6-ab86694b617b-func1', - }, - Label: 'Errors', - Datapoints: [{ Sum: 0 }], - }, - { - ResponseMetadata: { - RequestId: '1f63db14-b569-11e6-8501-d98a275ce164-func1', - }, - Label: 'Duration', - Datapoints: [{ Average: 1000 }], - }, - ], - ]; - - let expectedMessage = ''; - expectedMessage += `${chalk.yellow.underline(awsMetrics.options.function)}\n`; - expectedMessage += 'January 1, 1970 12:00 AM - January 2, 1970 12:00 AM\n\n'; - expectedMessage += `${chalk.yellow('Invocations: 20 \n')}`; - expectedMessage += `${chalk.yellow('Throttles: 30 \n')}`; - expectedMessage += `${chalk.yellow('Errors: 0 \n')}`; - expectedMessage += `${chalk.yellow('Duration (avg.): 1000ms')}`; - - awsMetrics.showMetrics(metrics); - - expect(consoleLogStub.calledOnce).to.equal(true); - expect(consoleLogStub.getCall(0).args[0]).to.equal(expectedMessage); - }); - - it('should resolve with an error message if no metrics are available', () => { - awsMetrics.options.function = 'function1'; - - let expectedMessage = ''; - expectedMessage += `${chalk.yellow.underline(awsMetrics.options.function)}\n`; - expectedMessage += 'January 1, 1970 12:00 AM - January 2, 1970 12:00 AM\n\n'; - expectedMessage += `${chalk.yellow('There are no metrics to show for these options')}`; - - awsMetrics.showMetrics(); - - expect(consoleLogStub.calledOnce).to.equal(true); - expect(consoleLogStub.getCall(0).args[0]).to.equal(expectedMessage); - }); - }); });