Skip to content
Snippets Groups Projects
Commit d36547a6 authored by Christoph Alt's avatar Christoph Alt
Browse files

fixed the variable encoding/escaping within the generated queries

parent b0a7a2fc
Branches
No related tags found
No related merge requests found
Pipeline #61321 passed
...@@ -60,7 +60,7 @@ def get_variable_condition(variable_name: str, *, tag_key: str = None) -> str: ...@@ -60,7 +60,7 @@ def get_variable_condition(variable_name: str, *, tag_key: str = None) -> str:
clean_lhs = clean_rhs clean_lhs = clean_rhs
if not clean_rhs: if not clean_rhs:
raise ValueError("Empty variable name") 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]): def join_conditions(conditions: List[str], operators: Union[List[str], str]):
......
...@@ -15,7 +15,7 @@ measurment_name = 'UniformGridGPU' ...@@ -15,7 +15,7 @@ measurment_name = 'UniformGridGPU'
q1 = Query( q1 = Query(
select_='mlupsPerProcess', select_='mlupsPerProcess',
from_=measurment_name, 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=[ group_by=[
"blocks_0", "blocks_0",
"blocks_1", "blocks_1",
......
import pytest import pytest
from dashboards.influx_queries import (Query, get_variable_condition, from dashboards.influx_queries import (
join_conditions, show_tag_values) Query,
get_variable_condition,
join_conditions,
show_tag_values,
)
def test_query(): def test_query():
q = Query( q = Query(
select_='mlupsPerProcess', select_="mlupsPerProcess",
from_='UniformGridGPU', from_="UniformGridGPU",
where_='"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/', where_='"host" =~ /^${host:regex}$/ AND "collisionSetup" =~ /^${collisionSetup:regex}$/',
group_by=[ group_by=[
"blocks_0", "blocks_0",
"blocks_1", "blocks_1",
...@@ -21,28 +25,30 @@ def test_query(): ...@@ -21,28 +25,30 @@ def test_query():
"gpuBlockSize_2", "gpuBlockSize_2",
"collisionSetup", "collisionSetup",
"stencil", "stencil",
"streamingPattern" "streamingPattern",
] ],
) )
q1 = ('SELECT "mlupsPerProcess" ' q1 = (
'FROM "UniformGridGPU" ' 'SELECT "mlupsPerProcess" '
'WHERE ("host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/) AND $timeFilter ' 'FROM "UniformGridGPU" '
'GROUP BY "blocks_0", "blocks_1", "blocks_2", ' 'WHERE ("host" =~ /^${host:regex}$/ AND "collisionSetup" =~ /^${collisionSetup:regex}$/) AND $timeFilter '
'"cellsPerBlock_0", "cellsPerBlock_1", "cellsPerBlock_2", ' 'GROUP BY "blocks_0", "blocks_1", "blocks_2", '
'"gpuBlockSize_0", "gpuBlockSize_1", "gpuBlockSize_2", ' '"cellsPerBlock_0", "cellsPerBlock_1", "cellsPerBlock_2", '
'"collisionSetup", "stencil", "streamingPattern"') '"gpuBlockSize_0", "gpuBlockSize_1", "gpuBlockSize_2", '
'"collisionSetup", "stencil", "streamingPattern"'
)
assert q1 == str(q) assert q1 == str(q)
def test_show_tag_values(): def test_show_tag_values():
s = show_tag_values("UniformGridGPU", "host") 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(): def test_get_variable_condtion():
assert get_variable_condition("host") == '"host" =~ /^$host$/' assert get_variable_condition("host") == '"host" =~ /^${host:regex}$/'
assert get_variable_condition(" host ") == '"host" =~ /^$host$/' assert get_variable_condition(" host ") == '"host" =~ /^${host:regex}$/'
with pytest.raises(ValueError): with pytest.raises(ValueError):
get_variable_condition("") get_variable_condition("")
with pytest.raises(ValueError): with pytest.raises(ValueError):
...@@ -55,21 +61,24 @@ def test_join_conditions_two(): ...@@ -55,21 +61,24 @@ def test_join_conditions_two():
h = get_variable_condition("host") h = get_variable_condition("host")
c = get_variable_condition("collisionSetup") c = get_variable_condition("collisionSetup")
actual = join_conditions([h, c], "AND") actual = join_conditions([h, c], "AND")
excpected = '"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/' excpected = '"host" =~ /^${host:regex}$/ AND "collisionSetup" =~ /^${collisionSetup:regex}$/'
assert actual == excpected assert actual == excpected
def test_join_conditions_three(): def test_join_conditions_three():
conds = [get_variable_condition(name) for name in ["host", conds = [
"collisionSetup", get_variable_condition(name)
"cellsPerBlock_0"]] for name in ["host", "collisionSetup", "cellsPerBlock_0"]
]
# excpected = '"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/' # excpected = '"host" =~ /^$host$/ AND "collisionSetup" =~ /^$collisionSetup$/'
assert join_conditions(conds, "AND") == " AND ".join(conds) assert join_conditions(conds, "AND") == " AND ".join(conds)
assert join_conditions(conds, ["AND", "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, ["AND"]) == " AND ".join(conds)
assert join_conditions(conds, ["OR"]) == " OR ".join(conds) assert join_conditions(conds, ["OR"]) == " OR ".join(conds)
excpected = ('"host" =~ /^$host$/ ' excpected = (
'AND "collisionSetup" =~ /^$collisionSetup$/ ' '"host" =~ /^${host:regex}$/ '
'OR "cellsPerBlock_0" =~ /^$cellsPerBlock_0$/') 'AND "collisionSetup" =~ /^${collisionSetup:regex}$/ '
'OR "cellsPerBlock_0" =~ /^${cellsPerBlock_0:regex}$/'
)
assert join_conditions(conds, ["AND", "OR"]) == excpected assert join_conditions(conds, ["AND", "OR"]) == excpected
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment