grafana / grafonnet

Jsonnet library for generating Grafana dashboards.
https://grafana.github.io/grafonnet/
Apache License 2.0
352 stars 19 forks source link

Add utils.grid.wrapPanels function #110

Closed v-zhuravlev closed 1 year ago

v-zhuravlev commented 1 year ago

This util function can be used to position an array of panels to the grid, without providing gridPos.x,gridPos.y coordinates. Once the total width of panels reaches total length of 24, it wraps to next 'row'.

This can serve as an alternative to makeGrid, especially if you want to use various width for panels.

This function uses gridPos.h and gridPos.w defined in panels by default, and if it is missing, would use panelWidth, panelHeight default values. x and y are calculated.

Example1:

            + g.dashboard.withPanels(
                g.util.grid.wrapPanels(
                    [
                        g.panel.stat.new("test1")
                        + g.panel.stat.gridPos.withH(3),
                        g.panel.stat.new("test2") {gridPos:{w:6, h:3}},
                        g.panel.stat.new("test3")
                        + g.panel.stat.gridPos.withH(3),
                        g.panel.stat.new("test4")
                        + g.panel.stat.gridPos.withW(20)
                        + g.panel.stat.gridPos.withH(3),
                        g.panel.stat.new("test5")
                        + g.panel.stat.gridPos.withW(6)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.stat.new("test6")
                        + g.panel.stat.gridPos.withW(10)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.stat.new("test7")
                        + g.panel.stat.gridPos.withW(2)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.stat.new("test8")
                        + g.panel.stat.gridPos.withW(20)
                        + g.panel.stat.gridPos.withH(6),
                    ], panelWidth=8, panelHeight=8)
            )
image

Example2:

            + g.dashboard.withPanels(
                g.util.grid.wrapPanels(
                    [
                        g.panel.row.new("Overview"),
                        g.panel.stat.new("test1"),
                        g.panel.stat.new("test2"),
                        g.panel.stat.new("test3"),
                        g.panel.stat.new("test4"),
                        g.panel.stat.new("test5"),
                        g.panel.stat.new("test6"),
                        g.panel.stat.new("test7"),
                        g.panel.stat.new("test8"),
                        g.panel.row.new("CPU"),
                        g.panel.timeSeries.new("test9")
                        + g.panel.stat.gridPos.withW(6)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.timeSeries.new("test10")
                        + g.panel.stat.gridPos.withW(12)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.timeSeries.new("test11")
                        + g.panel.stat.gridPos.withW(6)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.row.new("Memory"),
                        g.panel.timeSeries.new("test12")
                        + g.panel.stat.gridPos.withW(6)
                        + g.panel.stat.gridPos.withH(6),
                        g.panel.timeSeries.new("test13")
                        + g.panel.stat.gridPos.withW(18)
                        + g.panel.stat.gridPos.withH(6),
                    ],panelWidth=6, panelHeight=2)
            )

Would generate grid identical to one used in linux overview dashboard:

image image
v-zhuravlev commented 1 year ago

Fixed a bug (you can see that test4 comes before test1 ) and added row handling (shift Y when row encountered).

v-zhuravlev commented 1 year ago

The calculations looks fine, can't find an immediate problem. The code could be a bit more concise and perhaps a few more comments to explain what each if/else statement means in the process (eg. "start of new row as width exceeds gridWidth")

One potential problem: this doesn't take collapsed rows into account. I was looking at makeGrid and noticed I've inlined the logic to 'group' panels to their rows instead of making it generally available through a function, might be useful to incorporate that.

Added few more comments to explain main if/else conditionals. As of collapsed rows, I don't quite understand the logic as of now.