Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't crash when there is no data #7208

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,9 +968,9 @@ def get_by_id_and_org(cls, object_id, org):
return super(Alert, cls).get_by_id_and_org(object_id, org, Query)

def evaluate(self):
data = self.query_rel.latest_query_data.data
data = self.query_rel.latest_query_data.data if self.query_rel.latest_query_data else None

if data["rows"] and self.options["column"] in data["rows"][0]:
if data and data["rows"] and self.options["column"] in data["rows"][0]:
op = OPERATORS.get(self.options["op"], lambda v, t: False)

if "selector" not in self.options:
Expand Down
7 changes: 7 additions & 0 deletions tests/models/test_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ def test_evaluates_correctly_with_max_selector(self):
alert.options["selector"] = "max"
self.assertEqual(alert.evaluate(), Alert.UNKNOWN_STATE)

def test_evaluate_alerts_without_query_rel(self):
query = self.factory.create_query(latest_query_data_id=None)
alert = self.factory.create_alert(
query_rel=query, options={"selector": "first", "op": "equals", "column": "foo", "value": "1"}
)
self.assertEqual(alert.evaluate(), Alert.UNKNOWN_STATE)


class TestNextState(TestCase):
def test_numeric_value(self):
Expand Down
Loading