Skip to content

Commit 9e517fd

Browse files
author
Eugene Cheung
authored
feat(custom): support single value graph widgets (#182)
Implicitly requested via #181 (reply in thread) --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 6195488 commit 9e517fd

File tree

5 files changed

+166
-8
lines changed

5 files changed

+166
-8
lines changed

API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49930,6 +49930,7 @@ create a two sets of dashboards: standard set (interactive) and a copy (bitmap).
4993049930
| <code><a href="#cdk-monitoring-constructs.GraphWidgetType.STACKED_AREA">STACKED_AREA</a></code> | *No description.* |
4993149931
| <code><a href="#cdk-monitoring-constructs.GraphWidgetType.PIE">PIE</a></code> | *No description.* |
4993249932
| <code><a href="#cdk-monitoring-constructs.GraphWidgetType.BAR">BAR</a></code> | *No description.* |
49933+
| <code><a href="#cdk-monitoring-constructs.GraphWidgetType.SINGLE_VALUE">SINGLE_VALUE</a></code> | *No description.* |
4993349934

4993449935
---
4993549936

@@ -49953,6 +49954,11 @@ create a two sets of dashboards: standard set (interactive) and a copy (bitmap).
4995349954
---
4995449955

4995549956

49957+
##### `SINGLE_VALUE` <a name="SINGLE_VALUE" id="cdk-monitoring-constructs.GraphWidgetType.SINGLE_VALUE"></a>
49958+
49959+
---
49960+
49961+
4995649962
### HeaderLevel <a name="HeaderLevel" id="cdk-monitoring-constructs.HeaderLevel"></a>
4995749963

4995849964
#### Members <a name="Members" id="Members"></a>

lib/common/widget/types.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,56 @@ import {
22
GraphWidget,
33
GraphWidgetProps,
44
GraphWidgetView,
5+
IWidget,
6+
SingleValueWidget,
57
} from "aws-cdk-lib/aws-cloudwatch";
68

79
export enum GraphWidgetType {
810
LINE = "Line",
911
STACKED_AREA = "StackedArea",
1012
PIE = "Pie",
1113
BAR = "Bar",
14+
SINGLE_VALUE = "SingleValue",
1215
}
1316

1417
/**
1518
* Creates a graph widget of the desired type.
19+
*
1620
* @param type graph type (e.g. Pie or Bar)
1721
* @param props graph widget properties
1822
*/
1923
export function createGraphWidget(
2024
type: GraphWidgetType,
2125
props: GraphWidgetProps
22-
) {
26+
): IWidget {
2327
switch (type) {
24-
case "Line":
28+
case GraphWidgetType.LINE:
2529
return new GraphWidget(props);
26-
case "Bar":
30+
31+
case GraphWidgetType.BAR:
2732
return new GraphWidget({
2833
...props,
2934
view: GraphWidgetView.BAR,
3035
});
31-
case "Pie":
36+
37+
case GraphWidgetType.PIE:
3238
return new GraphWidget({
3339
...props,
3440
view: GraphWidgetView.PIE,
3541
});
36-
case "StackedArea":
42+
43+
case GraphWidgetType.STACKED_AREA:
3744
return new GraphWidget({
3845
...props,
3946
stacked: true,
4047
});
48+
49+
case GraphWidgetType.SINGLE_VALUE:
50+
return new SingleValueWidget({
51+
metrics: [...(props.left ?? []), ...(props.right ?? [])],
52+
});
53+
4154
default:
42-
throw new Error("Unsupported graph type: " + type);
55+
throw new Error(`Unsupported graph type: ${type}`);
4356
}
4457
}

lib/monitoring/custom/CustomMonitoring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export class CustomMonitoring extends Monitoring {
328328
annotatedGroups: CustomMetricGroupWithAnnotations[],
329329
summary: boolean
330330
) {
331-
const widgets: GraphWidget[] = [];
331+
const widgets: IWidget[] = [];
332332
const metricGroupWidgetWidth = recommendedWidgetWidth(
333333
annotatedGroups.length
334334
);

test/monitoring/custom/CustomMonitoring.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {
1010
import {
1111
AxisPosition,
1212
CustomMonitoring,
13+
GraphWidgetType,
1314
MetricFactory,
1415
MetricStatistic,
1516
} from "../../../lib";
17+
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
1618
import { TestMonitoringScope } from "../TestMonitoringScope";
1719

1820
const namespace = "DummyCustomNamespace";
@@ -24,7 +26,7 @@ test("snapshot test", () => {
2426

2527
let numAlarmsCreated = 0;
2628

27-
new CustomMonitoring(scope, {
29+
const monitoring = new CustomMonitoring(scope, {
2830
alarmFriendlyName: "DummyAlarmName",
2931
humanReadableName: "DummyName",
3032
description:
@@ -68,6 +70,7 @@ test("snapshot test", () => {
6870
},
6971
{
7072
title: "DummyGroup2",
73+
graphWidgetType: GraphWidgetType.BAR,
7174
metrics: [
7275
// regular metric
7376
new Metric({ metricName: "DummyMetric10", namespace, dimensionsMap }),
@@ -99,6 +102,7 @@ test("snapshot test", () => {
99102
},
100103
{
101104
title: "DummyGroup3",
105+
graphWidgetType: GraphWidgetType.PIE,
102106
metrics: [
103107
// regular metric
104108
new Metric({ metricName: "DummyMetric20", namespace, dimensionsMap }),
@@ -145,6 +149,14 @@ test("snapshot test", () => {
145149
{ label: "DummyAnnotation3", value: 20, fill: Shading.BELOW },
146150
],
147151
},
152+
{
153+
title: "DummyGroup4",
154+
graphWidgetType: GraphWidgetType.SINGLE_VALUE,
155+
metrics: [
156+
new Metric({ metricName: "DummyMetric40", namespace, dimensionsMap }),
157+
new Metric({ metricName: "DummyMetric41", namespace, dimensionsMap }),
158+
],
159+
},
148160
],
149161
useCreatedAlarms: {
150162
consume(alarms) {
@@ -154,6 +166,8 @@ test("snapshot test", () => {
154166
});
155167

156168
expect(numAlarmsCreated).toStrictEqual(6);
169+
170+
addMonitoringDashboardsToStack(stack, monitoring);
157171
expect(Template.fromStack(stack)).toMatchSnapshot();
158172
});
159173

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

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

0 commit comments

Comments
 (0)