Skip to content

Commit

Permalink
Remove extra handling for dashboards sources
Browse files Browse the repository at this point in the history
Signed-off-by: Rupal Mahajan <[email protected]>
  • Loading branch information
rupal-bq committed Jan 20, 2023
1 parent f292be3 commit a18a67e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 95 deletions.
22 changes: 0 additions & 22 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ export const DEFAULT_MIN_HEIGHT = '600';
export const DEFAULT_FILENAME = 'opensearch-report';
export const DEFAULT_EMAIL_SUBJECT = 'This is an email containing your opensearch dashboard report';

export const REPORT_TYPE = {
DASHBOARD: 'Dashboard',
VISUALIZATION: 'Visualization',
NOTEBOOK: 'Notebook',
DISCOVER: 'Saved search',
OTHER: 'Other',
}

export const SELECTOR = {
DASHBOARD: '#dashboardViewport',
VISUALIZATION: '.visEditor__content',
NOTEBOOK: '.euiPageBody',
DISCOVER: 'button[id="downloadReport"]'
}

export const FORMAT = {
PDF: 'pdf',
PNG: 'png',
Expand All @@ -40,13 +25,6 @@ export const AUTH = {
NONE: 'none',
}

export const URL_SOURCE = {
DASHBOARDS: 'dashboards#',
VISUALIZE: "Visualize",
DISCOVER: "discover#",
NOTEBOOKS: "notebooks",
}

export const ENV_VAR = {
URL: 'OPENSEARCH_URL',
USERNAME: 'OPENSEARCH_USERNAME',
Expand Down
75 changes: 2 additions & 73 deletions src/download-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import puppeteer from 'puppeteer';
import fs from 'fs';
import { FORMAT, REPORT_TYPE, SELECTOR, AUTH, URL_SOURCE } from './constants.js';
import { FORMAT, AUTH} from './constants.js';
import { exit } from "process";
import ora from 'ora';

Expand Down Expand Up @@ -64,63 +64,8 @@ export async function downloadReport(url, format, width, height, filename, authT
height: height,
});

const reportSource = getReportSourceFromURL(url);

// if its an OpenSearch report, remove extra elements.
if (reportSource !== 'Other' && reportSource !== 'Saved search') {
await page.evaluate(
(reportSource, REPORT_TYPE) => {
// remove buttons.
document
.querySelectorAll("[class^='euiButton']")
.forEach((e) => e.remove());
// remove top navBar.
document
.querySelectorAll("[class^='euiHeader']")
.forEach((e) => e.remove());
// remove visualization editor.
if (reportSource === REPORT_TYPE.VISUALIZATION) {
document
.querySelector('[data-test-subj="splitPanelResizer"]')
?.remove();
document.querySelector('.visEditor__collapsibleSidebar')?.remove();
}
document.body.style.paddingTop = '0px';
},
reportSource,
REPORT_TYPE
);
}

// force wait for any resize to load after the above DOM modification.
await new Promise(resolve => setTimeout(resolve, 1000));

switch (reportSource) {
case REPORT_TYPE.DASHBOARD:
await page.waitForSelector(SELECTOR.DASHBOARD, {
visible: true,
});
break;
case REPORT_TYPE.VISUALIZATION:
await page.waitForSelector(SELECTOR.VISUALIZATION, {
visible: true,
});
break;
case REPORT_TYPE.NOTEBOOK:
await page.waitForSelector(SELECTOR.NOTEBOOK, {
visible: true,
});
break;
case REPORT_TYPE.DISCOVER:
await page.waitForSelector(SELECTOR.DISCOVER, {
visible: true,
});
break;
default:
break;
}

await waitForDynamicContent(page);

let buffer;
spinner.text = `Downloading Report...`;

Expand Down Expand Up @@ -198,22 +143,6 @@ const waitForDynamicContent = async (
}
};

const getReportSourceFromURL = (url) => {
if (url.includes(URL_SOURCE.DASHBOARDS)) {
return REPORT_TYPE.DASHBOARD;
}
else if (url.includes(URL_SOURCE.VISUALIZE)) {
return REPORT_TYPE.VISUALIZATION;
}
else if (url.includes(URL_SOURCE.DISCOVER)) {
return REPORT_TYPE.DISCOVER;
}
else if (url.includes(URL_SOURCE.NOTEBOOKS)) {
return REPORT_TYPE.NOTEBOOK;
}
return REPORT_TYPE.OTHER;
}

const getUrl = async (url) => {
let urlExt = url.split("#");
let urlRef = "#" + urlExt[1];
Expand Down

0 comments on commit a18a67e

Please sign in to comment.