Skip to content

Commit 96b9fa8

Browse files
author
Eugene Cheung
authored
feat(custom): add support for vertical annotations (#507)
Leveraging aws/aws-cdk#26819 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 22909ef commit 96b9fa8

File tree

7 files changed

+108
-11
lines changed

7 files changed

+108
-11
lines changed

API.md

Lines changed: 50 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dashboard/widget/StrictGraphWidget.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
HorizontalAnnotation,
55
IMetric,
66
LegendPosition,
7+
VerticalAnnotation,
78
YAxisProps,
89
} from "aws-cdk-lib/aws-cloudwatch";
910

@@ -14,6 +15,7 @@ export interface SingleAxisGraphWidgetProps {
1415
readonly leftMetrics: IMetric[];
1516
readonly leftAxis: YAxisProps;
1617
readonly leftAnnotations?: HorizontalAnnotation[];
18+
readonly verticalAnnotations?: VerticalAnnotation[];
1719
}
1820

1921
/**
@@ -44,6 +46,7 @@ export class SingleAxisGraphWidget extends GraphWidget {
4446
left: props.leftMetrics,
4547
leftYAxis: props.leftAxis,
4648
leftAnnotations: props.leftAnnotations,
49+
verticalAnnotations: props.verticalAnnotations,
4750
legendPosition,
4851
};
4952
}
@@ -59,6 +62,7 @@ export interface DoubleAxisGraphWidgetProps {
5962
readonly rightMetrics: IMetric[];
6063
readonly rightAxis: YAxisProps;
6164
readonly rightAnnotations?: HorizontalAnnotation[];
65+
readonly verticalAnnotations?: VerticalAnnotation[];
6266
}
6367

6468
/**
@@ -95,6 +99,7 @@ export class DoubleAxisGraphWidget extends GraphWidget {
9599
right: props.rightMetrics,
96100
rightYAxis: props.rightAxis,
97101
rightAnnotations: props.rightAnnotations,
102+
verticalAnnotations: props.verticalAnnotations,
98103
};
99104
}
100105
}

lib/monitoring/custom/CustomMonitoring.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
LegendPosition,
1010
Row,
1111
TextWidget,
12+
VerticalAnnotation,
1213
YAxisProps,
1314
} from "aws-cdk-lib/aws-cloudwatch";
1415

@@ -188,15 +189,19 @@ export interface CustomMetricGroup {
188189
*/
189190
readonly metrics: CustomMetric[];
190191
/**
191-
* optional custom horizontal annotations which will be displayed over the metrics on the left axis
192-
* (if there are any alarms, any existing annotations will be merged together)
192+
* Optional custom horizontal annotations which will be displayed over the metrics on the left axis
193+
* (if there are any alarms, any existing annotations will be merged together).
193194
*/
194195
readonly horizontalAnnotations?: HorizontalAnnotation[];
195196
/**
196-
* optional custom horizontal annotations which will be displayed over the metrics on the right axis
197-
* (if there are any alarms, any existing annotations will be merged together)
197+
* Optional custom horizontal annotations which will be displayed over the metrics on the right axis
198+
* (if there are any alarms, any existing annotations will be merged together).
198199
*/
199200
readonly horizontalRightAnnotations?: HorizontalAnnotation[];
201+
/**
202+
* Optional custom vertical annotations which will be displayed over the metrics.
203+
*/
204+
readonly verticalAnnotations?: VerticalAnnotation[];
200205
}
201206

202207
export interface CustomMonitoringProps extends BaseMonitoringProps {
@@ -225,6 +230,7 @@ export interface CustomMetricGroupWithAnnotations {
225230
readonly metricGroup: CustomMetricGroup;
226231
readonly annotations: HorizontalAnnotation[];
227232
readonly rightAnnotations: HorizontalAnnotation[];
233+
readonly verticalAnnotations: VerticalAnnotation[];
228234
readonly titleAddons: string[];
229235
readonly height?: number;
230236
}
@@ -270,6 +276,7 @@ export class CustomMonitoring extends Monitoring {
270276
metricGroup,
271277
annotations: [],
272278
rightAnnotations: [],
279+
verticalAnnotations: [],
273280
titleAddons: [],
274281
};
275282

@@ -283,6 +290,11 @@ export class CustomMonitoring extends Monitoring {
283290
...metricGroup.horizontalRightAnnotations
284291
);
285292
}
293+
if (metricGroup.verticalAnnotations) {
294+
metricGroupWithAnnotation.verticalAnnotations.push(
295+
...metricGroup.verticalAnnotations
296+
);
297+
}
286298

