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: add validator information to email/slack alerts #10762

Merged
merged 3 commits into from
Sep 3, 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
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