aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[cloudwatch] position of the widget in a dashboard cannot be changed

Open pushthat opened this issue 6 years ago • 14 comments

Description of the bug

The position of the widget in the cloudwatch dashboard cannot be changed using the method described in the documentation.

Reproduction Steps

    initApiErrorWidgget(apiGateways: ApiGatewayConfig[]): cloudwatch.IWidget {
        let let apiErrorWidget = new cloudwatch.GraphWidget({
        // implem details
        })
        return apiErrorWidget;
    }


    constructor(scope: cdk.Construct, id: string, props: GlobalDashboardConfig) {
        super(scope, id);

        let globalDashboard = new cloudwatch.Dashboard(this, "globalDashboard", {
            dashboardName: props.name,
        })
        let apiErrorWidget = this.initApiErrorWidgget(props.apiGateways)
        globalDashboard.addWidgets(apiErrorWidget)
        apiErrorWidget.position(9, 0)
    }

Environment

  • **CLI Version :1.50.0
  • **Framework Version:1.50.0
  • **Node.js Version:v12.18.1
  • **OS :Darwin
  • **Language (Version):TypeScript (3.9.6)

This is :bug: Bug Report

pushthat avatar Jul 08 '20 09:07 pushthat

I confirm this behaviour. 'position' seems to have no effect.

nbenaglia avatar Aug 05 '20 15:08 nbenaglia

  1. implement the position function
// create a wrapper with no position and let it auto fit
class WrappedWidgetWithNoPosition {
    private child: IWidget;
    constructor(child: IWidget) {
        this.child = child;
    }
    get width() {
        return this.child.width;
    }
    get height() {
        return this.child.height;
    }
    toJson(){
        return this.child.toJson();
    }
    position(x: number, y: number) {

    }
}
  1. use WrappedWidgetWithNoPosition to wrap of GraphWidget
dashboard.addWidgets(new WrappedWidgetWithNoPosition(widget))
  1. change back to GraphWidget if this get fixed

cc189 avatar Aug 27 '20 04:08 cc189

I was trying to create LogQueryWidget where I am trying to set the "position(x, y)" by passing the x and y values dynamically its not modifying the position parameters. eg. Valid case : where i am able to create the widget but however the Position parameters i.e. x and y are automatically set by cdk.

widget= cloudwatch.LogQueryWidget(log_group_names=[xyz],
                                    height=6,
                                    width=6,
                                    query_lines=query.split('|'),
                                    title=xyz_test,
                                    view=cloudwatch.LogQueryVisualizationType.TABLE,
                                    region=ap-south-1
                                )

Invalid Case: If i try to set the position parameter it's create the cloudformation template. But the position parameters are not modified as per the values provide by us.

eg.

widget=cloudwatch.LogQueryWidget(log_group_names=[xyz],
                                    height=6,
                                    width=6,
                                    query_lines=query.split('|'),
                                    title=xyz_test,
                                    view=cloudwatch.LogQueryVisualizationType.TABLE,
                                    region=ap-south-1
                                )
widget.position(0,6)

Please have a look into this.

SMisra92 avatar Nov 05 '20 09:11 SMisra92

Instead of hard-coding position using the position method inherited from the ConcreteWidget class a workaround and probably easier way to position widgets is to use Columns and Rows which can be used to set the widgets matrix in a dashboard.

Example usage -

        const dashboard = new Dashboard(this, "dashboard", {
            dashboardName: "foo",
            widgets: [
                [
                    new Column(widget1),
                    new Column(widget2, widget3, widget4)
                    new Column(widget5, widget6, widget7)
                ],
                [
                    widget8
                ],
                [
                    widget9,
                    widget10
                ]
            ]
        });

jkshtj avatar Nov 17 '20 06:11 jkshtj

case confirmed here as well.

Kifah avatar Jan 11 '21 20:01 Kifah

case confirmed still not fixed.

smalik2043 avatar Feb 03 '21 10:02 smalik2043

still not working

renzroque avatar May 10 '21 02:05 renzroque

We've identified where the problem is.

