diff --git a/dashboards/dashboard_base.py b/dashboards/dashboard_base.py index 4168fb5927d2811ca0903ece571ffbbce340cf9d..b8802413a1dd328cd72a00efecd515f3b8e9fdd6 100644 --- a/dashboards/dashboard_base.py +++ b/dashboards/dashboard_base.py @@ -40,7 +40,9 @@ def build_row_repeat_dashboard(options: DashboardOptions, dataSource: str, measurment_name: str, panel_query: Query, - unit: str) -> Dashboard: + unit: str, + other_vars: List[Template] = [], + ) -> Dashboard: """Build a Dashboard that takes one query and repeats that with 2 variables.""" dashboard = Dashboard( **asdict(options), @@ -62,7 +64,7 @@ def build_row_repeat_dashboard(options: DashboardOptions, repeat=Repeat('v', row_repeat_var.name), ), ], - templating=Templating([panel_repeat_var, row_repeat_var]), + templating=Templating([panel_repeat_var, row_repeat_var, *other_vars]), time=Time('now-7d', 'now'), ) return dashboard.auto_panel_ids() diff --git a/dashboards/dashboard_list.py b/dashboards/dashboard_list.py index 2364ed7fe972304f83616e6c146e18990148c7d7..b2e06962a6721297c52124c13e9c59099742084f 100644 --- a/dashboards/dashboard_list.py +++ b/dashboards/dashboard_list.py @@ -3,7 +3,7 @@ from typing import List from dashboards.dashboard_base import (DashboardOptions, build_row_repeat_dashboard, get_dashboard_variable_query) -from dashboards.influx_queries import Query, show_tag_values +from dashboards.influx_queries import Query, join_variable_and, show_tag_values def _uniform_grid(arch: str, group_by: List[str]): @@ -19,11 +19,18 @@ def _uniform_grid(arch: str, group_by: List[str]): panel_repeat_var = get_dashboard_variable_query(panel_repeat, show_tag_values(measurment_name, panel_repeat), dataSource) + other_filter = 'cellsPerBlock_0' + cellsPerBlock_var = get_dashboard_variable_query(other_filter, + show_tag_values(measurment_name, other_filter), + dataSource + ) + where = join_variable_and([row_repeat, panel_repeat, other_filter]) query = Query( select_='mlupsPerProcess', from_=measurment_name, - where_=f'"{row_repeat}" =~ /^${row_repeat}$/ AND "{panel_repeat}" =~ /^${panel_repeat}$/', - group_by=group_by) + where_=where, + group_by=group_by + ) options = DashboardOptions( title=f'Uniform Grid {arch}', description=f"Benchmark dashboard for the Uniform Grid {arch} Benchmark from walberla", @@ -41,7 +48,9 @@ def _uniform_grid(arch: str, group_by: List[str]): dataSource, measurment_name, query, - unit) + unit, + [cellsPerBlock_var] + ) def dashboard_uniformGridGPU(): @@ -147,7 +156,7 @@ def dashboard_phasefieldallenchan(): query = Query( select_='mlupsPerProcess', from_=measurment_name, - where_=f'"{row_repeat}" =~ /^${row_repeat}$/ AND "{panel_repeat}" =~ /^${panel_repeat}$/', + where_=join_variable_and([row_repeat, panel_repeat]), group_by=[ "blocks_0", "blocks_1", diff --git a/dashboards/influx_queries.py b/dashboards/influx_queries.py index 1cc03b1d2acf1e2b6a5d6238a41586eded445491..c4f622abf5628de3d8fdb129812e4ea32330b7f5 100644 --- a/dashboards/influx_queries.py +++ b/dashboards/influx_queries.py @@ -1,5 +1,6 @@ from dataclasses import dataclass, field -from typing import List +from itertools import repeat +from typing import List, Union @dataclass @@ -22,3 +23,33 @@ class Query: def show_tag_values(table: str, key_name: str) -> str: """Return influx query to get all tag values from a measurment.""" return f"SHOW TAG VALUES FROM \"{table}\" WITH key = \"{key_name}\"" + + +def get_variable_condition(variable_name: str) -> str: + clean = variable_name.strip() + if not clean: + raise ValueError("Empty variable name") + return f'"{clean}" =~ /^${clean}$/' + + +def join_conditions(conditions: List[str], operators: Union[List[str], str]): + ops = operators + if isinstance(operators, str): + ops = repeat(operators, len(conditions) - 1) + elif len(operators) == 1: + ops = repeat(operators[0], len(conditions) - 1) + else: + if len(conditions) - 1 != len(operators): + raise ValueError("unfitting lengths of conditions and operators") + + ret = conditions[0] + for op, cond in zip(ops, conditions[1:]): + ret += f' {op} {cond}' + return ret + + +def join_variable_and(variable_names: List[str]) -> str: + return join_conditions( + [get_variable_condition(name) for name in variable_names], + "AND" + ) diff --git a/tests/test_dashboard_creation.py b/tests/test_dashboard_creation.py index 98fb976aaac40d23578636c97ec1be95d3dd186b..7ac0b2f3e5b34d8039680609791955687962c184 100644 --- a/tests/test_dashboard_creation.py +++ b/tests/test_dashboard_creation.py @@ -11,7 +11,7 @@ measurment_name = 'UniformGridGPU' q1 = Query( select_='mlupsPerProcess', from_=measurment_name, - where_='"collisionSetup" =~ /^$collisionSetup$/ AND "host" =~ /^$host$/', + where_='"collisionSetup" =~ /^$collisionSetup$/ AND "host" =~ /^$host$/ AND "cellsPerBlock_0" =~ /^$cellsPerBlock_0$/', group_by=[ "blocks_0", "blocks_1", @@ -35,6 +35,11 @@ collisionsetup_var = get_dashboard_variable_query("collisionSetup", show_tag_values(measurment_name, "collisionSetup"), dataSource) +other_filter = 'cellsPerBlock_0' +cellsPerBlock_var = get_dashboard_variable_query(other_filter, + show_tag_values(measurment_name, other_filter), + dataSource) + dashboard = Dashboard( title="Uniform Grid GPU", description="Benchmark dashboard for the Uniform Grid GPU Benchmark from walberla", @@ -63,7 +68,7 @@ dashboard = Dashboard( repeat=Repeat('v', collisionsetup_var.name), ), ], - templating=Templating([host_var, collisionsetup_var]), + templating=Templating([host_var, collisionsetup_var, cellsPerBlock_var]), time=Time('now-7d', 'now'), ).auto_panel_ids() diff --git a/tests/test_influx_queries.py b/tests/test_influx_queries.py index 76bfef092ee217584a6f6a75a428f7365e8fb54f..f0fb93783a9a0524ef020198c96a5eadcf47dd13 100644 --- a/tests/test_influx_queries.py +++ b/tests/test_influx_queries.py @@ -1,4 +1,7 @@ -from dashboards.influx_queries import Query, show_tag_values +import pytest + +from dashboards.influx_queries import (Query, get_variable_condition, + join_conditions, show_tag_values) def test_query(): @@ -35,3 +38,38 @@ def test_query(): def test_show_tag_values(): s = show_tag_values("UniformGridGPU", "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$/' + with pytest.raises(ValueError): + get_variable_condition("") + with pytest.raises(ValueError): + get_variable_condition(" ") + with pytest.raises(ValueError): + get_variable_condition("\t ") + + +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$/' + assert actual == excpected + + +def test_join_conditions_three(): + 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$/') + assert join_conditions(conds, ["AND", "OR"]) == excpected