diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets.json new file mode 100644 index 0000000000000..d263cc8b91a28 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets.json @@ -0,0 +1,19 @@ +{ + "version": "32.0.0", + "files": { + "d4dda261a18b29095c14be79773ff2c9cee39ed1fa347c6b46432c448eb55bfe": { + "source": { + "path": "aws-cdk-cloudwatch-singlevaluewidget-period-integ.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "d4dda261a18b29095c14be79773ff2c9cee39ed1fa347c6b46432c448eb55bfe.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/aws-cdk-cloudwatch-singlevaluewidget-period-integ.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/aws-cdk-cloudwatch-singlevaluewidget-period-integ.template.json new file mode 100644 index 0000000000000..97951783eced9 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/aws-cdk-cloudwatch-singlevaluewidget-period-integ.template.json @@ -0,0 +1,55 @@ +{ + "Resources": { + "Dashboard9E4231ED": { + "Type": "AWS::CloudWatch::Dashboard", + "Properties": { + "DashboardBody": { + "Fn::Join": [ + "", + [ + "{\"widgets\":[{\"type\":\"metric\",\"width\":6,\"height\":3,\"x\":0,\"y\":0,\"properties\":{\"view\":\"singleValue\",\"region\":\"", + { + "Ref": "AWS::Region" + }, + "\",\"metrics\":[[\"CDK/Test\",\"Metric\"]],\"period\":600}}]}" + ] + ] + } + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/cdk.out new file mode 100644 index 0000000000000..f0b901e7c06e5 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"32.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/integ.json new file mode 100644 index 0000000000000..3ebf220edecc8 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "32.0.0", + "testCases": { + "singlevaluewidget-with-period/DefaultTest": { + "stacks": [ + "aws-cdk-cloudwatch-singlevaluewidget-period-integ" + ], + "assertionStack": "singlevaluewidget-with-period/DefaultTest/DeployAssert", + "assertionStackName": "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/manifest.json new file mode 100644 index 0000000000000..ffd6feb03a3dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/manifest.json @@ -0,0 +1,111 @@ +{ + "version": "32.0.0", + "artifacts": { + "aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-cdk-cloudwatch-singlevaluewidget-period-integ": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-cdk-cloudwatch-singlevaluewidget-period-integ.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/d4dda261a18b29095c14be79773ff2c9cee39ed1fa347c6b46432c448eb55bfe.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "aws-cdk-cloudwatch-singlevaluewidget-period-integ.assets" + ], + "metadata": { + "/aws-cdk-cloudwatch-singlevaluewidget-period-integ/Dashboard/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "Dashboard9E4231ED" + } + ], + "/aws-cdk-cloudwatch-singlevaluewidget-period-integ/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-cdk-cloudwatch-singlevaluewidget-period-integ/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-cdk-cloudwatch-singlevaluewidget-period-integ" + }, + "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.template.json", + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets" + ], + "metadata": { + "/singlevaluewidget-with-period/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/singlevaluewidget-with-period/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "singlevaluewidget-with-period/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets.json new file mode 100644 index 0000000000000..77843d5347a68 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.assets.json @@ -0,0 +1,19 @@ +{ + "version": "32.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/singlevaluewidgetwithperiodDefaultTestDeployAssert48DEBCE0.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/tree.json new file mode 100644 index 0000000000000..bc0600913c3b3 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.js.snapshot/tree.json @@ -0,0 +1,136 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-cdk-cloudwatch-singlevaluewidget-period-integ": { + "id": "aws-cdk-cloudwatch-singlevaluewidget-period-integ", + "path": "aws-cdk-cloudwatch-singlevaluewidget-period-integ", + "children": { + "Dashboard": { + "id": "Dashboard", + "path": "aws-cdk-cloudwatch-singlevaluewidget-period-integ/Dashboard", + "children": { + "Resource": { + "id": "Resource", + "path": "aws-cdk-cloudwatch-singlevaluewidget-period-integ/Dashboard/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudWatch::Dashboard", + "aws:cdk:cloudformation:props": { + "dashboardBody": { + "Fn::Join": [ + "", + [ + "{\"widgets\":[{\"type\":\"metric\",\"width\":6,\"height\":3,\"x\":0,\"y\":0,\"properties\":{\"view\":\"singleValue\",\"region\":\"", + { + "Ref": "AWS::Region" + }, + "\",\"metrics\":[[\"CDK/Test\",\"Metric\"]],\"period\":600}}]}" + ] + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudwatch.CfnDashboard", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudwatch.Dashboard", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-cdk-cloudwatch-singlevaluewidget-period-integ/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-cdk-cloudwatch-singlevaluewidget-period-integ/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "singlevaluewidget-with-period": { + "id": "singlevaluewidget-with-period", + "path": "singlevaluewidget-with-period", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "singlevaluewidget-with-period/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "singlevaluewidget-with-period/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.55" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "singlevaluewidget-with-period/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "singlevaluewidget-with-period/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "singlevaluewidget-with-period/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.55" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.ts new file mode 100644 index 0000000000000..61c2b8808a889 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.period-singlevaluewidget-and-dashboard.ts @@ -0,0 +1,28 @@ +import { App, Duration, Stack, StackProps } from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import { Dashboard, SingleValueWidget, Metric } from 'aws-cdk-lib/aws-cloudwatch'; + +class TestStack extends Stack { + constructor(scope: App, id: string, props?: StackProps) { + super(scope, id, props); + + const dashboard = new Dashboard(this, 'Dashboard'); + + const testMetric = new Metric({ + namespace: 'CDK/Test', + metricName: 'Metric', + }); + + const widget = new SingleValueWidget({ + metrics: [testMetric], + period: Duration.minutes(10), + }); + + dashboard.addWidgets(widget); + } +} +const app = new App(); +const testCase = new TestStack(app, 'aws-cdk-cloudwatch-singlevaluewidget-period-integ'); +new IntegTest(app, 'singlevaluewidget-with-period', { + testCases: [testCase], +}); diff --git a/packages/aws-cdk-lib/aws-cloudwatch/README.md b/packages/aws-cdk-lib/aws-cloudwatch/README.md index c01213a90d6f1..bf28003b05a7d 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch/README.md @@ -544,6 +544,18 @@ dashboard.addWidgets(new cloudwatch.SingleValueWidget({ })); ``` +Period allows you to set the default period for the widget: + +```ts +declare const dashboard: cloudwatch.Dashboard; + +dashboard.addWidgets(new cloudwatch.SingleValueWidget({ + metrics: [ /* ... */ ], + + period: Duration.minutes(15), +})); +``` + ### Text widget A text widget shows an arbitrary piece of MarkDown. Use this to add explanations diff --git a/packages/aws-cdk-lib/aws-cloudwatch/lib/graph.ts b/packages/aws-cdk-lib/aws-cloudwatch/lib/graph.ts index 591655941b2d2..c7b5c7d497a34 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/lib/graph.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch/lib/graph.ts @@ -451,6 +451,15 @@ export interface SingleValueWidgetProps extends MetricWidgetProps { */ readonly metrics: IMetric[]; + /** + * The default period for all metrics in this widget. + * The period is the length of time represented by one data point on the graph. + * This default can be overridden within each metric definition. + * + * @default cdk.Duration.seconds(300) + */ + readonly period?: cdk.Duration; + /** * Whether to show the value from the entire time range. * @@ -505,6 +514,7 @@ export class SingleValueWidget extends ConcreteWidget { metrics: allMetricsGraphJson(this.props.metrics, []), setPeriodToTimeRange: this.props.setPeriodToTimeRange, singleValueFullPrecision: this.props.fullPrecision, + period: this.props.period?.toSeconds(), }, }]; } diff --git a/packages/aws-cdk-lib/aws-cloudwatch/test/graphs.test.ts b/packages/aws-cdk-lib/aws-cloudwatch/test/graphs.test.ts index 3d8d32c668f39..fc440f1c416b9 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/test/graphs.test.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch/test/graphs.test.ts @@ -677,6 +677,34 @@ describe('Graphs', () => { }); + test('add period to singleValueWidget', () => { + // GIVEN + const stack = new Stack(); + const metric = new Metric({ namespace: 'CDK', metricName: 'Test' }); + + // WHEN + const widget = new SingleValueWidget({ + metrics: [metric], + period: Duration.days(2), + }); + + // THEN + expect(stack.resolve(widget.toJson())).toEqual([{ + type: 'metric', + width: 6, + height: 3, + properties: { + view: 'singleValue', + region: { Ref: 'AWS::Region' }, + metrics: [ + ['CDK', 'Test'], + ], + period: 172800, + }, + }]); + + }); + test('allows overriding custom values of dashboard widgets', () => { class HiddenMetric extends Metric { public toMetricConfig() {