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): Dashboard TextWidget background support #23169

Merged
merged 1 commit into from
Nov 30, 2022
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
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ dashboard.addWidgets(new cloudwatch.TextWidget({
}));
```

Optionally set the TextWidget background to be transparent

```ts
declare const dashboard: cloudwatch.Dashboard;

dashboard.addWidgets(new cloudwatch.TextWidget({
markdown: '# Key Performance Indicators',
background: TextWidgetBackground.TRANSPARENT
}));
```

### Alarm Status widget

An alarm status widget displays instantly the status of any type of alarms and gives the
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { ConcreteWidget } from './widget';

/**
* Background types available
*/
export enum TextWidgetBackground {
/**
* Solid background
*/
SOLID = 'solid',
/**
* Transparent background
*/
TRANSPARENT = 'transparent'
}

/**
* Properties for a Text widget
*/
Expand All @@ -22,17 +36,26 @@ export interface TextWidgetProps {
* @default 2
*/
readonly height?: number;

/**
* Background for the widget
*
* @default solid
*/
readonly background?: TextWidgetBackground;
}

/**
* A dashboard widget that displays MarkDown
*/
export class TextWidget extends ConcreteWidget {
private readonly markdown: string;
private readonly background?: TextWidgetBackground;

constructor(props: TextWidgetProps) {
super(props.width || 6, props.height || 2);
this.markdown = props.markdown;
this.background = props.background;
}

public position(x: number, y: number): void {
Expand All @@ -49,6 +72,7 @@ export class TextWidget extends ConcreteWidget {
y: this.y,
properties: {
markdown: this.markdown,
background: this.background,
},
}];
}
Expand Down
8 changes: 5 additions & 3 deletions packages/@aws-cdk/aws-cloudwatch/test/dashboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Template, Annotations, Match } from '@aws-cdk/assertions';
import { App, Stack } from '@aws-cdk/core';
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression } from '../lib';
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression, TextWidgetBackground } from '../lib';

describe('Dashboard', () => {
test('widgets in different adds are laid out underneath each other', () => {
Expand All @@ -13,11 +13,13 @@ describe('Dashboard', () => {
width: 10,
height: 2,
markdown: 'first',
background: TextWidgetBackground.SOLID,
}));
dashboard.addWidgets(new TextWidget({
width: 1,
height: 4,
markdown: 'second',
background: TextWidgetBackground.TRANSPARENT,
}));
dashboard.addWidgets(new TextWidget({
width: 4,
Expand All @@ -30,8 +32,8 @@ describe('Dashboard', () => {
expect(Object.keys(resources).length).toEqual(1);
const key = Object.keys(resources)[0];
hasWidgets(resources[key].Properties, [
{ type: 'text', width: 10, height: 2, x: 0, y: 0, properties: { markdown: 'first' } },
{ type: 'text', width: 1, height: 4, x: 0, y: 2, properties: { markdown: 'second' } },
{ type: 'text', width: 10, height: 2, x: 0, y: 0, properties: { markdown: 'first', background: TextWidgetBackground.SOLID } },
{ type: 'text', width: 1, height: 4, x: 0, y: 2, properties: { markdown: 'second', background: TextWidgetBackground.TRANSPARENT } },
{ type: 'text', width: 4, height: 1, x: 0, y: 6, properties: { markdown: 'third' } },
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"DashCCD7F836": {
"Type": "AWS::CloudWatch::Dashboard",
"Properties": {
"DashboardBody": "{\"widgets\":[]}"
"DashboardBody": "{\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import * as cdk from '@aws-cdk/core';
import * as integ from '@aws-cdk/integ-tests';
import * as cloudwatch from '../lib';
import { TextWidgetBackground } from '../lib';

const app = new cdk.App();

const stack = new cdk.Stack(app, 'DashboardIntegrationTestStack');

const dashboard = new cloudwatch.Dashboard(stack, 'Dash');

dashboard.addWidgets(new cloudwatch.TextWidget({
markdown: 'I don\'t have a background',
background: TextWidgetBackground.TRANSPARENT,
}));

new cdk.CfnOutput(stack, 'DashboardArn', {
value: dashboard.dashboardArn,
});
Expand Down