From e467753bde7d95a5da8fb88ced8366d656e7308a Mon Sep 17 00:00:00 2001
From: Bjarke Madsen <bjarke@nordu.net>
Date: Tue, 5 Nov 2024 17:26:40 +0100
Subject: [PATCH] refactor render.py to easily show what is reused and what is
 not

---
 brian_dashboard_manager/templating/render.py | 309 +++++++++----------
 1 file changed, 145 insertions(+), 164 deletions(-)

diff --git a/brian_dashboard_manager/templating/render.py b/brian_dashboard_manager/templating/render.py
index b706a24..afb65e3 100644
--- a/brian_dashboard_manager/templating/render.py
+++ b/brian_dashboard_manager/templating/render.py
@@ -1,3 +1,126 @@
+BASE_DROPDOWN_PANEL = {
+    "aliasColors": {},
+    "collapsed": False,
+    "datasource": None,
+    "fill": None,
+    "fillGradient": None,
+    "legend": None,
+    "lines": None,
+    "linewidth": None,
+    "search": None,
+    "stack": None,
+    "tags": None,
+    "targets": None,
+    "type": "row",
+    "xaxis": None,
+    "yaxes": None,
+    "yaxis": None,
+}
+
+BASE_YAXES = {
+    "logBase": 1,
+    "max": None,
+    "min": 0,
+    "show": True,
+}
+
+BASE_PANEL = {
+    "bars": False,
+    "collapsed": None,
+    "dashLength": 10,
+    "dashes": False,
+    "decimals": 2,
+    "fieldConfig": {"defaults": {"custom": {}}, "overrides": []},
+    "fill": 1,
+    "fillGradient": 10,
+    "hiddenSeries": False,
+    "lines": True,
+    "nullPointMode": "null",
+    "options": {"alertThreshold": True},
+    "percentage": False,
+    "pointradius": 2,
+    "points": False,
+    "renderer": "flot",
+    "search": None,
+    "seriesOverrides": [],
+    "spaceLength": 10,
+    "steppedLine": False,
+    "tags": None,
+    "thresholds": [],
+    "timeFrom": None,
+    "timeRegions": [],
+    "timeShift": None,
+    "tooltip": {"shared": True, "sort": 0, "value_type": "individual"},
+    "type": "graph",
+    "xaxis": {
+            "buckets": None,
+            "mode": "time",
+            "name": None,
+            "show": True,
+            "values": None,
+    },
+    "yaxis": {"align": False, "alignLevel": None},
+    "targets": [],
+}
+
+BASE_LEGEND = {
+    "alignAsTable": True,
+    "avg": True,
+    "current": True,
+    "max": True,
+    "min": False,
+    "rightSide": None,
+    "show": True,
+    "total": False,
+    "values": True,
+}
+
+BASE_DASHBOARD = {
+    "id": None,
+    "uid": None,
+    "annotations": {
+        "list": [
+            {
+                "builtIn": 1,
+                "datasource": "-- Grafana --",
+                "enable": True,
+                "hide": True,
+                "iconColor": "rgba(0, 211, 255, 1)",
+                "name": "Annotations & Alerts",
+                "type": "dashboard",
+            }
+        ]
+    },
+    "editable": False,
+    "gnetId": None,
+    "graphTooltip": 0,
+    "schemaVersion": 27,
+    "style": "dark",
+    "templating": {"list": []},
+    "time": {"from": "now-24h", "to": "now"},
+    "timepicker": {},
+    "timezone": "",
+    "version": 1,
+    "links": [],
+}
+
+INFOBOX = {
+    "datasource": None,
+    "gridPos": {"h": 2, "w": 24, "x": 0, "y": 0},
+    "id": 1,
+    "options": {
+        "content": """
+            <center style="margin-top:5px;">
+                INFO: The average values displayed are only mean values for timescales of 2 days or less
+            </center>""",
+        "mode": "html"
+    },
+    "pluginVersion": "8.2.5",
+    "title": "",
+    "type": "text",
+}
+
+
 def create_dropdown_panel(title, id, y, **kwargs):
     """
     Creates a dropdown panel from the given data.
@@ -10,25 +133,10 @@ def create_dropdown_panel(title, id, y, **kwargs):
     :return: rendered dropdown panel JSON
     """
     return {
-        "aliasColors": {},
-        "collapsed": False,
-        "datasource": None,
-        "fill": None,
-        "fillGradient": None,
-        "gridPos": {"h": 1, "w": 24, "x": 0, "y": y},
+        **BASE_DROPDOWN_PANEL,
         "id": id,
-        "legend": None,
-        "lines": None,
-        "linewidth": None,
-        "search": None,
-        "stack": None,
-        "tags": None,
-        "targets": None,
+        "gridPos": {"h": 1, "w": 24, "x": 0, "y": y},
         "title": title,
-        "type": "row",
-        "xaxis": None,
-        "yaxes": None,
-        "yaxis": None,
     }
 
 
@@ -43,41 +151,28 @@ def create_yaxes(type):
     if type == "errors":
         return [
             {
+                **BASE_YAXES,
                 "format": "none",
                 "label": "errors and discards per second",
-                "logBase": 1,
-                "max": None,
-                "min": 0,
-                "show": True,
             },
             {
+                **BASE_YAXES,
                 "format": "none",
                 "label": "errors and discards per second",
-                "logBase": 1,
-                "max": None,
-                "min": 0,
-                "show": True,
-            },
-        ]
-    else:
-        return [
-            {
-                "format": "bps",
-                "label": "bits per second",
-                "logBase": 1,
-                "max": None,
-                "min": "",
-                "show": True,
-            },
-            {
-                "format": "bps",
-                "label": "bits per second",
-                "logBase": 1,
-                "max": None,
-                "min": "",
-                "show": True,
             },
         ]
