From d184be523393f54b734f17cd31e53fad237b3971 Mon Sep 17 00:00:00 2001 From: Rupal Mahajan Date: Thu, 22 Feb 2024 14:14:43 -0800 Subject: [PATCH 1/4] add configurable timeout Signed-off-by: Rupal Mahajan --- src/arguments.js | 15 ++++++++++++--- src/constants.js | 6 ++++-- src/download-helpers.js | 14 +++++++------- src/run.js | 3 ++- test/help.test.js | 1 + 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/arguments.js b/src/arguments.js index d9e4b80..0099183 100644 --- a/src/arguments.js +++ b/src/arguments.js @@ -8,7 +8,7 @@ const { program, Option } = require('commander'); const { exit } = require('process'); const fs = require('fs'); const ora = require('ora'); -const { AUTH, CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_FILENAME, DEFAULT_FORMAT, DEFAULT_MIN_HEIGHT, DEFAULT_TENANT, DEFAULT_WIDTH, ENV_VAR, FORMAT, TRANSPORT_TYPE, DEFAULT_EMAIL_SUBJECT, DEFAULT_EMAIL_NOTE, DEFAULT_MULTI_TENANCY, DEFAULT_SELF_SIGNED_CERTIFICATES } = require('./constants.js'); +const { AUTH, CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_FILENAME, DEFAULT_FORMAT, DEFAULT_MIN_HEIGHT, DEFAULT_TENANT, DEFAULT_WIDTH, ENV_VAR, FORMAT, TRANSPORT_TYPE, DEFAULT_EMAIL_SUBJECT, DEFAULT_EMAIL_NOTE, DEFAULT_MULTI_TENANCY, DEFAULT_SELF_SIGNED_CERTIFICATES, DEFAULT_TIMEOUT } = require('./constants.js'); const dotenv = require("dotenv"); dotenv.config(); const spinner = ora(''); @@ -67,7 +67,10 @@ async function getCommandArguments() { .env(ENV_VAR.EMAIL_NOTE)) .addOption(new Option('--selfsignedcerts ', 'enable or disable self-signed certicates for smtp transport') .default(DEFAULT_SELF_SIGNED_CERTIFICATES) - .choices(['true', 'false'])); + .choices(['true', 'false'])) + .addOption(new Option('--timeout ', 'timeout for generating report') + .default(DEFAULT_TIMEOUT) + .env(ENV_VAR.TIMEOUT)); program.addHelpText('after', ` Note: The tenant in the url has the higher priority than tenant value provided as command option.`); @@ -99,6 +102,8 @@ async function getEventArguments(event) { event['multitenancy'] = DEFAULT_MULTI_TENANCY; if (event.selfsignedcerts === undefined) event['selfsignedcerts'] = DEFAULT_SELF_SIGNED_CERTIFICATES; + if (event.timeout === undefined) + event['timeout'] = DEFAULT_TIMEOUT; return getOptions(event); } @@ -127,7 +132,8 @@ function getOptions(options) { time: null, note: null, emailbody: null, - selfsignedcerts: null + selfsignedcerts: null, + timeout: null } // Set url. @@ -222,6 +228,9 @@ function getOptions(options) { // Set self signed certificate flag. commandOptions.selfsignedcerts = options.selfsignedcerts; + // Set timeout. + commandOptions.timeout = options.timeout; + spinner.succeed('Fetched argument values'); return commandOptions; } diff --git a/src/constants.js b/src/constants.js index c427678..9717653 100644 --- a/src/constants.js +++ b/src/constants.js @@ -14,6 +14,7 @@ const DEFAULT_EMAIL_SUBJECT = 'This is an email containing your opensearch dashb const DEFAULT_EMAIL_NOTE = 'Hi,\nHere is the latest report!'; const DEFAULT_MULTI_TENANCY = true; const DEFAULT_SELF_SIGNED_CERTIFICATES = false; +const DEFAULT_TIMEOUT = 300000; const REPORT_TYPE = { DASHBOARD: 'Dashboard', @@ -65,6 +66,7 @@ const ENV_VAR = { SMTP_PASSWORD: 'OPENSEARCH_SMTP_PASSWORD', EMAIL_SUBJECT: 'OPENSEARCH_EMAIL_SUBJECT', EMAIL_NOTE: 'OPENSEARCH_EMAIL_NOTE', + TIMEOUT: 'OPENSEARCH_TIMEOUT' } const TRANSPORT_TYPE = { @@ -74,5 +76,5 @@ const TRANSPORT_TYPE = { module.exports = { CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_TENANT, DEFAULT_FORMAT, DEFAULT_WIDTH, DEFAULT_MIN_HEIGHT, DEFAULT_FILENAME, DEFAULT_EMAIL_SUBJECT, - DEFAULT_EMAIL_NOTE, REPORT_TYPE, SELECTOR, FORMAT, AUTH, URL_SOURCE, ENV_VAR, TRANSPORT_TYPE, DEFAULT_MULTI_TENANCY, DEFAULT_SELF_SIGNED_CERTIFICATES -}; \ No newline at end of file + DEFAULT_EMAIL_NOTE, REPORT_TYPE, SELECTOR, FORMAT, AUTH, URL_SOURCE, ENV_VAR, TRANSPORT_TYPE, DEFAULT_MULTI_TENANCY, DEFAULT_SELF_SIGNED_CERTIFICATES, DEFAULT_TIMEOUT +}; diff --git a/src/download-helpers.js b/src/download-helpers.js index 4ce6dac..d4a5aed 100644 --- a/src/download-helpers.js +++ b/src/download-helpers.js @@ -10,7 +10,7 @@ const exit = require('process'); const ora = require('ora'); const spinner = ora(''); -module.exports = async function downloadReport(url, format, width, height, filename, authType, username, password, tenant, multitenancy, time, transport, emailbody) { +module.exports = async function downloadReport(url, format, width, height, filename, authType, username, password, tenant, multitenancy, time, transport, emailbody, timeout) { spinner.start('Connecting to url ' + url); try { const browser = await puppeteer.launch({ @@ -32,13 +32,12 @@ module.exports = async function downloadReport(url, format, width, height, filen TZ: process.env.TZ || 'UTC', }, }); - const page = await browser.newPage(); const overridePage = await browser.newPage(); page.setDefaultNavigationTimeout(0); - page.setDefaultTimeout(300000); + page.setDefaultTimeout(timeout); overridePage.setDefaultNavigationTimeout(0); - overridePage.setDefaultTimeout(300000); + overridePage.setDefaultTimeout(timeout); // auth if (authType !== undefined && authType !== AUTH.NONE && username !== undefined && password !== undefined) { @@ -111,6 +110,7 @@ module.exports = async function downloadReport(url, format, width, height, filen height: scrollHeight + 'px', printBackground: true, pageRanges: '1', + timeout: 0 }); } else if (format === FORMAT.PNG) { buffer = await page.screenshot({ @@ -155,7 +155,7 @@ module.exports = async function downloadReport(url, format, width, height, filen const waitForDynamicContent = async ( page, - timeout = 30000, + timeout = timeout, interval = 1000, checks = 5 ) => { @@ -257,7 +257,7 @@ const samlAuthentication = async (page, url, username, password, tenant, multite await page.type('[name="identifier"]', username); await page.type('[name="credentials.passcode"]', password); await page.click('[value="Sign in"]') - await page.waitForTimeout(30000); + await page.waitForTimeout(timeout); try { if (multitenancy === true) { if (tenant === 'global' || tenant === 'private') { @@ -291,7 +291,7 @@ const cognitoAuthentication = async (page, overridePage, url, username, password await page.type('[name="username"]', username); await page.type('[name="password"]', password); await page.click('[name="signInSubmitButton"]'); - await page.waitForTimeout(30000); + await page.waitForTimeout(timeout); try { if (multitenancy === true) { if (tenant === 'global' || tenant === 'private') { diff --git a/src/run.js b/src/run.js index e6dc78c..1914628 100755 --- a/src/run.js +++ b/src/run.js @@ -27,7 +27,8 @@ module.exports = async function run(args) { options.multitenancy, options.time, options.transport, - options.emailbody + options.emailbody, + options.timeout ); await sendEmail( diff --git a/test/help.test.js b/test/help.test.js index 419cae7..c9acc8a 100644 --- a/test/help.test.js +++ b/test/help.test.js @@ -34,6 +34,7 @@ Options: --subject email subject (default: "This is an email containing your opensearch dashboard report", env: OPENSEARCH_EMAIL_SUBJECT) --note email body (string or path to text file) (default: "Hi,\\nHere is the latest report!", env: OPENSEARCH_EMAIL_NOTE) --selfsignedcerts enable or disable self-signed certicates for smtp transport (choices: "true", "false", default: false) + --timeout timeout for generating report (default: 300000, env: OPENSEARCH_TIMEOUT) -h, --help display help for command Note: The tenant in the url has the higher priority than tenant value provided as command option. From 8660cdd41aacb14c343c2c2331eeae1f9246f92f Mon Sep 17 00:00:00 2001 From: Rupal Mahajan Date: Thu, 22 Feb 2024 18:29:54 -0800 Subject: [PATCH 2/4] fix tests Signed-off-by: Rupal Mahajan --- src/download-helpers.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/download-helpers.js b/src/download-helpers.js index d4a5aed..df77af4 100644 --- a/src/download-helpers.js +++ b/src/download-helpers.js @@ -94,7 +94,7 @@ module.exports = async function downloadReport(url, format, width, height, filen // force wait for any resize to load after the above DOM modification. await new Promise(resolve => setTimeout(resolve, 2000)); - await waitForDynamicContent(page); + await waitForDynamicContent(page, timeout); let buffer; spinner.text = `Downloading Report...`; @@ -110,7 +110,6 @@ module.exports = async function downloadReport(url, format, width, height, filen height: scrollHeight + 'px', printBackground: true, pageRanges: '1', - timeout: 0 }); } else if (format === FORMAT.PNG) { buffer = await page.screenshot({ From 44720abc3d1f310754741c4eff7d15e1db5b0c5e Mon Sep 17 00:00:00 2001 From: Rupal Mahajan Date: Mon, 26 Feb 2024 13:59:47 -0800 Subject: [PATCH 3/4] Add timeout unit Signed-off-by: Rupal Mahajan --- USER_GUIDE.md | 1 + src/arguments.js | 2 +- test/help.test.js | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 7a83038..f9d55ae 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -41,6 +41,7 @@ Option | Default Value | Valid Options | Environment Variable | Description --subject | This is an email containing your dashboard report | - | OPENSEARCH_SUBJECT | subject for the email --note | Hi,\nHere is the latest report! | string or path to text file | OPENSEARCH_EMAIL_NOTE | The email body --selfsignedcerts | false | true, false | - | enable or disable self-signed certicates for smtp transport +--timeout | 300000ms | - | OPENSEARCH_TIMEOUT | timeout for generating report in ms | - | - | - | CHROMIUM_PATH | path to chromium directory You can also find this information using help command. diff --git a/src/arguments.js b/src/arguments.js index 0099183..516588f 100644 --- a/src/arguments.js +++ b/src/arguments.js @@ -68,7 +68,7 @@ async function getCommandArguments() { .addOption(new Option('--selfsignedcerts ', 'enable or disable self-signed certicates for smtp transport') .default(DEFAULT_SELF_SIGNED_CERTIFICATES) .choices(['true', 'false'])) - .addOption(new Option('--timeout ', 'timeout for generating report') + .addOption(new Option('--timeout ', 'timeout for generating report in ms') .default(DEFAULT_TIMEOUT) .env(ENV_VAR.TIMEOUT)); diff --git a/test/help.test.js b/test/help.test.js index c9acc8a..ad01ada 100644 --- a/test/help.test.js +++ b/test/help.test.js @@ -34,7 +34,7 @@ Options: --subject email subject (default: "This is an email containing your opensearch dashboard report", env: OPENSEARCH_EMAIL_SUBJECT) --note email body (string or path to text file) (default: "Hi,\\nHere is the latest report!", env: OPENSEARCH_EMAIL_NOTE) --selfsignedcerts enable or disable self-signed certicates for smtp transport (choices: "true", "false", default: false) - --timeout timeout for generating report (default: 300000, env: OPENSEARCH_TIMEOUT) + --timeout timeout for generating report in ms (default: 300000, env: OPENSEARCH_TIMEOUT) -h, --help display help for command Note: The tenant in the url has the higher priority than tenant value provided as command option. From 099455056d001079d91d499572574d35b4b64f23 Mon Sep 17 00:00:00 2001 From: Rupal Mahajan Date: Mon, 26 Feb 2024 16:21:19 -0800 Subject: [PATCH 4/4] Update default timeout in user guide Signed-off-by: Rupal Mahajan --- USER_GUIDE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index f9d55ae..64aa70f 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -41,7 +41,7 @@ Option | Default Value | Valid Options | Environment Variable | Description --subject | This is an email containing your dashboard report | - | OPENSEARCH_SUBJECT | subject for the email --note | Hi,\nHere is the latest report! | string or path to text file | OPENSEARCH_EMAIL_NOTE | The email body --selfsignedcerts | false | true, false | - | enable or disable self-signed certicates for smtp transport ---timeout | 300000ms | - | OPENSEARCH_TIMEOUT | timeout for generating report in ms +--timeout | 300000 | - | OPENSEARCH_TIMEOUT | timeout for generating report in ms | - | - | - | CHROMIUM_PATH | path to chromium directory You can also find this information using help command.