diff --git a/src/spaceone/dashboard/manager/data_table_manager/__init__.py b/src/spaceone/dashboard/manager/data_table_manager/__init__.py index 11fe2dd..7b10901 100644 --- a/src/spaceone/dashboard/manager/data_table_manager/__init__.py +++ b/src/spaceone/dashboard/manager/data_table_manager/__init__.py @@ -28,6 +28,7 @@ def __init__(self, *args, **kwargs): self.data_keys = None self.label_keys = None self.jinja_variables = None + self.jinja_variables_contain_space = [] self.state = None self.error_message = None @@ -224,12 +225,38 @@ def apply_page_df(self, page: dict) -> None: self.df = self.df.iloc[start - 1 : start + limit - 1] def is_jinja_expression(self, expression: str) -> bool: + if not expression: + return False + env = Environment() - parsed_content = env.parse(expression) + jinja_pattern = re.compile(r"\{\{\s*(.*?)\s*\}\}") + + modified_expression = re.sub( + jinja_pattern, + lambda m: "{{" + m.group(1).replace(" ", "_") + "}}", + expression, + ) + + jinja_keys_with_space = {} + if expression != modified_expression: + keys_with_space = re.findall(jinja_pattern, expression) + for key in keys_with_space: + jinja_keys_with_space[key.replace(" ", "_")] = key + + parsed_content = env.parse(modified_expression) variables = meta.find_undeclared_variables(parsed_content) if variables: + if jinja_keys_with_space: + for key, key_with_space in jinja_keys_with_space.items(): + self.jinja_variables_contain_space.append( + { + "origin_key": key_with_space, + "modified_key": key, + } + ) + self.jinja_variables = variables return bool(variables) @@ -276,6 +303,15 @@ def change_global_variables(self, expression: str, vars: dict): @staticmethod def remove_jinja_braces(expression: str) -> Union[str, float, list]: + jinja_pattern = re.compile(r"\{\{\s*(.*?)\s*\}\}") + + modified_expression = re.sub( + jinja_pattern, + lambda m: "{{" + m.group(1).replace(" ", "_") + "}}", + expression, + ) + expression = modified_expression + while "{{" in expression and "}}" in expression: if re.match(r"{{\s*(\w+)\s*}}", expression): expression = re.sub(r"{{\s*(\w+)\s*}}", r"\1", expression) @@ -310,6 +346,15 @@ def change_expression_data_type(expression: str, gv_type_map: dict) -> str: return expression + def change_space_variable(self, expression: str) -> str: + if self.jinja_variables_contain_space: + for space_variable in self.jinja_variables_contain_space: + expression = expression.replace( + space_variable["modified_key"], f"`{space_variable['origin_key']}`" + ) + + return expression + def _prepare_query_data( self, data_table_id: str, diff --git a/src/spaceone/dashboard/manager/data_table_manager/data_transformation_manager.py b/src/spaceone/dashboard/manager/data_table_manager/data_transformation_manager.py index b01451f..feb7f61 100755 --- a/src/spaceone/dashboard/manager/data_table_manager/data_transformation_manager.py +++ b/src/spaceone/dashboard/manager/data_table_manager/data_transformation_manager.py @@ -246,6 +246,7 @@ def query_data_table( condition, gv_type_map = self.change_global_variables(condition, vars) condition = self.remove_jinja_braces(condition) condition = self.change_expression_data_type(condition, gv_type_map) + condition = self.change_space_variable(condition) try: df = df.query(condition) @@ -298,6 +299,7 @@ def evaluate_data_table( ) condition = self.remove_jinja_braces(condition) condition = self.change_expression_data_type(condition, gv_type_map) + condition = self.change_space_variable(condition) if self.is_jinja_expression(value_expression): value_expression, gv_type_map = self.change_global_variables( @@ -307,6 +309,7 @@ def evaluate_data_table( value_expression = self.change_expression_data_type( value_expression, gv_type_map ) + value_expression = self.change_space_variable(value_expression) template_vars = {} for key in self.data_keys: @@ -875,6 +878,7 @@ def filter_data(self, df: pd.DataFrame, vars: dict) -> pd.DataFrame: condition, gv_type_map = self.change_global_variables(condition, vars) condition = self.remove_jinja_braces(condition) condition = self.change_expression_data_type(condition, gv_type_map) + condition = self.change_space_variable(condition) return df.query(condition).copy()