+    return [
+        {
+            **BASE_YAXES,
+            "format": "bps",
+            "label": "bits per second",
+        },
+        {
+            **BASE_YAXES,
+            "format": "bps",
+            "label": "bits per second",
+        },
+    ]
 
 
 def create_panel_target(
@@ -197,64 +292,19 @@ def create_panel(
     interval = 5 if not is_lambda else 15
 
     result = {
+        **BASE_PANEL,
         "aliasColors": alias_colors or {},
-        "bars": False,
-        "collapsed": None,
-        "dashLength": 10,
-        "dashes": False,
         "datasource": datasource,
-        "decimals": 2,
-        "fieldConfig": {"defaults": {"custom": {}}, "overrides": []},
-        "fill": 1,
-        "fillGradient": 10,
         "gridPos": {"h": height, "w": width, "x": x, "y": y},
-        "hiddenSeries": False,
         "id": id,
         "interval": f"{interval}m",
-        "lines": True,
         "linewidth": linewidth,
-        "nullPointMode": "null",
-        "options": {"alertThreshold": True},
-        "percentage": False,
-        "pointradius": 2,
-        "points": False,
-        "renderer": "flot",
-        "search": None,
-        "seriesOverrides": [],
-        "spaceLength": 10,
         "stack": stack,
-        "steppedLine": False,
-        "tags": None,
-        "thresholds": [],
-        "timeFrom": None,
-        "timeRegions": [],
-        "timeShift": None,
         "title": title,
-        "tooltip": {"shared": True, "sort": 0, "value_type": "individual"},
-        "type": "graph",
-        "xaxis": {
-            "buckets": None,
-            "mode": "time",
-            "name": None,
-            "show": True,
-            "values": None,
-        },
         "yaxes": yaxes,
-        "yaxis": {"align": False, "alignLevel": None},
-        "targets": [],
     }
     if not disable_legend:
-        result["legend"] = {
-            "alignAsTable": True,
-            "avg": True,
-            "current": True,
-            "max": True,
-            "min": False,
-            "rightSide": None,
-            "show": True,
-            "total": False,
-            "values": True,
-        }
+        result["legend"] = BASE_LEGEND
 
     targets = targets or []
     for target in panel_targets or []:
@@ -263,63 +313,19 @@ def create_panel(
     return result
 
 
-def create_infobox():
-    return {
-        "datasource": None,
-        "gridPos": {"h": 2, "w": 24, "x": 0, "y": 0},
-        "id": 1,
-        "options": {
-            "content": """
-            <center style="margin-top:5px;">
-                INFO: The average values displayed are only mean values for timescales of 2 days or less
-            </center>""",
-            "mode": "html"
-        },
-        "pluginVersion": "8.2.5",
-        "title": "",
-        "type": "text",
-    }
-
-
 def render_with_aggregate_dashboard(
     nren_name, aggregate_panels, dropdown_groups, tag=None, tags=None, **_
 ):
     assert tag or tags
-    panels = [create_infobox()]
-    panels.extend(aggregate_panels)
+    panels = [INFOBOX, *aggregate_panels]
     for group in dropdown_groups:
         panels.append(group["dropdown"])
         panels.extend(group["panels"])
 
     return {
-        "id": None,
-        "uid": None,
-        "annotations": {
-            "list": [
-                {
-                    "builtIn": 1,
-                    "datasource": "-- Grafana --",
-                    "enable": True,
-                    "hide": True,
-                    "iconColor": "rgba(0, 211, 255, 1)",
-                    "name": "Annotations & Alerts",
-                    "type": "dashboard",
-                }
-            ]
-        },
-        "editable": False,
-        "gnetId": None,
-        "graphTooltip": 0,
-        "schemaVersion": 27,
-        "style": "dark",
+        **BASE_DASHBOARD,
         "tags": tags or [tag],
-        "templating": {"list": []},
-        "time": {"from": "now-24h", "to": "now"},
-        "timepicker": {},
-        "timezone": "",
         "title": nren_name,
-        "version": 1,
-        "links": [],
         "panels": panels,
     }
 
@@ -327,36 +333,11 @@ def render_with_aggregate_dashboard(
 def render_simple_dashboard(title, tag=None, tags=None, panels=None, **_):
     assert tag or tags
     return {
-        "id": None,
-        "uid": None,
-        "annotations": {
-            "list": [
-                {
-                    "builtIn": 1,
-                    "datasource": "-- Grafana --",
-                    "enable": True,
-                    "hide": True,
-                    "iconColor": "rgba(0, 211, 255, 1)",
-                    "name": "Annotations & Alerts",
-                    "type": "dashboard",
-                }
-            ]
-        },
-        "editable": False,
-        "gnetId": None,
-        "graphTooltip": 0,
-        "schemaVersion": 27,
-        "style": "dark",
+        **BASE_DASHBOARD,
         "tags": tags or [tag],
-        "templating": {"list": []},
-        "time": {"from": "now-24h", "to": "now"},
-        "timepicker": {},
-        "timezone": "",
         "title": title,
-        "version": 1,
-        "links": [],
         "panels": [
-            create_infobox(),
+            INFOBOX,
             *(panels or []),
         ],
     }
-- 
GitLab