From 0d904890cb4a1e9fc35ad989d5e618bf8f85c041 Mon Sep 17 00:00:00 2001
From: Christoph Alt <christoph.alt@fau.de>
Date: Tue, 18 Oct 2022 14:23:06 +0200
Subject: [PATCH] added a alias building feature

---
 dashboards/dashboard_base.py | 21 +++++++++++-------
 dashboards/dashboard_list.py | 24 +++++++++++++++++----
 dashboards/legends.py        | 12 +++++++++++
 tests/test_legends.py        | 42 ++++++++++++++++++++++++++++++++++++
 tests/test_units.py          | 11 ----------
 5 files changed, 87 insertions(+), 23 deletions(-)
 create mode 100644 tests/test_legends.py
 delete mode 100644 tests/test_units.py

diff --git a/dashboards/dashboard_base.py b/dashboards/dashboard_base.py
index 7140368..7d90063 100644
--- a/dashboards/dashboard_base.py
+++ b/dashboards/dashboard_base.py
@@ -13,7 +13,7 @@ from dashboards.influx_queries import Query
 def get_influx_target(target_query: str, **kwargs) -> InfluxDBTarget:
     return InfluxDBTarget(
         query=target_query,
-        **kwargs,
+        **{k: v for k, v in kwargs.items() if v is not None},
     )
 
 
@@ -69,8 +69,19 @@ def build_row_repeat_dashboard(
     axisLabel: str,
     other_vars: List[Template] = [],
     annotations: Annotations = Annotations(),
+    alias: str = None,
 ) -> Dashboard:
     """Build a Dashboard that takes one query and repeats that with 2 variables."""
