Skip to content

Commit

Permalink
feat(cloudwatch): GraphWidget supports period and statistic (aws#14679)
Browse files Browse the repository at this point in the history
Dashboard metric widgets support overridding/setting both period and stat on the
widget as a whole. This is often useful in combination with `MathExpression`
metrics.

Reference: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html#CloudWatch-Dashboard-Properties-Metric-Widget-Object


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
njlynch authored and hollanddd committed Aug 26, 2021
1 parent c1bf271 commit c62258a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ export interface GraphWidgetProps extends MetricWidgetProps {
*/
readonly liveData?: boolean;


/**
* Display this metric
*
Expand All @@ -223,6 +222,23 @@ export interface GraphWidgetProps extends MetricWidgetProps {
* @default false
*/
readonly setPeriodToTimeRange?: boolean;

/**
* 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;

/**
* The default statistic to be displayed for each metric.
* This default can be overridden within the definition of each individual metric
*
* @default - The statistic for each metric is used
*/
readonly statistic?: string;
}

/**
Expand Down Expand Up @@ -287,6 +303,8 @@ export class GraphWidget extends ConcreteWidget {
legend: this.props.legendPosition !== undefined ? { position: this.props.legendPosition } : undefined,
liveData: this.props.liveData,
setPeriodToTimeRange: this.props.setPeriodToTimeRange,
period: this.props.period?.toSeconds(),
stat: this.props.statistic,
},
}];
}
Expand Down
31 changes: 30 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Stack } from '@aws-cdk/core';
import { Duration, Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { Alarm, AlarmWidget, Color, GraphWidget, GraphWidgetView, LegendPosition, LogQueryWidget, Metric, Shading, SingleValueWidget, LogQueryVisualizationType } from '../lib';

Expand Down Expand Up @@ -688,4 +688,33 @@ export = {

test.done();
},

'GraphWidget supports stat and period'(test: Test) {
// GIVEN
const stack = new Stack();
const widget = new GraphWidget({
left: [new Metric({ namespace: 'CDK', metricName: 'Test' })],
statistic: 'Average',
period: Duration.days(2),
});

// THEN
test.deepEqual(stack.resolve(widget.toJson()), [{
type: 'metric',
width: 6,
height: 6,
properties: {
view: 'timeSeries',
region: { Ref: 'AWS::Region' },
metrics: [
['CDK', 'Test'],
],
yAxis: {},
stat: 'Average',
period: 172800,
},
}]);

test.done();
},
};

0 comments on commit c62258a

Please sign in to comment.