|
| 1 | + @staticmethod |
| 2 | + def _get_template_config_value( |
| 3 | + config_value: str, property_name: str = None, value_key: str = "default" |
| 4 | + ): |
| 5 | + if not config_value: |
| 6 | + return None |
| 7 | + |
| 8 | + try: |
| 9 | + config_json = json.loads(config_value) |
| 10 | + |
| 11 | + if property_name: |
| 12 | + properties = config_json.get("properties", {}) |
| 13 | + field = properties.get(property_name, {}) |
| 14 | + return field.get(value_key) |
| 15 | + else: |
| 16 | + return config_json.get(value_key) |
| 17 | + except (json.JSONDecodeError, KeyError): |
| 18 | + return None |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def _validate_template_features( |
| 22 | + rule_type: str, |
| 23 | + rule_conditions: Optional[str], |
| 24 | + row_scope_filtering_enabled: Optional[bool], |
| 25 | + template_config: Optional[dict], |
| 26 | + threshold_compare_operator: Optional[ |
| 27 | + alpha_DQRuleThresholdCompareOperator |
| 28 | + ] = None, |
| 29 | + ) -> alpha_DQRuleThresholdCompareOperator: |
| 30 | + if not template_config or not template_config.get("config"): |
| 31 | + return |
| 32 | + |
| 33 | + config = template_config["config"] |
| 34 | + |
| 35 | + if ( |
| 36 | + rule_conditions |
| 37 | + and config.alpha_dq_rule_template_config_rule_conditions is None |
| 38 | + ): |
| 39 | + raise ErrorCode.DQ_RULE_TYPE_NOT_SUPPORTED.exception_with_parameters( |
| 40 | + rule_type, "rule conditions" |
| 41 | + ) |
| 42 | + |
| 43 | + if row_scope_filtering_enabled: |
| 44 | + advanced_settings = config.alpha_dq_rule_template_advanced_settings or "" |
| 45 | + if "alpha_dqRuleRowScopeFilteringEnabled" not in str(advanced_settings): |
| 46 | + raise ErrorCode.DQ_RULE_TYPE_NOT_SUPPORTED.exception_with_parameters( |
| 47 | + rule_type, "row scope filtering" |
| 48 | + ) |
| 49 | + |
| 50 | + if rule_conditions: |
| 51 | + allowed_rule_conditions = alpha_DQRule.Attributes._get_template_config_value( |
| 52 | + config.alpha_dq_rule_template_config_rule_conditions, None, "enum" |
| 53 | + ) |
| 54 | + if allowed_rule_conditions: |
| 55 | + try: |
| 56 | + rule_conditions_json = json.loads(rule_conditions) |
| 57 | + conditions = rule_conditions_json.get("conditions", []) |
| 58 | + if len(conditions) != 1: |
| 59 | + raise ErrorCode.DQ_RULE_CONDITIONS_INVALID.exception_with_parameters( |
| 60 | + f"exactly one condition required, found {len(conditions)}" |
| 61 | + ) |
| 62 | + condition_type = conditions[0].get("type") |
| 63 | + except json.JSONDecodeError: |
| 64 | + condition_type = rule_conditions |
| 65 | + |
| 66 | + if condition_type not in allowed_rule_conditions: |
| 67 | + raise ErrorCode.DQ_RULE_CONDITIONS_INVALID.exception_with_parameters( |
| 68 | + f"condition type '{condition_type}' not supported, allowed: {allowed_rule_conditions}" |
| 69 | + ) |
| 70 | + |
| 71 | + if threshold_compare_operator is None: |
| 72 | + return alpha_DQRuleThresholdCompareOperator.EQUAL |
| 73 | + elif ( |
| 74 | + threshold_compare_operator != alpha_DQRuleThresholdCompareOperator.EQUAL |
| 75 | + ): |
| 76 | + raise ErrorCode.INVALID_PARAMETER_VALUE.exception_with_parameters( |
| 77 | + f"threshold_compare_operator={threshold_compare_operator.value}", |
| 78 | + "threshold_compare_operator", |
| 79 | + "EQUAL when rule_conditions are provided", |
| 80 | + ) |
| 81 | + |
| 82 | + if threshold_compare_operator is not None: |
| 83 | + allowed_operators = alpha_DQRule.Attributes._get_template_config_value( |
| 84 | + config.alpha_dq_rule_template_config_threshold_object, |
| 85 | + "alpha_dqRuleTemplateConfigThresholdCompareOperator", |
| 86 | + "enum", |
| 87 | + ) |
| 88 | + if ( |
| 89 | + allowed_operators |
| 90 | + and threshold_compare_operator.value not in allowed_operators |
| 91 | + ): |
| 92 | + raise ErrorCode.INVALID_PARAMETER_VALUE.exception_with_parameters( |
| 93 | + f"threshold_compare_operator={threshold_compare_operator.value}", |
| 94 | + "threshold_compare_operator", |
| 95 | + f"must be one of {allowed_operators}", |
| 96 | + ) |
| 97 | + elif threshold_compare_operator is None: |
| 98 | + default_value = alpha_DQRule.Attributes._get_template_config_value( |
| 99 | + config.alpha_dq_rule_template_config_threshold_object, |
| 100 | + "alpha_dqRuleTemplateConfigThresholdCompareOperator", |
| 101 | + "default", |
| 102 | + ) |
| 103 | + if default_value: |
| 104 | + threshold_compare_operator = alpha_DQRuleThresholdCompareOperator( |
| 105 | + default_value |
| 106 | + ) |
| 107 | + |
| 108 | + return ( |
| 109 | + threshold_compare_operator |
| 110 | + or alpha_DQRuleThresholdCompareOperator.LESS_THAN_EQUAL |
| 111 | + ) |
1 | 112 |
|
2 | 113 | @staticmethod |
3 | 114 | def _generate_config_arguments_raw( |
|
12 | 123 | column: Optional[Asset] = None, |
13 | 124 | dq_priority: alpha_DQRuleAlertPriority, |
14 | 125 | description: Optional[str] = None, |
| 126 | + rule_conditions: Optional[str] = None, |
| 127 | + row_scope_filtering_enabled: Optional[bool] = None, |
15 | 128 | ) -> str: |
16 | 129 | config = { |
17 | 130 | "isAlertEnabled": is_alert_enabled, |
|
40 | 153 | if dimension is not None: |
41 | 154 | config["alpha_dqRuleTemplateConfigDimension"] = dimension |
42 | 155 |
|
| 156 | + if rule_conditions is not None: |
| 157 | + config["alpha_dqRuleTemplateConfigRuleConditions"] = json.loads( |
| 158 | + rule_conditions |
| 159 | + ) |
| 160 | + |
| 161 | + if row_scope_filtering_enabled is not None: |
| 162 | + config[ |
| 163 | + "alpha_dqRuleTemplateAdvancedSettings.alpha_dqRuleRowScopeFilteringEnabled" |
| 164 | + ] = row_scope_filtering_enabled |
| 165 | + |
43 | 166 | return json.dumps(config) |
44 | 167 |
|
45 | 168 | @staticmethod |
|
81 | 204 | dimension: Optional[alpha_DQDimension] = None, |
82 | 205 | custom_sql: Optional[str] = None, |
83 | 206 | description: Optional[str] = None, |
| 207 | + rule_conditions: Optional[str] = None, |
| 208 | + row_scope_filtering_enabled: Optional[bool] = False, |
84 | 209 | ) -> alpha_DQRule.Attributes: |
85 | 210 | template_config = client.dq_template_config_cache.get_template_config( |
86 | 211 | rule_type |
|
98 | 223 | if threshold_unit is None: |
99 | 224 | config = template_config.get("config") |
100 | 225 | if config is not None: |
101 | | - threashold_object = ( |
102 | | - config.alpha_dq_rule_template_config_threshold_object |
103 | | - ) |
104 | | - threashold_object_json = json.loads(threashold_object) |
105 | | - properties = threashold_object_json.get("properties", {}) |
106 | | - threshold_unit_field = properties.get( |
107 | | - "alpha_dqRuleTemplateConfigThresholdUnit", {} |
| 226 | + threshold_unit = alpha_DQRule.Attributes._get_template_config_value( |
| 227 | + config.alpha_dq_rule_template_config_threshold_object, |
| 228 | + "alpha_dqRuleTemplateConfigThresholdUnit", |
| 229 | + "default", |
108 | 230 | ) |
109 | | - default_value = threshold_unit_field.get("default") |
110 | | - threshold_unit = default_value |
111 | 231 |
|
112 | 232 | config_arguments_raw = ( |
113 | 233 | alpha_DQRule.Attributes._generate_config_arguments_raw( |
|
121 | 241 | column=column, |
122 | 242 | dq_priority=alert_priority, |
123 | 243 | description=description, |
| 244 | + rule_conditions=rule_conditions, |
| 245 | + row_scope_filtering_enabled=row_scope_filtering_enabled, |
124 | 246 | ) |
125 | 247 | ) |
126 | 248 |
|
|
133 | 255 | alpha_dq_rule_threshold_unit=threshold_unit, |
134 | 256 | ), |
135 | 257 | alpha_dq_rule_config_arguments_raw=config_arguments_raw, |
| 258 | + alpha_dq_rule_config_rule_conditions=rule_conditions, |
136 | 259 | ), |
137 | 260 | alpha_dq_rule_base_dataset_qualified_name=asset.qualified_name, |
138 | 261 | alpha_dq_rule_alert_priority=alert_priority, |
| 262 | + alpha_dq_rule_row_scope_filtering_enabled=row_scope_filtering_enabled, |
139 | 263 | alpha_dq_rule_source_sync_status=alpha_DQSourceSyncStatus.IN_PROGRESS, |
140 | 264 | alpha_dq_rule_status=alpha_DQRuleStatus.ACTIVE, |
141 | 265 | alpha_dq_rule_base_dataset=asset, |
|
0 commit comments