From e1d999e193c681b9df4f6cf7707231c26f6294de Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 6 Sep 2021 14:46:03 +0100 Subject: [PATCH 1/4] Add a little helper library to automatically manage the layout of panels. Signed-off-by: Tom Wilkie --- grafonnet-7.0/layout.libsonnet | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 grafonnet-7.0/layout.libsonnet diff --git a/grafonnet-7.0/layout.libsonnet b/grafonnet-7.0/layout.libsonnet new file mode 100644 index 00000000..af174ef2 --- /dev/null +++ b/grafonnet-7.0/layout.libsonnet @@ -0,0 +1,58 @@ +// layout.libsonnet - a little mixin for the Grafana dashboard type that +// automatically arranges panels on a dashboard, so you don't have to manually +// manage the positions anymore. +// +// We track a cursor in the dashboard and add panels at the cursor, moving +// the cursor to the right after each new panel. We also expose a function +// to move the cursor to the next row, and track the height of the panels added +// to make this possible. +// +// Usage +// local grafana = import 'grafonnet/grafana.libsonnet'; +// local layout = import "grafonnet-7.0/layout.libsonnet"; +// +// local dashboard = grafana.dashboard.new('Empty Dashboard') + layout; +// +// dashboard +// .addPanel(...) +// .addPanel(...) +// .nextRow() +// .addPanel(...) +// .addPanel(...) + +{ + // Track where the next panel should go using these private variables, aka "the cursor". + _cursor:: { + x: 0, + y: 0, + h: 0, + }, + + // addPanels add a panel at the cursor and moves the cursor to the right by the panel width. + addPanel(panel):: + local cursor = self._cursor; + local panelWithPos = panel { + gridPos+: { + x: cursor.x, + y: cursor.y, + }, + }; + super.addPanel(panelWithPos) + { + _cursor+:: { + x+: panel.gridPos.w, + h: + if cursor.h > panel.gridPos.h + then cursor.h + else panel.gridPos.h, + }, + }, + + // Start a new row beneath the current cursor. + nextRow():: self + { + _cursor+:: { + x: 0, + y+: super.h, + h: 0, + }, + }, +} From 3521d13cf6fd669f5392ab97c49a88a388c8b43b Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 6 Sep 2021 14:49:43 +0100 Subject: [PATCH 2/4] jsonnetfmt Signed-off-by: Tom Wilkie --- grafonnet-7.0/layout.libsonnet | 62 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/grafonnet-7.0/layout.libsonnet b/grafonnet-7.0/layout.libsonnet index af174ef2..ee49650c 100644 --- a/grafonnet-7.0/layout.libsonnet +++ b/grafonnet-7.0/layout.libsonnet @@ -21,38 +21,38 @@ // .addPanel(...) { - // Track where the next panel should go using these private variables, aka "the cursor". - _cursor:: { - x: 0, - y: 0, - h: 0, - }, + // Track where the next panel should go using these private variables, aka "the cursor". + _cursor:: { + x: 0, + y: 0, + h: 0, + }, - // addPanels add a panel at the cursor and moves the cursor to the right by the panel width. - addPanel(panel):: - local cursor = self._cursor; - local panelWithPos = panel { - gridPos+: { - x: cursor.x, - y: cursor.y, - }, - }; - super.addPanel(panelWithPos) + { - _cursor+:: { - x+: panel.gridPos.w, - h: - if cursor.h > panel.gridPos.h - then cursor.h - else panel.gridPos.h, - }, - }, + // addPanels add a panel at the cursor and moves the cursor to the right by the panel width. + addPanel(panel):: + local cursor = self._cursor; + local panelWithPos = panel { + gridPos+: { + x: cursor.x, + y: cursor.y, + }, + }; + super.addPanel(panelWithPos) + { + _cursor+:: { + x+: panel.gridPos.w, + h: + if cursor.h > panel.gridPos.h + then cursor.h + else panel.gridPos.h, + }, + }, - // Start a new row beneath the current cursor. - nextRow():: self + { - _cursor+:: { - x: 0, - y+: super.h, - h: 0, - }, + // Start a new row beneath the current cursor. + nextRow():: self + { + _cursor+:: { + x: 0, + y+: super.h, + h: 0, }, + }, } From f68483101ffd3aa8a7193a96eda801bc1c4dee5d Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Tue, 7 Sep 2021 14:11:13 +0100 Subject: [PATCH 3/4] Typo in usage example. Signed-off-by: Tom Wilkie --- grafonnet-7.0/layout.libsonnet | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grafonnet-7.0/layout.libsonnet b/grafonnet-7.0/layout.libsonnet index ee49650c..b1207648 100644 --- a/grafonnet-7.0/layout.libsonnet +++ b/grafonnet-7.0/layout.libsonnet @@ -8,7 +8,7 @@ // to make this possible. // // Usage -// local grafana = import 'grafonnet/grafana.libsonnet'; +// local grafana = import 'grafonnet-7.0/grafana.libsonnet'; // local layout = import "grafonnet-7.0/layout.libsonnet"; // // local dashboard = grafana.dashboard.new('Empty Dashboard') + layout; From dcb5a405a4b1878d58bf6e756536d67369e9636e Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 11 Oct 2021 12:34:58 +0100 Subject: [PATCH 4/4] Move layout to contrib folder. Signed-off-by: Tom Wilkie --- {grafonnet-7.0 => contrib}/layout.libsonnet | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {grafonnet-7.0 => contrib}/layout.libsonnet (100%) diff --git a/grafonnet-7.0/layout.libsonnet b/contrib/layout.libsonnet similarity index 100% rename from grafonnet-7.0/layout.libsonnet rename to contrib/layout.libsonnet