robdiciuccio commented on a change in pull request #9944:
URL:
https://github.com/apache/incubator-superset/pull/9944#discussion_r449965034
##########
File path: superset/tasks/schedules.py
##########
@@ -410,6 +419,141 @@ def schedule_email_report( # pylint:
disable=unused-argument
raise RuntimeError("Unknown report type")
+@celery_app.task(
+ name="alerts.run_query",
+ bind=True,
+ soft_time_limit=config["EMAIL_ASYNC_TIME_LIMIT_SEC"],
+)
+def schedule_alert_query( # pylint: disable=unused-argument
+ task: Task,
+ report_type: ScheduleType,
+ schedule_id: int,
+ recipients: Optional[str] = None,
+) -> None:
+ model_cls = get_scheduler_model(report_type)
+ dbsession = db.create_scoped_session()
+ schedule = dbsession.query(model_cls).get(schedule_id)
+
+ # The user may have disabled the schedule. If so, ignore this
+ if not schedule or not schedule.active:
+ logger.info("Ignoring deactivated alert")
+ return
+
+ if report_type == ScheduleType.alert:
+ if run_alert_query(schedule, dbsession):
+ # deliver_dashboard OR deliver_slice
+ return
+ else:
+ raise RuntimeError("Unknown report type")
+
+
+class AlertState:
+ ERROR = "error"
+ TRIGGER = "trigger"
+ PASS = "pass"
+
+
+def deliver_alert(alert):
+ logging.info("Triggering alert: %s", alert)
+ img_data = None
+ if alert.slice:
+
+ chart_url = get_url_path(
+ "Superset.slice", slice_id=alert.slice.id, standalone="true"
+ )
+ screenshot = ChartScreenshot(chart_url, alert.slice.digest)
+ cache_key = screenshot.cache_key()
+ image_url = get_url_path(
+ "ChartRestApi.screenshot", pk=alert.slice.id, digest=cache_key
+ )
+
+ user =
security_manager.find_user(current_app.config["THUMBNAIL_SELENIUM_USER"])
+ img_data = screenshot.compute_and_cache(
+ user=user, cache=thumbnail_cache, force=True,
+ )
+ else:
+ image_url = "https://media.giphy.com/media/dzaUX7CAG0Ihi/giphy.gif"
+
+ # generate the email
+ subject = f"[Superset] Triggered alert: {alert.label}"
+ data = None
+ images = {"screenshot": img_data}
+ body = __(
+ textwrap.dedent(
+ """\
+ <h2>Alert: %(label)s</h2>
+ <img src="cid:screenshot" alt="%(label)s" />
+ """
+ ),
+ label=alert.label,
+ image_url=image_url,
+ )
+
+ email = EmailContent(body, data, images)
+
+ # send the email
+ _deliver_email(alert, subject, email)
+
+
+def run_alert_query(alert: Alert, session: Session) -> Optional[bool]:
+ """
+ Execute alert.sql and return value if any rows are returned
+ """
+ logger.info(f"Processing alert: {alert}")
+ database = alert.database
+ if not database:
Review comment:
We likely don't want celery to retry the failed jobs in this case.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]