The L2 construct is building a CfnDashboard object. In Cloudformation docs, widgets in the dashboard are built with a string. The CDK is building that string on line 101:

https://github.com/aws/aws-cdk/blob/6179ac16f2fcab5bf5e03a3629db281018d7585b/packages/%40aws-cdk/aws-cloudwatch/lib/dashboard.ts#L95-L109

Widgets are imported into that dashboard via rows:

https://github.com/aws/aws-cdk/blob/6179ac16f2fcab5bf5e03a3629db281018d7585b/packages/%40aws-cdk/aws-cloudwatch/lib/dashboard.ts#L125-L132

These widgets are turned into strings via the Column object as seen on line 99 of the first snippet. However, column position is adjusted in the following line.

And as you can see here:

https://github.com/aws/aws-cdk/blob/6179ac16f2fcab5bf5e03a3629db281018d7585b/packages/%40aws-cdk/aws-cloudwatch/lib/layout.ts#L83-L89

When column position is adjusted, it adjusts the position of individual widgets as well, thereby always setting them to 0.

We are investigating a possible fix

peterwoodworth avatar May 19 '21 23:05 peterwoodworth

The intended way to position widgets is as @jain98 said above. This:

const widget1 = new AlarmWidget({ alarm: alarm, width: 16, height: 9 })
const widget2 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget3 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget4 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget5 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget6 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget7 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget8 = new AlarmWidget({ alarm: alarm, width: 24, height: 4 })
const widget9 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 })
const widget10 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 })

new cloudwatch.Dashboard(this, "dashboard", {
  dashboardName: "foo",
  widgets: [
    [
      new Column(widget1),
      new Column(widget2, widget3, widget4),
      new Column(widget5, widget6, widget7)
    ],
    [
      widget8
    ],
    [
      widget9,
      widget10
    ]
  ]
})

will look like this: Screen Shot 2021-05-26 at 3 53 19 PM Each array of widgets is a new Row. Let the autofilling work for you 😄

The documentation having this is misleading, as the user is not supposed to be able to set the position manually.

peterwoodworth avatar May 26 '21 23:05 peterwoodworth

const wd1 = new GraphWidget({ "width": 12,"height": 5, title: 'lamba 1', left: [new Metric({ metricName: 'Error', namespace: 'AWS/Lambda', dimensions: {'lambda_name': 'lambda_name'}, statistic: 'avg', }) ]})

      dashboard.addWidgets(wd1)

How should I add position as X and Y ?

max258-new avatar Jul 17 '21 18:07 max258-new

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Jul 17 '22 20:07 github-actions[bot]

Still an issue. What is the .position() command used for anyway?

Roseidon avatar Jul 25 '22 09:07 Roseidon

still seems to be an issue.

example below.

invocation_graph_widget = aws_cloudwatch.GraphWidget( title=f"Invocations - 1 hour", height=6, width=8, left=Invocations_metric_graph, ) errors_widget.position(x=2,y=0)

Hektor-V avatar May 23 '23 21:05 Hektor-V

The intended way to position widgets is as @Jain98 said above. This:

const widget1 = new AlarmWidget({ alarm: alarm, width: 16, height: 9 })
const widget2 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget3 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget4 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget5 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget6 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget7 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget8 = new AlarmWidget({ alarm: alarm, width: 24, height: 4 })
const widget9 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 })
const widget10 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 })

new cloudwatch.Dashboard(this, "dashboard", {
  dashboardName: "foo",
  widgets: [
    [
      new Column(widget1),
      new Column(widget2, widget3, widget4),
      new Column(widget5, widget6, widget7)
    ],
    [
      widget8
    ],
    [
      widget9,
      widget10
    ]
  ]
})

will look like this: Screen Shot 2021-05-26 at 3 53 19 PM Each array of widgets is a new Row. Let the autofilling work for you 😄

The documentation having this is misleading, as the user is not supposed to be able to set the position manually.

Thanks @peterwoodworth ! I was having a hard time positioning widgets with different heights, but this made it super simple.

citosid avatar Sep 18 '23 18:09 citosid