From d36547a6481f617f6a957aa5cce2caf3498048e1 Mon Sep 17 00:00:00 2001
From: Christoph Alt <christoph.alt@fau.de>
Date: Mon, 29 Jan 2024 18:47:26 +0100
Subject: [PATCH] fixed the variable encoding/escaping within the generated
 queries

---
 dashboards/influx_queries.py     |  2 +-
 tests/test_dashboard_creation.py |  2 +-
 tests/test_influx_queries.py     | 57 ++++++++++++++++++--------------
 3 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/dashboards/influx_queries.py b/dashboards/influx_queries.py
index a0277d9..ac09048 100644
--- a/dashboards/influx_queries.py
+++ b/dashboards/influx_queries.py
@@ -60,7 +60,7 @@ def get_variable_condition(variable_name: str, *, tag_key: str = None) -> str:
         clean_lhs = clean_rhs
     if not clean_rhs:
         raise ValueError("Empty variable name")
-    return f'"{clean_lhs}" =~ /^${clean_rhs}$/'
+    return f'"{clean_lhs}" =~ /^${{{clean_rhs}:regex}}$/'
 
 
 def join_conditions(conditions: List[str], operators: Union[List[str], str]):
diff --git a/tests/test_dashboard_creation.py b/tests/test_dashboard_creation.py
index 5103017..6b3f5ad 100644
--- a/tests/test_dashboard_creation.py
+++ b/tests/test_dashboard_creation.py
@@ -15,7 +15,7 @@ measurment_name = 'UniformGridGPU'
 q1 = Query(
     select_='mlupsPerProcess',
     from_=measurment_name,
-    where_='"collisionSetup" =~ /^$collisionSetup$/ AND "host" =~ /^$host$/ AND "cellsPerBlock_0" =~ /^$cellsPerBlock_0$/',
+    where_='"collisionSetup" =~ /^${collisionSetup:regex}$/ AND "host" =~ /^${host:regex}$/ AND "cellsPerBlock_0" =~ /^${cellsPerBlock_0:regex}$/',
     group_by=[
             "blocks_0",
             "blocks_1",
diff --git a/tests/test_influx_queries.py b/tests/test_influx_queries.py
index f0fb937..7864f3e 100644
--- a/tests/test_influx_queries.py
+++ b/tests/test_influx_queries.py
@@ -1,14 +1,18 @@
 import pytest
 
-from dashboards.influx_queries import (Query, get_variable_condition,
-                                       join_conditions, show_tag_values)
+from dashboards.influx_queries import (
+    Query,
+    get_variable_condition,
+    join_conditions,
+    show_tag_values,
+)
 
 
 def test_query():
     q = Query(
-        select_='mlupsPerProcess',
-        from_='UniformGridGPU',
-        where_='"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/',
+        select_="mlupsPerProcess",
+        from_="UniformGridGPU",
+        where_='"host" =~ /^${host:regex}$/ AND "collisionSetup" =~ /^${collisionSetup:regex}$/',
         group_by=[
             "blocks_0",
             "blocks_1",
@@ -21,28 +25,30 @@ def test_query():
             "gpuBlockSize_2",
             "collisionSetup",
             "stencil",
-            "streamingPattern"
-        ]
+            "streamingPattern",
+        ],
     )
 
-    q1 = ('SELECT "mlupsPerProcess" '
-          'FROM "UniformGridGPU" '
-          'WHERE ("host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/) AND $timeFilter '
-          'GROUP BY "blocks_0", "blocks_1", "blocks_2", '
-          '"cellsPerBlock_0", "cellsPerBlock_1", "cellsPerBlock_2", '
-          '"gpuBlockSize_0", "gpuBlockSize_1", "gpuBlockSize_2", '
-          '"collisionSetup", "stencil", "streamingPattern"')
+    q1 = (
+        'SELECT "mlupsPerProcess" '
+        'FROM "UniformGridGPU" '
+        'WHERE ("host" =~ /^${host:regex}$/ AND "collisionSetup" =~ /^${collisionSetup:regex}$/) AND $timeFilter '
+        'GROUP BY "blocks_0", "blocks_1", "blocks_2", '
+        '"cellsPerBlock_0", "cellsPerBlock_1", "cellsPerBlock_2", '
+        '"gpuBlockSize_0", "gpuBlockSize_1", "gpuBlockSize_2", '
+        '"collisionSetup", "stencil", "streamingPattern"'
+    )
     assert q1 == str(q)
 
 
 def test_show_tag_values():
     s = show_tag_values("UniformGridGPU", "host")
-    assert s == 'SHOW TAG VALUES FROM \"UniformGridGPU\" WITH key = \"host\"'
+    assert s == 'SHOW TAG VALUES FROM "UniformGridGPU" WITH key = "host"'
 
 
 def test_get_variable_condtion():
-    assert get_variable_condition("host") == '"host" =~ /^$host$/'
-    assert get_variable_condition(" host ") == '"host" =~ /^$host$/'
+    assert get_variable_condition("host") == '"host" =~ /^${host:regex}$/'
+    assert get_variable_condition(" host ") == '"host" =~ /^${host:regex}$/'
     with pytest.raises(ValueError):
         get_variable_condition("")
     with pytest.raises(ValueError):
@@ -55,21 +61,24 @@ def test_join_conditions_two():
     h = get_variable_condition("host")
     c = get_variable_condition("collisionSetup")
     actual = join_conditions([h, c], "AND")
-    excpected = '"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/'
+    excpected = '"host" =~ /^${host:regex}$/ AND "collisionSetup" =~ /^${collisionSetup:regex}$/'
     assert actual == excpected
 
 
 def test_join_conditions_three():
-    conds = [get_variable_condition(name) for name in ["host",
-                                                       "collisionSetup",
-                                                       "cellsPerBlock_0"]]
+    conds = [
+        get_variable_condition(name)
+        for name in ["host", "collisionSetup", "cellsPerBlock_0"]
+    ]
     # excpected = '"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/'
     assert join_conditions(conds, "AND") == " AND ".join(conds)
     assert join_conditions(conds, ["AND", "AND"]) == " AND ".join(conds)
     assert join_conditions(conds, ["AND"]) == " AND ".join(conds)
     assert join_conditions(conds, ["OR"]) == " OR ".join(conds)
 
-    excpected = ('"host" =~ /^$host$/ '
-                 'AND "collisionSetup" =~ /^$collisionSetup$/ '
-                 'OR "cellsPerBlock_0" =~ /^$cellsPerBlock_0$/')
+    excpected = (
+        '"host" =~ /^${host:regex}$/ '
+        'AND "collisionSetup" =~ /^${collisionSetup:regex}$/ '
+        'OR "cellsPerBlock_0" =~ /^${cellsPerBlock_0:regex}$/'
+    )
     assert join_conditions(conds, ["AND", "OR"]) == excpected
-- 
GitLab