From 95917e0793c927ffcbf7810142435a669d1ee2c3 Mon Sep 17 00:00:00 2001 From: bogdan kyryliuk Date: Fri, 20 Mar 2020 15:44:54 -0700 Subject: [PATCH] Add extra configration to the email reports Superset email. Fix slice url Make WEBDRIVER_BASEURL a function --- superset/config.py | 5 +++++ superset/tasks/schedules.py | 41 +++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/superset/config.py b/superset/config.py index 19de94a0561c..5bd49cdfbf5d 100644 --- a/superset/config.py +++ b/superset/config.py @@ -751,6 +751,11 @@ class CeleryConfig: # pylint: disable=too-few-public-methods # The base URL to query for accessing the user interface WEBDRIVER_BASEURL = "http://0.0.0.0:8080/" +# The base URL for the email report hyperlinks. +WEBDRIVER_BASEURL_USER_FRIENDLY = WEBDRIVER_BASEURL +# Time in seconds, selenium will wait for the page to load +# and render for the email report. +EMAIL_PAGE_RENDER_WAIT = 30 # Send user to a link where they can report bugs BUG_REPORT_URL = None diff --git a/superset/tasks/schedules.py b/superset/tasks/schedules.py index 45036a8b20e2..0a356ea5034a 100644 --- a/superset/tasks/schedules.py +++ b/superset/tasks/schedules.py @@ -58,9 +58,9 @@ logger = logging.getLogger("tasks.email_reports") logger.setLevel(logging.INFO) -# Time in seconds, we will wait for the page to load and render -PAGE_RENDER_WAIT = 30 - +EMAIL_PAGE_RENDER_WAIT = config["EMAIL_PAGE_RENDER_WAIT"] +WEBDRIVER_BASEURL = config["WEBDRIVER_BASEURL"] +WEBDRIVER_BASEURL_USER_FRIENDLY = config["WEBDRIVER_BASEURL_USER_FRIENDLY"] EmailContent = namedtuple("EmailContent", ["body", "data", "images"]) @@ -152,11 +152,12 @@ def _get_auth_cookies() -> List[TypeConversionDict]: return cookies -def _get_url_path(view: str, **kwargs: Any) -> str: +def _get_url_path(view: str, user_friendly: bool = False, **kwargs: Any) -> str: with app.test_request_context(): - return urllib.parse.urljoin( - str(config["WEBDRIVER_BASEURL"]), url_for(view, **kwargs) + base_url = ( + WEBDRIVER_BASEURL_USER_FRIENDLY if user_friendly else WEBDRIVER_BASEURL ) + return urllib.parse.urljoin(str(base_url), url_for(view, **kwargs)) def create_webdriver() -> Union[ @@ -225,19 +226,22 @@ def deliver_dashboard(schedule: DashboardEmailSchedule) -> None: dashboard = schedule.dashboard dashboard_url = _get_url_path("Superset.dashboard", dashboard_id=dashboard.id) + dashboard_url_user_friendly = _get_url_path( + "Superset.dashboard", user_friendly=True, dashboard_id=dashboard.id + ) # Create a driver, fetch the page, wait for the page to render driver = create_webdriver() window = config["WEBDRIVER_WINDOW"]["dashboard"] driver.set_window_size(*window) driver.get(dashboard_url) - time.sleep(PAGE_RENDER_WAIT) + time.sleep(EMAIL_PAGE_RENDER_WAIT) # Set up a function to retry once for the element. # This is buggy in certain selenium versions with firefox driver get_element = getattr(driver, "find_element_by_class_name") element = retry_call( - get_element, fargs=["grid-container"], tries=2, delay=PAGE_RENDER_WAIT + get_element, fargs=["grid-container"], tries=2, delay=EMAIL_PAGE_RENDER_WAIT ) try: @@ -251,7 +255,7 @@ def deliver_dashboard(schedule: DashboardEmailSchedule) -> None: # Generate the email body and attachments email = _generate_mail_content( - schedule, screenshot, dashboard.dashboard_title, dashboard_url + schedule, screenshot, dashboard.dashboard_title, dashboard_url_user_friendly ) subject = __( @@ -271,7 +275,9 @@ def _get_slice_data(schedule: SliceEmailSchedule) -> EmailContent: ) # URL to include in the email - url = _get_url_path("Superset.slice", slice_id=slc.id) + slice_url_user_friendly = _get_url_path( + "Superset.slice", slice_id=slc.id, user_friendly=True + ) cookies = {} for cookie in _get_auth_cookies(): @@ -298,7 +304,7 @@ def _get_slice_data(schedule: SliceEmailSchedule) -> EmailContent: columns=columns, rows=rows, name=slc.slice_name, - link=url, + link=slice_url_user_friendly, ) elif schedule.delivery_type == EmailDeliveryType.attachment: @@ -306,7 +312,7 @@ def _get_slice_data(schedule: SliceEmailSchedule) -> EmailContent: body = __( 'Explore in Superset

', name=slc.slice_name, - url=url, + url=slice_url_user_friendly, ) return EmailContent(body, data, None) @@ -321,9 +327,12 @@ def _get_slice_visualization(schedule: SliceEmailSchedule) -> EmailContent: driver.set_window_size(*window) slice_url = _get_url_path("Superset.slice", slice_id=slc.id) + slice_url_user_friendly = _get_url_path( + "Superset.slice", slice_id=slc.id, user_friendly=True + ) driver.get(slice_url) - time.sleep(PAGE_RENDER_WAIT) + time.sleep(EMAIL_PAGE_RENDER_WAIT) # Set up a function to retry once for the element. # This is buggy in certain selenium versions with firefox driver @@ -331,7 +340,7 @@ def _get_slice_visualization(schedule: SliceEmailSchedule) -> EmailContent: driver.find_element_by_class_name, fargs=["chart-container"], tries=2, - delay=PAGE_RENDER_WAIT, + delay=EMAIL_PAGE_RENDER_WAIT, ) try: @@ -344,7 +353,9 @@ def _get_slice_visualization(schedule: SliceEmailSchedule) -> EmailContent: destroy_webdriver(driver) # Generate the email body and attachments - return _generate_mail_content(schedule, screenshot, slc.slice_name, slice_url) + return _generate_mail_content( + schedule, screenshot, slc.slice_name, slice_url_user_friendly + ) def deliver_slice(schedule: Union[DashboardEmailSchedule, SliceEmailSchedule]) -> None: