Skip to content

Commit d4717cf

Browse files
authored
feat(cloudwatch): added defaultInterval prop to cw-dashboard (#24707)
This PR adds defaultInterval to cloudwatch dashboard, which allows interval duration in relative time eg. 7 days. ```ts const dashboard = cw.Dashboard(stack, 'Dash', { defaultInterval: cdk.Duration.days(7), }); ``` Here, the dashboard would show the metrics for the last 7 days. > [CONTRIBUTING GUIDE]: https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md > [DESIGN GUIDELINES]: https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md Closes #<issue number here>. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 767cf93 commit d4717cf

11 files changed

+71
-17
lines changed

packages/@aws-cdk/aws-cloudwatch/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,18 @@ new cloudwatch.Row(widgetA, widgetB);
697697

698698
You can add a widget after object instantiation with the method
699699
`addWidget()`.
700+
701+
### Interval duration for dashboard
702+
703+
Interval duration for metrics in dashboard. You can specify `defaultInterval` with
704+
the relative time(eg. 7 days) as `cdk.Duration.days(7)`.
705+
706+
```ts
707+
import * as cw from '@aws-cdk/aws-cloudwatch';
708+
709+
const dashboard = new cw.Dashboard(stack, 'Dash', {
710+
defaultInterval: cdk.Duration.days(7),
711+
});
712+
```
713+
714+
Here, the dashboard would show the metrics for the last 7 days.

packages/@aws-cdk/aws-cloudwatch/lib/dashboard.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Lazy, Resource, Stack, Token, Annotations } from '@aws-cdk/core';
1+
import { Lazy, Resource, Stack, Token, Annotations, Duration } from '@aws-cdk/core';
22
import { Construct } from 'constructs';
33
import { CfnDashboard } from './cloudwatch.generated';
44
import { Column, Row } from './layout';
@@ -31,6 +31,14 @@ export interface DashboardProps {
3131
*/
3232
readonly dashboardName?: string;
3333

34+
/**
35+
* Interval duration for metrics.
36+
* You can specify defaultInterval with the relative time(eg. cdk.Duration.days(7)).
37+
*
38+
* @default When the dashboard loads, the defaultInterval time will be the default time range.
39+
*/
40+
readonly defaultInterval?: Duration
41+
3442
/**
3543
* The start of the time range to use for each widget on the dashboard.
3644
* You can specify start without specifying end to specify a relative time range that ends with the current time.
@@ -107,15 +115,19 @@ export class Dashboard extends Resource {
107115
}
108116
}
109117

118+
if (props.start !== undefined && props.defaultInterval !== undefined) {
119+
throw ('both properties defaultInterval and start cannot be set at once');
120+
}
121+
110122
const dashboard = new CfnDashboard(this, 'Resource', {
111123
dashboardName: this.physicalName,
112124
dashboardBody: Lazy.string({
113125
produce: () => {
114126
const column = new Column(...this.rows);
115127
column.position(0, 0);
116128
return Stack.of(this).toJsonString({
117-
start: props.start,
118-
end: props.end,
129+
start: props.defaultInterval !== undefined ? `-${props.defaultInterval?.toIsoString()}` : props.start,
130+
end: props.defaultInterval !== undefined ? undefined : props.end,
119131
periodOverride: props.periodOverride,
120132
widgets: column.toJson(),
121133
});

packages/@aws-cdk/aws-cloudwatch/test/dashboard.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Template, Annotations, Match } from '@aws-cdk/assertions';
2-
import { App, Stack } from '@aws-cdk/core';
2+
import { App, Duration, Stack } from '@aws-cdk/core';
33
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression, TextWidgetBackground } from '../lib';
44

55
describe('Dashboard', () => {
@@ -131,6 +131,31 @@ describe('Dashboard', () => {
131131

132132
});
133133

134+
test('defaultInterval test', () => {
135+
// GIVEN
136+
const stack = new Stack();
137+
// WHEN
138+
const dashboard = new Dashboard(stack, 'Dash', {
139+
defaultInterval: Duration.days(7),
140+
});
141+
dashboard.addWidgets(
142+
new GraphWidget({ width: 1, height: 1 }), // GraphWidget has internal reference to current region
143+
);
144+
145+
// THEN
146+
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Dashboard', {
147+
DashboardBody: {
148+
'Fn::Join': ['', [
149+
'{"start":"-P7D",\
150+
"widgets":[{"type":"metric","width":1,"height":1,"x":0,"y":0,"properties":{"view":"timeSeries","region":"',
151+
{ Ref: 'AWS::Region' },
152+
'","yAxis":{}}}]}',
153+
]],
154+
},
155+
});
156+
157+
});
158+
134159
test('DashboardName is set when provided', () => {
135160
// GIVEN
136161
const app = new App();

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/DashboardIntegrationTestDefaultTestDeployAssert5BE38902.assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "30.0.0",
2+
"version": "31.0.0",
33
"files": {
44
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
55
"source": {

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/DashboardIntegrationTestStack.assets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "30.0.0",
2+
"version": "31.0.0",
33
"files": {
4-
"53eb5ec97b9df3953bc84bdc2aee87ace7b502c665b7e5b9f7b7d14dd46cea69": {
4+
"1a70f8470c838c02020b9010528363b17eebd55d55c1a53fb3e0f6760a606c98": {
55
"source": {
66
"path": "DashboardIntegrationTestStack.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "53eb5ec97b9df3953bc84bdc2aee87ace7b502c665b7e5b9f7b7d14dd46cea69.json",
12+
"objectKey": "1a70f8470c838c02020b9010528363b17eebd55d55c1a53fb3e0f6760a606c98.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/DashboardIntegrationTestStack.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"DashCCD7F836": {
44
"Type": "AWS::CloudWatch::Dashboard",
55
"Properties": {
6-
"DashboardBody": "{\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
6+
"DashboardBody": "{\"start\":\"-P7D\",\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
77
}
88
}
99
},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"30.0.0"}
1+
{"version":"31.0.0"}

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "30.0.0",
2+
"version": "31.0.0",
33
"testCases": {
44
"DashboardIntegrationTest/DefaultTest": {
55
"stacks": [

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "30.0.0",
2+
"version": "31.0.0",
33
"artifacts": {
44
"DashboardIntegrationTestStack.assets": {
55
"type": "cdk:asset-manifest",
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/53eb5ec97b9df3953bc84bdc2aee87ace7b502c665b7e5b9f7b7d14dd46cea69.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/1a70f8470c838c02020b9010528363b17eebd55d55c1a53fb3e0f6760a606c98.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/tree.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"attributes": {
1919
"aws:cdk:cloudformation:type": "AWS::CloudWatch::Dashboard",
2020
"aws:cdk:cloudformation:props": {
21-
"dashboardBody": "{\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
21+
"dashboardBody": "{\"start\":\"-P7D\",\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
2222
}
2323
},
2424
"constructInfo": {
@@ -75,7 +75,7 @@
7575
"path": "DashboardIntegrationTest/DefaultTest/Default",
7676
"constructInfo": {
7777
"fqn": "constructs.Construct",
78-
"version": "10.1.252"
78+
"version": "10.1.270"
7979
}
8080
},
8181
"DeployAssert": {
@@ -121,7 +121,7 @@
121121
"path": "Tree",
122122
"constructInfo": {
123123
"fqn": "constructs.Construct",
124-
"version": "10.1.252"
124+
"version": "10.1.270"
125125
}
126126
}
127127
},

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const app = new cdk.App();
77

88
const stack = new cdk.Stack(app, 'DashboardIntegrationTestStack');
99

10-
const dashboard = new cloudwatch.Dashboard(stack, 'Dash');
10+
const dashboard = new cloudwatch.Dashboard(stack, 'Dash', {
11+
defaultInterval: cdk.Duration.days(7),
12+
});
1113

1214
dashboard.addWidgets(new cloudwatch.TextWidget({
1315
markdown: 'I don\'t have a background',

0 commit comments

Comments
 (0)