Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Dashboard): Sync/Async Dashboard Screenshot Generation #30755

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
317 changes: 307 additions & 10 deletions superset-frontend/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions superset-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"d3-scale": "^2.1.2",
"dayjs": "^1.11.13",
"dom-to-image-more": "^3.2.0",
"dom-to-pdf": "^0.3.2",
"emotion-rgba": "0.0.12",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export enum FeatureFlag {
UseAnalagousColors = 'USE_ANALAGOUS_COLORS',
ForceSqlLabRunAsync = 'SQLLAB_FORCE_RUN_ASYNC',
SlackEnableAvatars = 'SLACK_ENABLE_AVATARS',
EnableDashboardScreenshotEndpoints = 'ENABLE_DASHBOARD_SCREENSHOT_ENDPOINTS',
EnableDashboardDownloadWebDriverScreenshot = 'ENABLE_DASHBOARD_DOWNLOAD_WEBDRIVER_SCREENSHOT',
}

export type ScheduleQueriesProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ import { Menu } from 'src/components/Menu';
import downloadAsImage from 'src/utils/downloadAsImage';
import DownloadAsImage from './DownloadAsImage';

const mockAddDangerToast = jest.fn();

jest.mock('src/utils/downloadAsImage', () => ({
__esModule: true,
default: jest.fn(() => (_e: SyntheticEvent) => {}),
}));

jest.mock('src/components/MessageToasts/withToasts', () => ({
useToasts: () => ({
addDangerToast: mockAddDangerToast,
}),
}));

const createProps = () => ({
addDangerToast: jest.fn(),
text: 'Download as Image',
dashboardTitle: 'Test Dashboard',
logEvent: jest.fn(),
Expand All @@ -40,22 +47,24 @@ const renderComponent = () => {
<Menu>
<DownloadAsImage {...createProps()} />
</Menu>,
{
useRedux: true,
},
);
};

test('Should call download image on click', async () => {
const props = createProps();
renderComponent();
await waitFor(() => {
expect(downloadAsImage).toHaveBeenCalledTimes(0);
expect(props.addDangerToast).toHaveBeenCalledTimes(0);
expect(mockAddDangerToast).toHaveBeenCalledTimes(0);
});

userEvent.click(screen.getByRole('button', { name: 'Download as Image' }));

await waitFor(() => {
expect(downloadAsImage).toHaveBeenCalledTimes(1);
expect(props.addDangerToast).toHaveBeenCalledTimes(0);
expect(mockAddDangerToast).toHaveBeenCalledTimes(0);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ import { logging, t } from '@superset-ui/core';
import { Menu } from 'src/components/Menu';
import { LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE } from 'src/logger/LogUtils';
import downloadAsImage from 'src/utils/downloadAsImage';
import { useToasts } from 'src/components/MessageToasts/withToasts';

export default function DownloadAsImage({
text,
logEvent,
dashboardTitle,
addDangerToast,
...rest
}: {
text: string;
addDangerToast: Function;
dashboardTitle: string;
logEvent?: Function;
}) {
const SCREENSHOT_NODE_SELECTOR = '.dashboard';
const { addDangerToast } = useToasts();
const onDownloadImage = async (e: SyntheticEvent) => {
try {
downloadAsImage(SCREENSHOT_NODE_SELECTOR, dashboardTitle, true)(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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 { SyntheticEvent } from 'react';
import { render, screen, waitFor } from 'spec/helpers/testing-library';
import userEvent from '@testing-library/user-event';
import { Menu } from 'src/components/Menu';
import downloadAsPdf from 'src/utils/downloadAsPdf';
import DownloadAsPdf from './DownloadAsPdf';

const mockAddDangerToast = jest.fn();

jest.mock('src/utils/downloadAsPdf', () => ({
__esModule: true,
default: jest.fn(() => (_e: SyntheticEvent) => {}),
}));

jest.mock('src/components/MessageToasts/withToasts', () => ({
useToasts: () => ({
addDangerToast: mockAddDangerToast,
}),
}));

const createProps = () => ({
text: 'Export as PDF',
dashboardTitle: 'Test Dashboard',
logEvent: jest.fn(),
});

const renderComponent = () => {
render(
<Menu>
<DownloadAsPdf {...createProps()} />
</Menu>,
{ useRedux: true },
);
};

test('Should call download pdf on click', async () => {
renderComponent();
await waitFor(() => {
expect(downloadAsPdf).toHaveBeenCalledTimes(0);
expect(mockAddDangerToast).toHaveBeenCalledTimes(0);
});

userEvent.click(screen.getByRole('button', { name: 'Export as PDF' }));

await waitFor(() => {
expect(downloadAsPdf).toHaveBeenCalledTimes(1);
expect(mockAddDangerToast).toHaveBeenCalledTimes(0);
});
});

test('Component is rendered with role="button"', async () => {
renderComponent();
const button = screen.getByRole('button', { name: 'Export as PDF' });
expect(button).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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 { SyntheticEvent } from 'react';
import { logging, t } from '@superset-ui/core';
import { Menu } from 'src/components/Menu';
import downloadAsPdf from 'src/utils/downloadAsPdf';
import { LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF } from 'src/logger/LogUtils';
import { useToasts } from 'src/components/MessageToasts/withToasts';

export default function DownloadAsPdf({
text,
logEvent,
dashboardTitle,
...rest
}: {
text: string;
dashboardTitle: string;
logEvent?: Function;
}) {
const SCREENSHOT_NODE_SELECTOR = '.dashboard';
const { addDangerToast } = useToasts();
const onDownloadPdf = async (e: SyntheticEvent) => {
try {
downloadAsPdf(SCREENSHOT_NODE_SELECTOR, dashboardTitle, true)(e);
} catch (error) {
logging.error(error);
addDangerToast(t('Sorry, something went wrong. Try again later.'));
}
logEvent?.(LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF);
};

return (
<Menu.Item key="download-pdf" {...rest}>
<div onClick={onDownloadPdf} role="button" tabIndex={0}>
{text}
</div>
</Menu.Item>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ describe('DownloadScreenshot component', () => {
await waitFor(() => {
expect(mockAddInfoToast).toHaveBeenCalledWith(
'The screenshot is being generated. Please, do not leave the page.',
{
noDuplicate: true,
},
);
});
});
Expand Down Expand Up @@ -202,7 +205,7 @@ describe('DownloadScreenshot component', () => {
// Wait for the successful image retrieval message
await waitFor(() => {
expect(mockAddSuccessToast).toHaveBeenCalledWith(
'The screenshot is now being downloaded.',
'The screenshot has been downloaded.',
);
});
});
Expand Down
Loading
Loading