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

fix: allow empty observations in alerting #10939

Merged
merged 1 commit into from
Sep 21, 2020
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
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