diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/graph.ts b/packages/@aws-cdk/aws-cloudwatch/lib/graph.ts index 145f662feb3cd..ff5f7509d4882 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/graph.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/graph.ts @@ -320,13 +320,7 @@ function metricJson(metric: IMetric, yAxis: string): any[] { } // Options - ret.push({ - yAxis, - label: config.label, - color: config.color, - period: config.period, - stat: config.statistic, - }); + ret.push({ yAxis, ...config.renderingProperties }); return ret; } diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts index 5a3d90895ecfe..e09c6a5abbe76 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts @@ -132,28 +132,68 @@ export interface MetricGraphConfig { */ readonly metricName: string; + /** + * Rendering properties override yAxis parameter of the widget object + */ + readonly renderingProperties: MetricRenderingProperties; + /** * How many seconds to aggregate over + * + * @deprecated Use `period` in `renderingProperties` */ readonly period: number; /** * Label for the metric + * + * @deprecated Use `label` in `renderingProperties` */ readonly label?: string; /** * Color for the graph line + * + * @deprecated Use `color` in `renderingProperties` */ readonly color?: string; /** * Aggregation function to use (can be either simple or a percentile) + * + * @deprecated Use `stat` in `renderingProperties` */ readonly statistic?: string; /** * The unit of the alarm + * + * @deprecated not used in dashboard widgets */ readonly unit?: Unit; -} \ No newline at end of file +} + +/** + * Custom rendering properties that override the default rendering properties specified in the yAxis parameter of the widget object. + */ +export interface MetricRenderingProperties { + /** + * How many seconds to aggregate over + */ + readonly period: number; + + /** + * Label for the metric + */ + readonly label?: string; + + /** + * Color for the graph line + */ + readonly color?: string; + + /** + * Aggregation function to use (can be either simple or a percentile) + */ + readonly stat?: string; +} diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index a5dbfaeb371e3..f07ee49e0316c 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -195,6 +195,13 @@ export class Metric implements IMetric { dimensions: this.dimensionsAsList(), namespace: this.namespace, metricName: this.metricName, + renderingProperties: { + period: this.period.toSeconds(), + stat: this.statistic, + color: this.color, + label: this.label, + }, + // deprecated properties for backwards compatibility period: this.period.toSeconds(), statistic: this.statistic, unit: this.unit, diff --git a/packages/@aws-cdk/aws-cloudwatch/package.json b/packages/@aws-cdk/aws-cloudwatch/package.json index 076d522b1af16..3d8a224524e9e 100644 --- a/packages/@aws-cdk/aws-cloudwatch/package.json +++ b/packages/@aws-cdk/aws-cloudwatch/package.json @@ -82,6 +82,7 @@ "exclude": [ "duration-prop-type:@aws-cdk/aws-cloudwatch.MetricAlarmConfig.period", "duration-prop-type:@aws-cdk/aws-cloudwatch.MetricGraphConfig.period", + "duration-prop-type:@aws-cdk/aws-cloudwatch.MetricRenderingProperties.period", "docs-public-apis:@aws-cdk/aws-cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD", "docs-public-apis:@aws-cdk/aws-cloudwatch.Unit.NONE", "docs-public-apis:@aws-cdk/aws-cloudwatch.Unit.COUNT_PER_SECOND", @@ -118,8 +119,12 @@ "props-default-doc:@aws-cdk/aws-cloudwatch.MetricGraphConfig.color", "props-default-doc:@aws-cdk/aws-cloudwatch.MetricGraphConfig.dimensions", "props-default-doc:@aws-cdk/aws-cloudwatch.MetricGraphConfig.label", + "props-default-doc:@aws-cdk/aws-cloudwatch.MetricGraphConfig.period", "props-default-doc:@aws-cdk/aws-cloudwatch.MetricGraphConfig.statistic", "props-default-doc:@aws-cdk/aws-cloudwatch.MetricGraphConfig.unit", + "props-default-doc:@aws-cdk/aws-cloudwatch.MetricRenderingProperties.color", + "props-default-doc:@aws-cdk/aws-cloudwatch.MetricRenderingProperties.label", + "props-default-doc:@aws-cdk/aws-cloudwatch.MetricRenderingProperties.stat", "props-default-doc:@aws-cdk/aws-cloudwatch.MetricWidgetProps.title", "docs-public-apis:@aws-cdk/aws-cloudwatch.Unit.GIGABITS_PER_SECOND", "docs-public-apis:@aws-cdk/aws-cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD", diff --git a/packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts b/packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts index c9d57f1a4ca35..df765cdf86433 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts @@ -317,4 +317,30 @@ export = { test.done(); }, + + 'allows overriding custom values of dashboard widgets'(test: Test) { + class HiddenMetric extends Metric { + public toGraphConfig(): any { + const ret = super.toGraphConfig(); + // @ts-ignore + ret.renderingProperties.visible = false; + return ret; + } + } + + const stack = new Stack(); + const widget = new GraphWidget({ + left: [ + new HiddenMetric({ namespace: 'CDK', metricName: 'Test' }) + ] + }); + + // test.ok(widget.toJson()[0].properties.metrics[0].visible === false); + test.deepEqual( + stack.resolve(widget.toJson())[0].properties.metrics[0], + ["CDK", "Test", { yAxis: 'left', period: 300, stat: 'Average', visible: false }] + ); + + test.done(); + }, };