287299
metricGroup.metrics.forEach((metric) => {
288300
if (this.hasAlarm(metric) && this.hasAnomalyDetection(metric)) {
@@ -412,6 +424,7 @@ export class CustomMonitoring extends Monitoring {
412424
rightAnnotations: annotatedGroup.rightAnnotations,
413425
leftYAxis: annotatedGroup.metricGroup.graphWidgetAxis,
414426
rightYAxis: annotatedGroup.metricGroup.graphWidgetRightAxis,
427+
verticalAnnotations: annotatedGroup.verticalAnnotations,
415428
legendPosition: annotatedGroup.metricGroup.graphWidgetLegend,
416429
setPeriodToTimeRange:
417430
annotatedGroup.metricGroup.graphWidgetSetPeriodToTimeRange,

test/dashboard/widget/StrictGraphWidget.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
HorizontalAnnotation,
33
Metric,
4+
VerticalAnnotation,
5+
VerticalShading,
46
YAxisProps,
57
} from "aws-cdk-lib/aws-cloudwatch";
68
import { DoubleAxisGraphWidget, SingleAxisGraphWidget } from "../../../lib";
@@ -11,6 +13,12 @@ const DummyAxis1: YAxisProps = { min: 0, max: 1 };
1113
const DummyAxis2: YAxisProps = { min: 100, max: 200 };
1214
const DummyAnnotation1: HorizontalAnnotation = { value: 42 };
1315
const DummyAnnotation2: HorizontalAnnotation = { value: 66 };
16+
const DummyVerticalAnnotation: VerticalAnnotation = {
17+
date: "2021-07-29T02:31:09.890Z",
18+
color: "123456",
19+
fill: VerticalShading.BEFORE,
20+
label: "Custom vertical annotation",
21+
};
1422

1523
test("single axis - single metric: snapshot test", () => {
1624
const widget = new SingleAxisGraphWidget({
@@ -33,6 +41,7 @@ test("single axis - two metrics: snapshot test", () => {
3341
leftMetrics: [DummyMetric1, DummyMetric2],
3442
leftAnnotations: [DummyAnnotation1],
3543
leftAxis: DummyAxis1,
44+
verticalAnnotations: [DummyVerticalAnnotation],
3645
});
3746

3847
expect(removeRegionToken(widget.toJson())).toMatchSnapshot();
@@ -49,6 +58,7 @@ test("double axis - two metrics: snapshot test", () => {
4958
rightMetrics: [DummyMetric1],
5059
rightAxis: DummyAxis2,
5160
rightAnnotations: [DummyAnnotation2],
61+
verticalAnnotations: [DummyVerticalAnnotation],
5262
});
5363

5464
expect(removeRegionToken(widget.toJson())).toMatchSnapshot();

test/dashboard/widget/__snapshots__/StrictGraphWidget.test.ts.snap

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/monitoring/custom/CustomMonitoring.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
MathExpression,
77
Metric,
88
Shading,
9+
VerticalShading,
910
} from "aws-cdk-lib/aws-cloudwatch";
1011

1112
import {
@@ -153,6 +154,14 @@ test("snapshot test", () => {
153154
horizontalRightAnnotations: [
154155
{ label: "DummyAnnotation3", value: 20, fill: Shading.BELOW },
155156
],
157+
verticalAnnotations: [
158+
{
159+
date: "2021-07-29T02:31:09.890Z",
160+
color: "667788",
161+
fill: VerticalShading.AFTER,
162+
label: "this is a vertical annotation",
163+
},
164+
],
156165
graphWidgetLegend: LegendPosition.RIGHT,
157166
},
158167
{

test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)