Example #1
0
    def _get_csv_data(self) -> bytes:
        url = self._get_url(csv=True)
        auth_cookies = machine_auth_provider_factory.instance.get_auth_cookies(
            self._get_user())

        # To load CSV data from the endpoint the chart must have been saved
        # with its query context. For charts without saved query context we
        # get a screenshot to force the chart to produce and save the query
        # context.
        if self._report_schedule.chart.query_context is None:
            logger.warning(
                "No query context found, taking a screenshot to generate it")
            try:
                self._get_screenshot()
            except (
                    ReportScheduleScreenshotFailedError,
                    ReportScheduleScreenshotTimeout,
            ) as ex:
                raise ReportScheduleCsvFailedError(
                    "Unable to fetch CSV data because the chart has no query context "
                    "saved, and an error occurred when fetching it via a screenshot. "
                    "Please try loading the chart and saving it again."
                ) from ex

        try:
            logger.info("Getting chart from %s", url)
            csv_data = get_chart_csv_data(url, auth_cookies)
        except SoftTimeLimitExceeded:
            raise ReportScheduleCsvTimeout()
        except Exception as ex:
            raise ReportScheduleCsvFailedError(
                f"Failed generating csv {str(ex)}")
        if not csv_data:
            raise ReportScheduleCsvFailedError()
        return csv_data
Example #2
0
 def _get_csv_data(self) -> bytes:
     if self._report_schedule.chart:
         url = self._get_url(csv=True)
         auth_cookies = machine_auth_provider_factory.instance.get_auth_cookies(
             self._get_user()
         )
     try:
         csv_data = get_chart_csv_data(url, auth_cookies)
     except SoftTimeLimitExceeded:
         raise ReportScheduleCsvTimeout()
     except Exception as ex:
         raise ReportScheduleCsvFailedError(f"Failed generating csv {str(ex)}")
     if not csv_data:
         raise ReportScheduleCsvFailedError()
     return csv_data
    def _get_embedded_data(self) -> pd.DataFrame:
        """
        Return data as a Pandas dataframe, to embed in notifications as a table.
        """
        url = self._get_url(result_format=ChartDataResultFormat.JSON)
        auth_cookies = machine_auth_provider_factory.instance.get_auth_cookies(
            self._get_user()
        )

        if self._report_schedule.chart.query_context is None:
            logger.warning("No query context found, taking a screenshot to generate it")
            self._update_query_context()

        try:
            logger.info("Getting chart from %s", url)
            dataframe = get_chart_dataframe(url, auth_cookies)
        except SoftTimeLimitExceeded as ex:
            raise ReportScheduleDataFrameTimeout() from ex
        except Exception as ex:
            raise ReportScheduleDataFrameFailedError(
                f"Failed generating dataframe {str(ex)}"
            ) from ex
        if dataframe is None:
            raise ReportScheduleCsvFailedError()
        return dataframe
Example #4
0
    def _get_csv_data(self) -> bytes:
        url = self._get_url(result_format=ChartDataResultFormat.CSV)
        auth_cookies = machine_auth_provider_factory.instance.get_auth_cookies(
            self._get_user())

        if self._report_schedule.chart.query_context is None:
            logger.warning(
                "No query context found, taking a screenshot to generate it")
            self._update_query_context()

        try:
            logger.info("Getting chart from %s", url)
            csv_data = get_chart_csv_data(url, auth_cookies)
        except SoftTimeLimitExceeded as ex:
            raise ReportScheduleCsvTimeout() from ex
        except Exception as ex:
            raise ReportScheduleCsvFailedError(
                f"Failed generating csv {str(ex)}") from ex
        if not csv_data:
            raise ReportScheduleCsvFailedError()
        return csv_data
Example #5
0
    def _update_query_context(self) -> None:
        """
        Update chart query context.

        To load CSV data from the endpoint the chart must have been saved
        with its query context. For charts without saved query context we
        get a screenshot to force the chart to produce and save the query
        context.
        """
        try:
            self._get_screenshot()
        except (
                ReportScheduleScreenshotFailedError,
                ReportScheduleScreenshotTimeout,
        ) as ex:
            raise ReportScheduleCsvFailedError(
                "Unable to fetch data because the chart has no query context "
                "saved, and an error occurred when fetching it via a screenshot. "
                "Please try loading the chart and saving it again.") from ex