Skip to content

Commit

Permalink
fix(overview-dashboard): upgrade to AWS SDK v3 (#1450)
Browse files Browse the repository at this point in the history
Also added a unit test for the sqs-dlq-stats-widget Lambda function.

----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
Co-authored-by: Momo Kornher <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2024
1 parent fd1bf92 commit b3073a5
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const project = new CdklabsConstructLibrary({
'@types/tough-cookie',
'@types/uuid',
cdkCli,
'@aws-sdk/client-cloudwatch',
'@aws-sdk/client-codeartifact',
'@aws-sdk/client-lambda',
'@aws-sdk/client-s3',
Expand Down
1 change: 1 addition & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/__tests__/__snapshots__/construct-hub.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
CloudWatchClient,
GetMetricDataCommand,
} from '@aws-sdk/client-cloudwatch';
import { mockClient } from 'aws-sdk-client-mock';

import {
Event,
handler,
} from '../../../overview-dashboard/sqs-dlq-stats-widget-function.lambda';

test('build stats page', async () => {
const cloudwatchMock = mockClient(CloudWatchClient);

const event: Event = {
description: 'foo',
key: 'bar',
widgetContext: {
params: {
queues: {
MyQueue: {
queueName: 'MyQueue',
name: 'bar',
reDriveFunctionArn:
'arn:aws:lambda:us-east-2:123456789012:function:my-function',
},
},
nonEmptyQueueMessage: 'MyQueue',
},
accountId: 'XXXXXXXXXXXX',
dashboardName: 'MyDashboard',
domain: 'us-east-1',
height: 6,
period: 300,
title: 'SomeTitle',
width: 6,
widgetId: 'abc',
linkCharts: false,
locale: 'en-US',
timezone: {
label: 'UTC',
offsetISO: '+0',
offsetInMinutes: 0,
},
isAutoPeriod: true,
theme: 'dark',
timeRange: {
mode: 'relative',
start: 0,
end: 0,
relativeStart: 0,
zoom: {
start: 0,
end: 0,
},
},
forms: { all: {} },
},
};

cloudwatchMock.on(GetMetricDataCommand).resolves({
MetricDataResults: [
{
Id: 'm0',
Values: [1, 2, 3],
},
],
});

const response = await handler(event);

expect(response).toEqual(`<h4>SomeTitle</h4>
<p>MyQueue</p><table>
<tr><th>Queue Name</th><th>Visible Messages</th><th>Action</th></tr>
<tr><td>bar</td><td>1</td><td><a class="btn " href="/sqs/v2/home#/queues/https%3A%2F%2Fsqs.undefined.amazonaws.com%2FXXXXXXXXXXXX%2FMyQueue">Goto Queue</a> <a class="btn btn-primary">ReDrive</a><cwdb-action action="call" display="widget" endpoint="arn:aws:lambda:us-east-2:123456789012:function:my-function" event="click"></<cwdb-action></td></tr>
</table>`);
});
2 changes: 1 addition & 1 deletion src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 34 additions & 27 deletions src/overview-dashboard/sqs-dlq-stats-widget-function.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ Param | Description
\`\`\`
`;

import { CloudWatch } from 'aws-sdk';
import {
CloudWatchClient,
GetMetricDataCommand,
} from '@aws-sdk/client-cloudwatch';

const DURATION = 5; // minutes

interface Event {
export interface Event {
readonly key: string;
readonly description: string;
readonly widgetContext: WidgetContext;
Expand Down Expand Up @@ -152,38 +156,41 @@ const getVisibleMessageCount = async (
): Promise<Record<string, number>> => {
// Keeping the time period to 5 minutes to show current state of the queue when re-drive happens
const queues = Object.values(queueConfigs);
const params: CloudWatch.GetMetricDataInput = {
StartTime: new Date(new Date().getTime() - DURATION * 60 * 1000), // 5 minutes ago
EndTime: new Date(), // now
ScanBy: 'TimestampDescending',
MetricDataQueries: queues.map((queue, index) => ({
Id: `m${index}`,
MetricStat: {
Period: 60,
Stat: 'Maximum',
Metric: {
Namespace: 'AWS/SQS',
MetricName: 'ApproximateNumberOfMessagesVisible',
Dimensions: [
{
Name: 'QueueName',
Value: queue.queueName,
},
],

const cloudwatchClient = new CloudWatchClient({});

const response = await cloudwatchClient.send(
new GetMetricDataCommand({
StartTime: new Date(new Date().getTime() - DURATION * 60 * 1000), // 5 minutes ago
EndTime: new Date(), // now
ScanBy: 'TimestampDescending',
MetricDataQueries: queues.map((queue, index) => ({
Id: `m${index}`,
MetricStat: {
Period: 60,
Stat: 'Maximum',
Metric: {
Namespace: 'AWS/SQS',
MetricName: 'ApproximateNumberOfMessagesVisible',
Dimensions: [
{
Name: 'QueueName',
Value: queue.queueName,
},
],
},
},
},
})),
};
const cloudwatch = new CloudWatch();
const response = await cloudwatch.getMetricData(params).promise();
const data = (response.MetricDataResults ?? []).reduce((acc, result) => {
})),
})
);

return (response.MetricDataResults ?? []).reduce((acc, result) => {
if (result.Id) {
const id = Number.parseInt(result.Id.replace('m', ''), 10);
return { ...acc, [queues[id].queueName]: result.Values?.[0] ?? 0 };
}
return acc;
}, {} as Record<string, number>);
return data;
};

function generateTable(
Expand Down
69 changes: 69 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3073a5

Please sign in to comment.