Skip to content

Commit

Permalink
Create sub-structure for Metric rendering options
Browse files Browse the repository at this point in the history
This allows to create custom graphs easier by creating sub-class of
metric and overriding the `toGraphConfig` method. Before attributes have
been only selectively copied to the widget configuration.

```
class HiddenMetric extends Metric {
  public toGraphConfig(): any {
    const ret = super.toGraphConfig();
    ret.renderingProperties.visible = false;
  }
}
```

Before `visible` would not have been taken over since it was not part of
options.

Docs: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html#CloudWatch-Dashboard-Properties-Rendering-Object-Format
  • Loading branch information
workeitel committed Oct 13, 2019
1 parent f98f42f commit 4721a17
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
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 @@ -312,13 +312,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;
}
19 changes: 12 additions & 7 deletions packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ export interface MetricGraphConfig {
*/
readonly metricName: string;

/**
* Rendering properties override yAxis parameter of the widget object
*/
readonly renderingProperties: MetricRenderingProperties;
}

/**
* 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
*/
Expand All @@ -150,10 +160,5 @@ export interface MetricGraphConfig {
/**
* Aggregation function to use (can be either simple or a percentile)
*/
readonly statistic?: string;

/**
* The unit of the alarm
*/
readonly unit?: Unit;
}
readonly stat?: string;
}
11 changes: 6 additions & 5 deletions packages/@aws-cdk/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ export class Metric implements IMetric {
dimensions: this.dimensionsAsList(),
namespace: this.namespace,
metricName: this.metricName,
period: this.period.toSeconds(),
statistic: this.statistic,
unit: this.unit,
color: this.color,
label: this.label,
renderingProperties: {
period: this.period.toSeconds(),
stat: this.statistic,
color: this.color,
label: this.label,
},
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cloudwatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"awslint": {
"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"
]
},
"engines": {
Expand Down

0 comments on commit 4721a17

Please sign in to comment.