Skip to content

Commit 6a975b6

Browse files
authored
ref(workflow_engine): Refactor evaluate_value and add a metric (#95941)
# Description Was adding the metric `workflow_engine.data_condition.evaluation` but couldn't do it cleanly. so quickly refactored the evaluate_value logic to be a little cleaner and have a consistent final return value.
1 parent cf4d43e commit 6a975b6

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

src/sentry/workflow_engine/models/data_condition.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -162,36 +162,26 @@ def get_condition_result(self) -> DataConditionResult:
162162

163163
return None
164164

165-
def evaluate_value(self, value: T) -> DataConditionResult:
165+
def _evaluate_operator(self, condition_type: Condition, value: T) -> DataConditionResult:
166+
# If the condition is a base type, handle it directly
167+
op = CONDITION_OPS[condition_type]
168+
result = None
166169
try:
167-
condition_type = Condition(self.type)
168-
except ValueError:
170+
result = op(cast(Any, value), self.comparison)
171+
except TypeError:
169172
logger.exception(
170-
"Invalid condition type",
171-
extra={"type": self.type, "id": self.id},
173+
"Invalid comparison for data condition",
174+
extra={
175+
"comparison": self.comparison,
176+
"value": value,
177+
"type": self.type,
178+
"condition_id": self.id,
179+
},
172180
)
173-
return None
174-
175-
if condition_type in CONDITION_OPS:
176-
# If the condition is a base type, handle it directly
177-
op = CONDITION_OPS[Condition(self.type)]
178-
result = None
179-
try:
180-
result = op(cast(Any, value), self.comparison)
181-
except TypeError:
182-
logger.exception(
183-
"Invalid comparison for data condition",
184-
extra={
185-
"comparison": self.comparison,
186-
"value": value,
187-
"type": self.type,
188-
"condition_id": self.id,
189-
},
190-
)
191181

192-
return self.get_condition_result() if result else None
182+
return result
193183

194-
# Otherwise, we need to get the handler and evaluate the value
184+
def _evaluate_condition(self, condition_type: Condition, value: T) -> DataConditionResult:
195185
try:
196186
handler = condition_handler_registry.get(condition_type)
197187
except registry.NoRegistrationExistsError:
@@ -237,7 +227,28 @@ def evaluate_value(self, value: T) -> DataConditionResult:
237227
},
238228
)
239229

230+
return result
231+
232+
def evaluate_value(self, value: T) -> DataConditionResult:
233+
try:
234+
condition_type = Condition(self.type)
235+
except ValueError:
236+
logger.exception(
237+
"Invalid condition type",
238+
extra={"type": self.type, "id": self.id},
239+
)
240+
return None
241+
242+
result: DataConditionResult
243+
if condition_type in CONDITION_OPS:
244+
result = self._evaluate_operator(condition_type, value)
245+
else:
246+
result = self._evaluate_condition(condition_type, value)
247+
248+
metrics.incr("workflow_engine.data_condition.evaluation", tags={"type": self.type})
249+
240250
if isinstance(result, bool):
251+
# If the result is True, get the result from `.condition_result`
241252
return self.get_condition_result() if result else None
242253

243254
return result

tests/sentry/workflow_engine/models/test_data_condition.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def evaluate_value(
6969
assert dc.evaluate_value(2) == DetectorPriorityLevel.HIGH
7070

7171
dc.update(comparison={"baz": MockDataConditionEnum.FOO})
72-
assert dc.evaluate_value(0) == DetectorPriorityLevel.OK
72+
result = dc.evaluate_value(0)
73+
assert result == DetectorPriorityLevel.OK
7374
self.teardown_condition_mocks()
7475

7576
def test_bad_condition(self):

0 commit comments

Comments
 (0)