Skip to content

Commit

Permalink
Alerts: Add more condition comparison options (#4134)
Browse files Browse the repository at this point in the history
* #4132 Add more condition comparison options

* Add arguments to fallback lambda
  • Loading branch information
kravets-levko authored and arikfr committed Sep 11, 2019
1 parent 2c98f04 commit ecf160c
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,14 +791,25 @@ def evaluate(self):
data = json_loads(self.query_rel.latest_query_data.data)

if data['rows'] and self.options['column'] in data['rows'][0]:
operators = {
'>': lambda v, t: v > t,
'>=': lambda v, t: v >= t,
'<': lambda v, t: v < t,
'<=': lambda v, t: v <= t,
'==': lambda v, t: v == t,
'!=': lambda v, t: v != t,

# backward compatibility
'greater than': lambda v, t: v > t,
'less than': lambda v, t: v < t,
'equals': lambda v, t: v == t,
}
should_trigger = operators.get(self.options['op'], lambda v, t: False)

value = data['rows'][0][self.options['column']]
op = self.options['op']
threshold = self.options['value']

if op == 'greater than' and value > self.options['value']:
new_state = self.TRIGGERED_STATE
elif op == 'less than' and value < self.options['value']:
new_state = self.TRIGGERED_STATE
elif op == 'equals' and value == self.options['value']:
if should_trigger(value, threshold):
new_state = self.TRIGGERED_STATE
else:
new_state = self.OK_STATE
Expand Down

0 comments on commit ecf160c

Please sign in to comment.