+    time_series_kwargs = {
+        'title': f'{panel_repeat_var.name}: ${panel_repeat_var.name}',
+        'dataSource': dataSource,
+        'targets': [get_influx_target(str(panel_query), alias=alias)],
+        'repeat': Repeat('h', panel_repeat_var.name),
+        'unit': unit,
+        'axisLabel': axisLabel,
+        'pointSize': 9,
+    }
+
     dashboard = Dashboard(
         **asdict(options),
         rows=[
@@ -78,13 +89,7 @@ def build_row_repeat_dashboard(
                 title=f'{row_repeat_var.name}: ${row_repeat_var.name}',
                 panels=[
                     TimeSeries(
-                        title=f'{panel_repeat_var.name}: ${panel_repeat_var.name}',
-                        dataSource=dataSource,
-                        targets=[get_influx_target(str(panel_query))],
-                        repeat=Repeat('h', panel_repeat_var.name),
-                        unit=unit,
-                        axisLabel=axisLabel,
-                        pointSize=9,
+                        **time_series_kwargs,
                     ),
                 ],
                 repeat=Repeat('v', row_repeat_var.name),
diff --git a/dashboards/dashboard_list.py b/dashboards/dashboard_list.py
index d4a1dba..85bcdba 100644
--- a/dashboards/dashboard_list.py
+++ b/dashboards/dashboard_list.py
@@ -135,6 +135,18 @@ def dashboard_phasefieldallenchan():
     panel_repeat = "host"
     unit = Units.number
     axisLabel = AxisLabel.mlups
+    alias_tags = ["blocks_0",
+                  "blocks_1",
+                  "blocks_2",
+                  "mpi_num_processes",
+                  "timeStepStrategy"]
+    # alias_descr = [
+    #     *[f"Blocks in {chr(ord('x') + i)} dimension" for i in range(3)],
+    #     "Number of Processes",
+    #     "Timestep Strategy"
+    # ]
+    #
+    # alias = build_alias(zip(alias_descr, alias_tags))
 
     options = DashboardOptions(
         title='Phase Field Allen Chan',
@@ -151,9 +163,12 @@ def dashboard_phasefieldallenchan():
                   from_=measurment_name,
                   where_=join_variable_and([row_repeat, panel_repeat]),
                   group_by=[
-                      "blocks_0", "blocks_1", "blocks_2", "cellsPerBlock_0",
-                      "mpi_num_processes", "host", "executable",
-                      "timeStepStrategy", "stencil_phase", "stencil_hydro"
+                      *alias_tags,
+                      "cellsPerBlock_0",
+                      "host",
+                      "executable",
+                      "stencil_phase",
+                      "stencil_hydro"
                   ])
 
     power_query = Query(select_="Power Core [W]",
@@ -171,7 +186,8 @@ def dashboard_phasefieldallenchan():
     dashboard = build_row_repeat_dashboard(options, row_repeat_var,
                                            panel_repeat_var, dataSource,
                                            measurment_name, query, unit, axisLabel,
-                                           annotations=annotations)
+                                           annotations=annotations,
+                                           )
     dashboard.rows = [
         pack_in_row("Power Consumption", panel_power), *dashboard.rows
     ]
diff --git a/dashboards/legends.py b/dashboards/legends.py
index b9d6c9b..f20c2e8 100644
--- a/dashboards/legends.py
+++ b/dashboards/legends.py
@@ -1,4 +1,5 @@
 from enum import Enum
+from typing import Iterable, Tuple
 
 
 class Units(str, Enum):
@@ -10,3 +11,14 @@ class Units(str, Enum):
 class AxisLabel(str, Enum):
     mlups = 'MLUPS per process'
     runtime = 'Runtime'
+
+
+def build_alias_entry(descr, var_name) -> str:
+    return f"{descr}: $tag_{var_name}"
+
+
+def build_alias(alias_entries: Iterable[Tuple[str, str]]) -> str:
+    return ", ".join(
+        build_alias_entry(descr, var_name)
+        for descr, var_name in alias_entries
+    )
diff --git a/tests/test_legends.py b/tests/test_legends.py
new file mode 100644
index 0000000..6acdf21
--- /dev/null
+++ b/tests/test_legends.py
@@ -0,0 +1,42 @@
+import pytest
+
+from dashboards.legends import AxisLabel, Units, build_alias, build_alias_entry
+
+
+def test_units():
+    assert Units.seconds == 's'
+    assert Units.milliseconds == 'ms'
+
+
+def test_labels():
+    assert AxisLabel.mlups == 'MLUPS per process'
+    assert AxisLabel.runtime == 'Runtime'
+
+
+@pytest.fixture
+def alias_content_single():
+    return ("Timestep Strategy", "timeStepStrategy")
+
+
+def alias_content_multiple(alias_content_single):
+    yield alias_content_single
+    yield ("Number of Processes", "num_mpi_processes")
+    for i in range(3):
+        dim = chr(ord('x') + i)
+        yield (f"Blocks in {dim} dimension", f"block_{i}")
+
+
+def test_entry(*alias_content):
+    expected = "Timestep Strategy: $tag_timeStepStrategy"
+    assert build_alias_entry("Timestep Strategy", "timeStepStrategy") == expected
+
+
+def test_alias_single(alias_content_single):
+    assert build_alias([alias_content_single]) == build_alias_entry(*alias_content_single)
+
+
+def test_alias_multiple(alias_content_single):
+    actual = build_alias(alias_content_multiple(alias_content_single))
+
+    for desc, var_name in alias_content_multiple(alias_content_single):
+        assert build_alias_entry(desc, var_name) in actual
diff --git a/tests/test_units.py b/tests/test_units.py
deleted file mode 100644
index c42f288..0000000
--- a/tests/test_units.py
+++ /dev/null
@@ -1,11 +0,0 @@
-from dashboards.legends import AxisLabel, Units
-
-
-def test_units():
-    assert Units.seconds == 's'
-    assert Units.milliseconds == 'ms'
-
-
-def test_labels():
-    assert AxisLabel.mlups == 'MLUPS per process'
-    assert AxisLabel.runtime == 'Runtime'
-- 
GitLab