You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "geido (via GitHub)" <gi...@apache.org> on 2024/06/17 15:41:35 UTC
[PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
geido opened a new pull request, #29272:
URL: https://github.com/apache/superset/pull/29272
<!---
Please write the PR title following the conventions at https://www.conventionalcommits.org/en/v1.0.0/
Example:
fix(dashboard): load charts correctly
-->
### SUMMARY
<!--- Describe the change below, including rationale and design decisions -->
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
<!--- Skip this if not applicable -->
### TESTING INSTRUCTIONS
<!--- Required! What steps can be taken to manually verify the changes? -->
### ADDITIONAL INFORMATION
<!--- Check any relevant boxes with "x" -->
<!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
- [ ] Has associated issue:
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351))
- [ ] Migration is atomic, supports rollback & is backwards-compatible
- [ ] Confirm DB migration upgrade and downgrade tested
- [ ] Runtime estimates and downtime expectations provided
- [ ] Introduces new feature or API
- [ ] Removes existing feature or API
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "lwandrebeck (via GitHub)" <gi...@apache.org>.
lwandrebeck commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2337419342
Hi there.
Hitting #29719 too, I’ve updated to 4.1.0rc2. When exporting as PDF, I now get

(dis)appearing several times, but I never get the PDF output.
Python 3.11.7 venv on Rocky Linux 9.4 x86_64, backend is PostgreSQL 16.4 through pgbouncer 1.23.1 (coming from PGDG repo) using `pool_mode=session`.
I do lack logs, the only thing I have is:
```
Sep 09 07:53:28 superset gunicorn[2297879]: df = self.database.get_df(
Sep 09 07:53:28 superset gunicorn[2297879]: ^^^^^^^^^^^^^^^^^^^^^
Sep 09 07:53:28 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/superset/models/core.py", line 677, in get_df
Sep 09 07:53:28 superset gunicorn[2297879]: self.db_engine_spec.execute(cursor, sql_, self)
Sep 09 07:53:28 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/superset/db_engine_specs/base.py", line 1835, in execute
Sep 09 07:53:28 superset gunicorn[2297879]: raise cls.get_dbapi_mapped_exception(ex) from ex
Sep 09 07:53:28 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/superset/db_engine_specs/base.py", line 1831, in execute
Sep 09 07:53:28 superset gunicorn[2297879]: cursor.execute(query)
Sep 09 07:53:28 superset gunicorn[2297879]: psycopg2.errors.DivisionByZero: division by zero
Sep 09 07:55:44 superset gunicorn[2297879]: 2024-09-09 07:55:44,224:INFO:superset.dashboards.api:Triggering screenshot ASYNC
```
Trying to dig deeper and have proper logs. If anyone has a clue, I can give it a try.
Thanks !
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "geido (via GitHub)" <gi...@apache.org>.
geido commented on code in PR #29272:
URL: https://github.com/apache/superset/pull/29272#discussion_r1668879781
##########
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadScreenshot.tsx:
##########
@@ -0,0 +1,149 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { logging, t, SupersetClient } from '@superset-ui/core';
+import { Menu } from 'src/components/Menu';
+import {
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE,
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+} from 'src/logger/LogUtils';
+import { RootState } from 'src/dashboard/types';
+import { useSelector } from 'react-redux';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DownloadScreenshotFormat } from './types';
+
+const RETRY_INTERVAL = 3000;
+const MAX_RETRIES = 30;
+
+export default function DownloadScreenshot({
+ text,
+ logEvent,
+ dashboardId,
+ format,
+ ...rest
+}: {
+ text: string;
+ dashboardId: string;
+ logEvent?: Function;
+ format: string;
+}) {
+ const activeTabs = useSelector(
+ (state: RootState) => state.dashboardState.activeTabs || [],
+ );
+ const currentTabs = [...activeTabs];
+ const { addDangerToast, addSuccessToast, addInfoToast } = useToasts();
+ const anchor = currentTabs.pop();
+
+ const onDownloadScreenshot = () => {
+ let retries = 0;
+
+ // this function checks if the image is ready
+ const checkImageReady = (imageUrl: string) =>
+ fetch(`${imageUrl}?download_format=${format}`)
+ .then(response => {
+ if (response.status === 404) {
+ throw new Error('Image not ready');
+ }
+ return response.blob();
+ })
+ .then(blob => {
+ const url = window.URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `screenshot.${format}`;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ window.URL.revokeObjectURL(url);
+ })
+ .catch(() => {
+ throw new Error('Image not ready');
+ });
+
+ // this is the functions that handles the retries
+ const fetchImageWithRetry = (imageUrl: string) => {
+ checkImageReady(imageUrl)
+ .then(() => {
+ addSuccessToast(t('The screenshot is now being downloaded.'));
+ })
+ .catch(error => {
+ // we check how many retries have been made
+ if (retries < MAX_RETRIES) {
+ retries += 1;
+ addInfoToast(
+ t(
+ 'The screenshot is being generated. Please, do not leave the page.',
+ ),
+ {
+ noDuplicate: true,
+ },
+ );
+ setTimeout(() => fetchImageWithRetry(imageUrl), RETRY_INTERVAL);
+ } else {
+ addDangerToast(
+ t(
+ 'The screenshot could not be downloaded. Please, try again later.',
+ ),
+ );
+ logging.error(error);
+ }
+ });
+ };
+
+ SupersetClient.post({
+ endpoint: `/api/v1/dashboard/${dashboardId}/cache_dashboard_screenshot`,
+ jsonPayload: {
+ anchor,
+ },
+ })
+ .then(({ json }) => {
+ const imageUrl = json?.image_url;
+ if (!imageUrl) {
+ throw new Error('No image URL in response');
+ }
+ addInfoToast(
+ t(
+ 'The screenshot is being generated. Please, do not leave the page.',
+ ),
+ );
+ fetchImageWithRetry(imageUrl);
+ })
+ .catch(error => {
+ logging.error(error);
+ addDangerToast(
+ t('The screenshot could not be downloaded. Please, try again later.'),
+ );
+ })
+ .finally(() => {
+ logEvent?.(
+ format === DownloadScreenshotFormat.PNG
+ ? LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE
+ : LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+ );
+ });
+ };
+
+ return (
+ <Menu.Item key={format} {...rest}>
+ <div onClick={onDownloadScreenshot} role="button" tabIndex={0}>
Review Comment:
This is actually necessary as the onClick is controlled at the Menu level not at the item level. At this level it would be overridden. Even we decide to control the override there is some other issue at play here that would make this fail. This is in line with previous version of the implementation of these buttons too.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "kgabryje (via GitHub)" <gi...@apache.org>.
kgabryje commented on code in PR #29272:
URL: https://github.com/apache/superset/pull/29272#discussion_r1668872138
##########
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadScreenshot.tsx:
##########
@@ -0,0 +1,149 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { logging, t, SupersetClient } from '@superset-ui/core';
+import { Menu } from 'src/components/Menu';
+import {
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE,
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+} from 'src/logger/LogUtils';
+import { RootState } from 'src/dashboard/types';
+import { useSelector } from 'react-redux';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DownloadScreenshotFormat } from './types';
+
+const RETRY_INTERVAL = 3000;
+const MAX_RETRIES = 30;
+
+export default function DownloadScreenshot({
+ text,
+ logEvent,
+ dashboardId,
+ format,
+ ...rest
+}: {
+ text: string;
+ dashboardId: string;
+ logEvent?: Function;
+ format: string;
+}) {
+ const activeTabs = useSelector(
+ (state: RootState) => state.dashboardState.activeTabs || [],
+ );
+ const currentTabs = [...activeTabs];
+ const { addDangerToast, addSuccessToast, addInfoToast } = useToasts();
+ const anchor = currentTabs.pop();
+
+ const onDownloadScreenshot = () => {
+ let retries = 0;
+
+ // this function checks if the image is ready
+ const checkImageReady = (imageUrl: string) =>
+ fetch(`${imageUrl}?download_format=${format}`)
+ .then(response => {
+ if (response.status === 404) {
+ throw new Error('Image not ready');
+ }
+ return response.blob();
+ })
+ .then(blob => {
+ const url = window.URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `screenshot.${format}`;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ window.URL.revokeObjectURL(url);
+ })
+ .catch(() => {
+ throw new Error('Image not ready');
+ });
+
+ // this is the functions that handles the retries
+ const fetchImageWithRetry = (imageUrl: string) => {
+ checkImageReady(imageUrl)
+ .then(() => {
+ addSuccessToast(t('The screenshot is now being downloaded.'));
+ })
+ .catch(error => {
+ // we check how many retries have been made
+ if (retries < MAX_RETRIES) {
+ retries += 1;
+ addInfoToast(
+ t(
+ 'The screenshot is being generated. Please, do not leave the page.',
+ ),
+ {
+ noDuplicate: true,
+ },
+ );
+ setTimeout(() => fetchImageWithRetry(imageUrl), RETRY_INTERVAL);
+ } else {
+ addDangerToast(
+ t(
+ 'The screenshot could not be downloaded. Please, try again later.',
+ ),
+ );
+ logging.error(error);
+ }
+ });
+ };
+
+ SupersetClient.post({
+ endpoint: `/api/v1/dashboard/${dashboardId}/cache_dashboard_screenshot`,
+ jsonPayload: {
+ anchor,
+ },
+ })
+ .then(({ json }) => {
+ const imageUrl = json?.image_url;
+ if (!imageUrl) {
+ throw new Error('No image URL in response');
+ }
+ addInfoToast(
+ t(
+ 'The screenshot is being generated. Please, do not leave the page.',
+ ),
+ );
+ fetchImageWithRetry(imageUrl);
+ })
+ .catch(error => {
+ logging.error(error);
+ addDangerToast(
+ t('The screenshot could not be downloaded. Please, try again later.'),
+ );
+ })
+ .finally(() => {
+ logEvent?.(
+ format === DownloadScreenshotFormat.PNG
+ ? LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE
+ : LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+ );
+ });
+ };
+
+ return (
+ <Menu.Item key={format} {...rest}>
+ <div onClick={onDownloadScreenshot} role="button" tabIndex={0}>
Review Comment:
I think `Menu.Item` accepts `onClick` so that div wrapper is not necessary
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "geido (via GitHub)" <gi...@apache.org>.
geido commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2213609676
/testenv up
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "kgabryje (via GitHub)" <gi...@apache.org>.
kgabryje commented on code in PR #29272:
URL: https://github.com/apache/superset/pull/29272#discussion_r1668869621
##########
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadScreenshot.tsx:
##########
@@ -0,0 +1,149 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { logging, t, SupersetClient } from '@superset-ui/core';
+import { Menu } from 'src/components/Menu';
+import {
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE,
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+} from 'src/logger/LogUtils';
+import { RootState } from 'src/dashboard/types';
+import { useSelector } from 'react-redux';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DownloadScreenshotFormat } from './types';
+
+const RETRY_INTERVAL = 3000;
+const MAX_RETRIES = 30;
+
+export default function DownloadScreenshot({
+ text,
+ logEvent,
+ dashboardId,
+ format,
+ ...rest
+}: {
+ text: string;
+ dashboardId: string;
+ logEvent?: Function;
+ format: string;
+}) {
+ const activeTabs = useSelector(
+ (state: RootState) => state.dashboardState.activeTabs || [],
+ );
+ const currentTabs = [...activeTabs];
+ const { addDangerToast, addSuccessToast, addInfoToast } = useToasts();
+ const anchor = currentTabs.pop();
+
+ const onDownloadScreenshot = () => {
+ let retries = 0;
+
+ // this function checks if the image is ready
+ const checkImageReady = (imageUrl: string) =>
+ fetch(`${imageUrl}?download_format=${format}`)
+ .then(response => {
+ if (response.status === 404) {
+ throw new Error('Image not ready');
+ }
+ return response.blob();
+ })
+ .then(blob => {
+ const url = window.URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `screenshot.${format}`;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ window.URL.revokeObjectURL(url);
+ })
+ .catch(() => {
Review Comment:
Does it make sense to catch it here to rethrow the same error? Unless something in `then` block could also throw?
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2236298820
Ephemeral environment shutdown and build artifacts deleted.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "kgabryje (via GitHub)" <gi...@apache.org>.
kgabryje commented on code in PR #29272:
URL: https://github.com/apache/superset/pull/29272#discussion_r1668867052
##########
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadScreenshot.tsx:
##########
@@ -0,0 +1,149 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { logging, t, SupersetClient } from '@superset-ui/core';
+import { Menu } from 'src/components/Menu';
+import {
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE,
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+} from 'src/logger/LogUtils';
+import { RootState } from 'src/dashboard/types';
+import { useSelector } from 'react-redux';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DownloadScreenshotFormat } from './types';
+
+const RETRY_INTERVAL = 3000;
+const MAX_RETRIES = 30;
+
+export default function DownloadScreenshot({
+ text,
+ logEvent,
+ dashboardId,
+ format,
+ ...rest
+}: {
+ text: string;
+ dashboardId: string;
+ logEvent?: Function;
+ format: string;
+}) {
+ const activeTabs = useSelector(
+ (state: RootState) => state.dashboardState.activeTabs || [],
Review Comment:
nit: we could get the last element with lodash `last` function
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "geido (via GitHub)" <gi...@apache.org>.
geido commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2213748105
/testenv up
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "geido (via GitHub)" <gi...@apache.org>.
geido commented on code in PR #29272:
URL: https://github.com/apache/superset/pull/29272#discussion_r1668879781
##########
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadScreenshot.tsx:
##########
@@ -0,0 +1,149 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { logging, t, SupersetClient } from '@superset-ui/core';
+import { Menu } from 'src/components/Menu';
+import {
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE,
+ LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+} from 'src/logger/LogUtils';
+import { RootState } from 'src/dashboard/types';
+import { useSelector } from 'react-redux';
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { DownloadScreenshotFormat } from './types';
+
+const RETRY_INTERVAL = 3000;
+const MAX_RETRIES = 30;
+
+export default function DownloadScreenshot({
+ text,
+ logEvent,
+ dashboardId,
+ format,
+ ...rest
+}: {
+ text: string;
+ dashboardId: string;
+ logEvent?: Function;
+ format: string;
+}) {
+ const activeTabs = useSelector(
+ (state: RootState) => state.dashboardState.activeTabs || [],
+ );
+ const currentTabs = [...activeTabs];
+ const { addDangerToast, addSuccessToast, addInfoToast } = useToasts();
+ const anchor = currentTabs.pop();
+
+ const onDownloadScreenshot = () => {
+ let retries = 0;
+
+ // this function checks if the image is ready
+ const checkImageReady = (imageUrl: string) =>
+ fetch(`${imageUrl}?download_format=${format}`)
+ .then(response => {
+ if (response.status === 404) {
+ throw new Error('Image not ready');
+ }
+ return response.blob();
+ })
+ .then(blob => {
+ const url = window.URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `screenshot.${format}`;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ window.URL.revokeObjectURL(url);
+ })
+ .catch(() => {
+ throw new Error('Image not ready');
+ });
+
+ // this is the functions that handles the retries
+ const fetchImageWithRetry = (imageUrl: string) => {
+ checkImageReady(imageUrl)
+ .then(() => {
+ addSuccessToast(t('The screenshot is now being downloaded.'));
+ })
+ .catch(error => {
+ // we check how many retries have been made
+ if (retries < MAX_RETRIES) {
+ retries += 1;
+ addInfoToast(
+ t(
+ 'The screenshot is being generated. Please, do not leave the page.',
+ ),
+ {
+ noDuplicate: true,
+ },
+ );
+ setTimeout(() => fetchImageWithRetry(imageUrl), RETRY_INTERVAL);
+ } else {
+ addDangerToast(
+ t(
+ 'The screenshot could not be downloaded. Please, try again later.',
+ ),
+ );
+ logging.error(error);
+ }
+ });
+ };
+
+ SupersetClient.post({
+ endpoint: `/api/v1/dashboard/${dashboardId}/cache_dashboard_screenshot`,
+ jsonPayload: {
+ anchor,
+ },
+ })
+ .then(({ json }) => {
+ const imageUrl = json?.image_url;
+ if (!imageUrl) {
+ throw new Error('No image URL in response');
+ }
+ addInfoToast(
+ t(
+ 'The screenshot is being generated. Please, do not leave the page.',
+ ),
+ );
+ fetchImageWithRetry(imageUrl);
+ })
+ .catch(error => {
+ logging.error(error);
+ addDangerToast(
+ t('The screenshot could not be downloaded. Please, try again later.'),
+ );
+ })
+ .finally(() => {
+ logEvent?.(
+ format === DownloadScreenshotFormat.PNG
+ ? LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE
+ : LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF,
+ );
+ });
+ };
+
+ return (
+ <Menu.Item key={format} {...rest}>
+ <div onClick={onDownloadScreenshot} role="button" tabIndex={0}>
Review Comment:
This is actually necessary as the onClick is controlled at the Menu level not at the item level. At this level it would be overridden.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2213632229
@geido Ephemeral environment spinning up at http://52.36.201.158:8080. Credentials are `admin`/`admin`. Please allow several minutes for bootstrapping and startup.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2213770274
@geido Ephemeral environment spinning up at http://35.95.67.178:8080. Credentials are `admin`/`admin`. Please allow several minutes for bootstrapping and startup.
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "geido (via GitHub)" <gi...@apache.org>.
geido merged PR #29272:
URL: https://github.com/apache/superset/pull/29272
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] refactor(Dashboard): Fetch dashboard screenshot via dedicated endpoint [superset]
Posted by "lwandrebeck (via GitHub)" <gi...@apache.org>.
lwandrebeck commented on PR #29272:
URL: https://github.com/apache/superset/pull/29272#issuecomment-2338134596
```
Sep 09 06:18:35 superset gunicorn[2297879]: 2024-09-09 06:18:35,167:ERROR:superset.utils.json:JSON is not valid Expecting value: line 1 column 1 (char 0)
Sep 09 06:18:35 superset gunicorn[2297879]: Traceback (most recent call last):
Sep 09 06:18:35 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/superset/utils/json.py", line 244, in loads
Sep 09 06:18:35 superset gunicorn[2297879]: return simplejson.loads(
Sep 09 06:18:35 superset gunicorn[2297879]: ^^^^^^^^^^^^^^^^^
Sep 09 06:18:35 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/simplejson/__init__.py", line 514, in loads
Sep 09 06:18:35 superset gunicorn[2297879]: return _default_decoder.decode(s)
Sep 09 06:18:35 superset gunicorn[2297879]: ^^^^^^^^^^^^^^^^^^^^^^^^^^
Sep 09 06:18:35 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/simplejson/decoder.py", line 386, in decode
Sep 09 06:18:35 superset gunicorn[2297879]: obj, end = self.raw_decode(s)
Sep 09 06:18:35 superset gunicorn[2297879]: ^^^^^^^^^^^^^^^^^^
Sep 09 06:18:35 superset gunicorn[2297879]: File "/home/ZZZ/superset_3.11/lib64/python3.11/site-packages/simplejson/decoder.py", line 416, in raw_decode
Sep 09 06:18:35 superset gunicorn[2297879]: return self.scan_once(s, idx=_w(s, idx).end())
Sep 09 06:18:35 superset gunicorn[2297879]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
This *may* be the culprit of PDF export failure but can’t be sure about that.
Hope that helps anyway.
Thanks
--
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.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org