From 1481efea59696fca81a1ccd60fb147468c9ff36d Mon Sep 17 00:00:00 2001 From: Jason Davis <@dropbox.com> Date: Mon, 3 Aug 2020 00:39:09 -0700 Subject: [PATCH 1/6] added sql statement and link to chart in alert email --- superset/tasks/schedules.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/superset/tasks/schedules.py b/superset/tasks/schedules.py index 7b39362aec037..3cbefcd88b5bc 100644 --- a/superset/tasks/schedules.py +++ b/superset/tasks/schedules.py @@ -583,7 +583,9 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: image_url = get_url_path( "ChartRestApi.screenshot", pk=alert.slice.id, digest=cache_key ) - + standalone_index = chart_url.find("/?standalone=true") + if standalone_index != -1: + image_url = chart_url[:standalone_index] user = security_manager.find_user(current_app.config["THUMBNAIL_SELENIUM_USER"]) img_data = screenshot.compute_and_cache( user=user, cache=thumbnail_cache, force=True, @@ -602,10 +604,15 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: textwrap.dedent( """\

Alert: %(label)s

- %(label)s +

SQL Statement: %(sql)s

+

Click here or the image below to view the Slice related to this alert.

+ + %(label)s + """ ), label=alert.label, + sql=alert.sql, image_url=image_url, ) From e5f03b6eb5f665e99b2ab0c762c82fe2b1e33d1f Mon Sep 17 00:00:00 2001 From: Jason Davis <@dropbox.com> Date: Mon, 3 Aug 2020 17:53:54 -0700 Subject: [PATCH 2/6] pylint --- superset/tasks/schedules.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/superset/tasks/schedules.py b/superset/tasks/schedules.py index 3cbefcd88b5bc..f65b0e4fe3286 100644 --- a/superset/tasks/schedules.py +++ b/superset/tasks/schedules.py @@ -605,7 +605,8 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: """\

Alert: %(label)s

SQL Statement: %(sql)s

-

Click here or the image below to view the Slice related to this alert.

+

Click here or the image below + to view the Slice related to this alert.

%(label)s From 1683976318caa2760a9fbc5e18745912b4115e03 Mon Sep 17 00:00:00 2001 From: Jason Davis <@dropbox.com> Date: Tue, 4 Aug 2020 10:55:12 -0700 Subject: [PATCH 3/6] updated email to include link to alert --- superset/tasks/schedules.py | 17 +++++------------ superset/templates/email/alert.txt | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 superset/templates/email/alert.txt diff --git a/superset/tasks/schedules.py b/superset/tasks/schedules.py index f65b0e4fe3286..80a7fc8c184c0 100644 --- a/superset/tasks/schedules.py +++ b/superset/tasks/schedules.py @@ -573,6 +573,8 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: images = {} recipients = recipients or alert.recipients + alert_url = get_url_path("AlertModelView.show", pk=alert.id) + if alert.slice: chart_url = get_url_path( @@ -600,18 +602,9 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: data = None if img_data: images = {"screenshot": img_data} - body = __( - textwrap.dedent( - """\ -

Alert: %(label)s

-

SQL Statement: %(sql)s

-

Click here or the image below - to view the Slice related to this alert.

- - %(label)s - - """ - ), + body = render_template( + "email/alert.txt", + alert_url=alert_url, label=alert.label, sql=alert.sql, image_url=image_url, diff --git a/superset/templates/email/alert.txt b/superset/templates/email/alert.txt new file mode 100644 index 0000000000000..5540d7e0c8364 --- /dev/null +++ b/superset/templates/email/alert.txt @@ -0,0 +1,26 @@ + +

Alert: {{label}} ⚠

+

SQL Statement:

+{{sql}} +

View Alert Details

+

Click here or the image below to view the chart related to this alert.

+ + {{label}} + \ No newline at end of file From 7caf863994aa2f584c91c90109fdd5e7a5636f9b Mon Sep 17 00:00:00 2001 From: Jason Davis <@dropbox.com> Date: Tue, 4 Aug 2020 11:12:29 -0700 Subject: [PATCH 4/6] style changes --- superset/templates/email/alert.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/superset/templates/email/alert.txt b/superset/templates/email/alert.txt index 5540d7e0c8364..6a37f59116b17 100644 --- a/superset/templates/email/alert.txt +++ b/superset/templates/email/alert.txt @@ -17,8 +17,8 @@ under the License. -->

Alert: {{label}} ⚠

-

SQL Statement:

-{{sql}} +

SQL Statement: +{{sql}}

View Alert Details

Click here or the image below to view the chart related to this alert.

From 514fff8eb6e9fd4e23447083b6503e1c90b68210 Mon Sep 17 00:00:00 2001 From: Jason Davis <@dropbox.com> Date: Tue, 4 Aug 2020 11:46:39 -0700 Subject: [PATCH 5/6] pylint --- superset/tasks/schedules.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/superset/tasks/schedules.py b/superset/tasks/schedules.py index 80a7fc8c184c0..86a47127159d6 100644 --- a/superset/tasks/schedules.py +++ b/superset/tasks/schedules.py @@ -18,7 +18,6 @@ """Utility functions used across Superset""" import logging -import textwrap import time import urllib.request from collections import namedtuple @@ -573,8 +572,6 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: images = {} recipients = recipients or alert.recipients - alert_url = get_url_path("AlertModelView.show", pk=alert.id) - if alert.slice: chart_url = get_url_path( @@ -588,6 +585,7 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: standalone_index = chart_url.find("/?standalone=true") if standalone_index != -1: image_url = chart_url[:standalone_index] + user = security_manager.find_user(current_app.config["THUMBNAIL_SELENIUM_USER"]) img_data = screenshot.compute_and_cache( user=user, cache=thumbnail_cache, force=True, @@ -604,7 +602,7 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: images = {"screenshot": img_data} body = render_template( "email/alert.txt", - alert_url=alert_url, + alert_url=get_url_path("AlertModelView.show", pk=alert.id), label=alert.label, sql=alert.sql, image_url=image_url, From 77ee4902cf971166854eb356aa694aa1218879c8 Mon Sep 17 00:00:00 2001 From: Jason Davis <@dropbox.com> Date: Tue, 4 Aug 2020 12:46:45 -0700 Subject: [PATCH 6/6] added todo and fixed formatting of email --- superset/tasks/schedules.py | 1 + superset/templates/email/alert.txt | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/superset/tasks/schedules.py b/superset/tasks/schedules.py index 86a47127159d6..9dbdb54312440 100644 --- a/superset/tasks/schedules.py +++ b/superset/tasks/schedules.py @@ -595,6 +595,7 @@ def deliver_alert(alert: Alert, recipients: Optional[str] = None) -> None: image_url = "https://media.giphy.com/media/dzaUX7CAG0Ihi/giphy.gif" # generate the email + # TODO add sql query results to email subject = f"[Superset] Triggered alert: {alert.label}" deliver_as_group = False data = None diff --git a/superset/templates/email/alert.txt b/superset/templates/email/alert.txt index 6a37f59116b17..663b51fb59cc7 100644 --- a/superset/templates/email/alert.txt +++ b/superset/templates/email/alert.txt @@ -17,8 +17,8 @@ under the License. -->

Alert: {{label}} ⚠

-

SQL Statement: -{{sql}}

+

SQL Statement:

+{{sql}}

View Alert Details

Click here or the image below to view the chart related to this alert.