Skip to content

Commit

Permalink
Allow empty observations in alerting (#10939)
Browse files Browse the repository at this point in the history
Co-authored-by: bogdan kyryliuk <bogdankyryliuk@dropbox.com>
  • Loading branch information
bkyryliuk and bogdan-dbx committed Sep 21, 2020
1 parent 74a2270 commit 801fb40
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions superset/tasks/alerts/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def observe(alert_id: int, session: Session) -> Optional[str]:

error_msg = validate_observer_result(df, alert.id, alert.label)

if not error_msg and df.to_records()[0][1] is not None:
if not error_msg and not df.empty and df.to_records()[0][1] is not None:
value = float(df.to_records()[0][1])

observation = SQLObservation(
Expand All @@ -74,9 +74,9 @@ def validate_observer_result(
Returns an error message if the result is invalid.
"""
try:
assert (
not sql_result.empty
), f"Observer for alert <{alert_id}:{alert_label}> returned no rows"
if sql_result.empty:
# empty results are used for the not null validator
return None

rows = sql_result.to_records()

Expand Down
4 changes: 2 additions & 2 deletions tests/alerts_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ def test_alert_observer(setup_database):
assert alert3.sql_observer[0].observations[-1].value is None
assert alert3.sql_observer[0].observations[-1].error_msg is None

# Test SQLObserver with empty SQL return
# Test SQLObserver with empty SQL return, expected
alert4 = create_alert(dbsession, "SELECT first FROM test_table WHERE first = -1")
observe(alert4.id, dbsession)
assert alert4.sql_observer[0].observations[-1].value is None
assert alert4.sql_observer[0].observations[-1].error_msg is not None
assert alert4.sql_observer[0].observations[-1].error_msg is None

# Test SQLObserver with str result
alert5 = create_alert(dbsession, "SELECT 'test_string' as string_value")
Expand Down

0 comments on commit 801fb40

Please sign in to comment.