Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch): allow overriding of metric graph rendering #4571

Merged
merged 4 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions packages/@aws-cdk/aws-cloudwatch/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
42 changes: 41 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
* 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;
}
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
};