def _get_notification_content(self) -> NotificationContent: """ Gets a notification content, this is composed by a title and a screenshot :raises: ReportScheduleScreenshotFailedError """ screenshot_data = None url = self._get_url(user_friendly=True) if (feature_flag_manager.is_feature_enabled("ALERTS_ATTACH_REPORTS") or self._report_schedule.type == ReportScheduleType.REPORT): screenshot_data = self._get_screenshot() if not screenshot_data: return NotificationContent( name=self._report_schedule.name, text="Unexpected missing screenshot", ) if self._report_schedule.chart: name = (f"{self._report_schedule.name}: " f"{self._report_schedule.chart.slice_name}") else: name = (f"{self._report_schedule.name}: " f"{self._report_schedule.dashboard.dashboard_title}") return NotificationContent(name=name, url=url, screenshot=screenshot_data)
def _get_notification_content(self) -> NotificationContent: """ Gets a notification content, this is composed by a title and a screenshot :raises: ReportScheduleScreenshotFailedError """ csv_data = None embedded_data = None error_text = None screenshot_data = [] url = self._get_url(user_friendly=True) if ( feature_flag_manager.is_feature_enabled("ALERTS_ATTACH_REPORTS") or self._report_schedule.type == ReportScheduleType.REPORT ): if self._report_schedule.report_format == ReportDataFormat.VISUALIZATION: screenshot_data = self._get_screenshots() if not screenshot_data: error_text = "Unexpected missing screenshot" elif ( self._report_schedule.chart and self._report_schedule.report_format == ReportDataFormat.DATA ): csv_data = self._get_csv_data() if not csv_data: error_text = "Unexpected missing csv file" if error_text: return NotificationContent( name=self._report_schedule.name, text=error_text ) if ( self._report_schedule.chart and self._report_schedule.report_format == ReportDataFormat.TEXT ): embedded_data = self._get_embedded_data() if self._report_schedule.chart: name = ( f"{self._report_schedule.name}: " f"{self._report_schedule.chart.slice_name}" ) else: name = ( f"{self._report_schedule.name}: " f"{self._report_schedule.dashboard.dashboard_title}" ) return NotificationContent( name=name, url=url, screenshots=screenshot_data, description=self._report_schedule.description, csv=csv_data, embedded_data=embedded_data, )
def test_render_description_with_html() -> None: # `superset.models.helpers`, a dependency of following imports, # requires app context from superset.reports.models import ReportRecipients, ReportRecipientType from superset.reports.notifications.base import NotificationContent from superset.reports.notifications.email import EmailNotification content = NotificationContent( name="test alert", embedded_data=pd.DataFrame({ "A": [1, 2, 3], "B": [4, 5, 6], "C": ["111", "222", '<a href="http://www.example.com">333</a>'], }), description='<p>This is <a href="#">a test</a> alert</p><br />', header_data={ "notification_format": "PNG", "notification_type": "Alert", "owners": [1], "notification_source": None, "chart_id": None, "dashboard_id": None, "error_text": None, }, ) email_body = (EmailNotification( recipient=ReportRecipients(type=ReportRecipientType.EMAIL), content=content)._get_content().body) assert '<p>This is <a href="#">a test</a> alert</p><br>' in email_body assert '<td><a href="http://www.example.com">333</a></td>' in email_body
def send_error(self, name: str, message: str) -> None: """ Creates and sends a notification for an error, to all recipients :raises: ReportScheduleNotificationError """ notification_content = NotificationContent(name=name, text=message) self._send(notification_content)
def _get_notification_content(self) -> NotificationContent: """ Gets a notification content, this is composed by a title and a screenshot :raises: ReportScheduleScreenshotFailedError """ screenshot_data = self._get_screenshot() if self._report_schedule.chart: name = (f"{self._report_schedule.name}: " f"{self._report_schedule.chart.slice_name}") else: name = (f"{self._report_schedule.name}: " f"{self._report_schedule.dashboard.dashboard_title}") return NotificationContent(name=name, screenshot=screenshot_data)
def send_error(self, name: str, message: str) -> None: """ Creates and sends a notification for an error, to all recipients :raises: ReportScheduleNotificationError """ notification_content = NotificationContent(name=name, text=message) # filter recipients to recipients who are also owners owner_recipients = [ ReportRecipients( type=ReportRecipientType.EMAIL, recipient_config_json=json.dumps({"target": owner.email}), ) for owner in self._report_schedule.owners ] self._send(notification_content, owner_recipients)