Skip to content

Commit

Permalink
fix: add validator information to email/slack alerts (apache#10762)
Browse files Browse the repository at this point in the history
* added validator info to alerts

* adjusted format of messages

* added nits

Co-authored-by: Jason Davis <@dropbox.com>
  • Loading branch information
JasonD28 authored and auxten committed Nov 20, 2020
1 parent 1f8ef77 commit e70b9ff
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
13 changes: 13 additions & 0 deletions superset/models/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
"""Models for scheduled execution of jobs"""
import json
import textwrap
from datetime import datetime
from typing import Any, Optional
Expand Down Expand Up @@ -201,3 +202,15 @@ def alert(self) -> RelationshipProperty:
foreign_keys=[self.alert_id],
backref=backref("validators", cascade="all, delete-orphan"),
)

def pretty_print(self) -> str:
""" String representing the comparison that will trigger a validator """
config = json.loads(self.config)

if self.validator_type.lower() == "operator":
return f"{config['op']} {config['threshold']}"

if self.validator_type.lower() == "not null":
return "!= Null or 0"

return ""
24 changes: 15 additions & 9 deletions superset/tasks/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class AlertContent(NamedTuple):
label: str # alert name
sql: str # sql statement for alert
observation_value: str # value from observation that triggered the alert
validation_error_message: str # a string of the comparison that triggered an alert
alert_url: str # url to alert details
image_data: Optional[ScreenshotData] # data for the alert screenshot

Expand Down Expand Up @@ -571,29 +572,31 @@ def deliver_alert(

# Set all the values for the alert report
# Alternate values are used in the case of a test alert
# where an alert has no observations yet
# where an alert might not have a validator
recipients = recipients or alert.recipients
slack_channel = slack_channel or alert.slack_channel
sql = alert.sql_observer[0].sql if alert.sql_observer else ""
observation_value = (
str(alert.observations[-1].value) if alert.observations else "Value"
validation_error_message = (
str(alert.observations[-1].value) + " " + alert.validators[0].pretty_print()
if alert.validators
else ""
)

# TODO: add sql query results and validator information to alert content
if alert.slice:
alert_content = AlertContent(
alert.label,
sql,
observation_value,
alert.sql_observer[0].sql,
str(alert.observations[-1].value),
validation_error_message,
_get_url_path("AlertModelView.show", user_friendly=True, pk=alert_id),
_get_slice_screenshot(alert.slice.id),
)
else:
# TODO: dashboard delivery!
alert_content = AlertContent(
alert.label,
sql,
observation_value,
alert.sql_observer[0].sql,
str(alert.observations[-1].value),
validation_error_message,
_get_url_path("AlertModelView.show", user_friendly=True, pk=alert_id),
None,
)
Expand Down Expand Up @@ -623,6 +626,7 @@ def deliver_email_alert(alert_content: AlertContent, recipients: str) -> None:
label=alert_content.label,
sql=alert_content.sql,
observation_value=alert_content.observation_value,
validation_error_message=alert_content.validation_error_message,
image_url=image_url,
)

Expand All @@ -641,6 +645,7 @@ def deliver_slack_alert(alert_content: AlertContent, slack_channel: str) -> None
label=alert_content.label,
sql=alert_content.sql,
observation_value=alert_content.observation_value,
validation_error_message=alert_content.validation_error_message,
url=alert_content.image_data.url,
alert_url=alert_content.alert_url,
)
Expand All @@ -651,6 +656,7 @@ def deliver_slack_alert(alert_content: AlertContent, slack_channel: str) -> None
label=alert_content.label,
sql=alert_content.sql,
observation_value=alert_content.observation_value,
validation_error_message=alert_content.validation_error_message,
alert_url=alert_content.alert_url,
)

Expand Down
5 changes: 3 additions & 2 deletions superset/templates/email/alert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
under the License.
-->
<h2 style="color: red;">Alert: {{label}} &#x26A0;</h2>
<p><b>SQL Statement:</b></p>
<p><b>Query:</b></p>
<code><mark style="background-color: LightGrey; font-size: 1.1em">{{sql}}</mark></code></p>
<p><b>SQL Result</b>: {{observation_value}}</p>
<p><b>Result</b>: {{observation_value}}</p>
<p><b>Reason</b>: {{validation_error_message}}</p>
<p><a href="{{alert_url}}">View Alert Details</a></p>
<p>Click <a href="{{image_url}}">here</a> or the image below to view the chart related to this alert.</p>
<a href="{{image_url}}">
Expand Down
5 changes: 3 additions & 2 deletions superset/templates/slack/alert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
under the License.
#}
*Triggered Alert: {{label}} :redalert:*
*SQL* *Statement*:```{{sql}}```
*SQL* *Result*: {{observation_value}}
*Query*:```{{sql}}```
*Result*: {{observation_value}}
*Reason*: {{validation_error_message}}
<{{alert_url}}|View Alert Details>
<{{url}}|*Explore in Superset*>
5 changes: 3 additions & 2 deletions superset/templates/slack/alert_no_screenshot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
under the License.
#}
*Triggered Alert: {{label}} :redalert:*
*SQL* *Statement*:```{{sql}}```
*SQL* *Result*: {{observation_value}}
*Query*:```{{sql}}```
*Result*: {{observation_value}}
*Reason*: {{validation_error_message}}
<{{alert_url}}|View Alert Details>
9 changes: 5 additions & 4 deletions tests/alerts_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def test_deliver_alert_screenshot(
screenshot_mock, url_mock, email_mock, file_upload_mock, setup_database
):
dbsession = setup_database
alert = create_alert(dbsession, "SELECT 55")
alert = create_alert(dbsession, "SELECT 55", "not null", "{}")
observe(alert.id)

screenshot = read_fixture("sample.png")
Expand All @@ -328,9 +328,10 @@ def test_deliver_alert_screenshot(
"channels": alert.slack_channel,
"file": screenshot,
"initial_comment": f"\n*Triggered Alert: {alert.label} :redalert:*\n"
f"*SQL* *Statement*:```{alert.sql_observer[0].sql}```\n"
f"*SQL* *Result*: {alert.observations[-1].value}"
f"\n<http://0.0.0.0:8080/alert/show/{alert.id}"
f"*Query*:```{alert.sql_observer[0].sql}```\n"
f"*Result*: {alert.observations[-1].value}\n"
f"*Reason*: {alert.observations[-1].value} {alert.validators[0].pretty_print()}\n"
f"<http://0.0.0.0:8080/alert/show/{alert.id}"
f"|View Alert Details>\n<http://0.0.0.0:8080/superset/slice/{alert.slice_id}/"
"|*Explore in Superset*>",
"title": f"[Alert] {alert.label}",
Expand Down

0 comments on commit e70b9ff

Please sign in to comment.