Skip to content

Commit

Permalink
fix(alerts): Handle None on results (#13289)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar authored Feb 23, 2021
1 parent 9e2455a commit 6e31212
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 3 additions & 2 deletions superset/reports/commands/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def run(self) -> bool:

if self._is_validator_not_null:
self._report_schedule.last_value_row_json = str(self._result)
return self._result is not None
return self._result not in (0, None, np.nan)
self._report_schedule.last_value = self._result
try:
operator = json.loads(self._report_schedule.validator_config_json)["op"]
Expand Down Expand Up @@ -90,7 +90,8 @@ def _validate_result(rows: np.recarray) -> None:

def _validate_operator(self, rows: np.recarray) -> None:
self._validate_result(rows)
if rows[0][1] is None:
if rows[0][1] in (0, None, np.nan):
self._result = 0.0
return
try:
# Check if it's float or if we can convert it
Expand Down
22 changes: 21 additions & 1 deletion tests/reports/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,17 @@ def create_alert_email_chart(request):


@pytest.yield_fixture(
params=["alert1", "alert2", "alert3", "alert4", "alert5", "alert6", "alert7"]
params=[
"alert1",
"alert2",
"alert3",
"alert4",
"alert5",
"alert6",
"alert7",
"alert8",
"alert9",
]
)
def create_no_alert_email_chart(request):
param_config = {
Expand Down Expand Up @@ -358,6 +368,16 @@ def create_no_alert_email_chart(request):
"validator_type": ReportScheduleValidatorType.OPERATOR,
"validator_config_json": '{"op": ">", "threshold": 0}',
},
"alert8": {
"sql": "SELECT Null as metric",
"validator_type": ReportScheduleValidatorType.NOT_NULL,
"validator_config_json": "{}",
},
"alert9": {
"sql": "SELECT Null as metric",
"validator_type": ReportScheduleValidatorType.OPERATOR,
"validator_config_json": '{"op": ">", "threshold": 0}',
},
}
with app.app_context():
chart = db.session.query(Slice).first()
Expand Down

0 comments on commit 6e31212

Please sign in to comment.