From 6e24a082571686f21c93f4706f446224d789faf5 Mon Sep 17 00:00:00 2001 From: PaulDalek Date: Thu, 11 Jul 2024 17:44:10 +0200 Subject: [PATCH] The utils getNewDate and getNewDateTime functions related corrections, touch #456. --- lib/logger.js | 12 ++++-------- lib/pool.js | 10 +++++----- lib/server/routes/health.js | 7 +++---- tests/cli/cli_test_runner.js | 6 +++--- tests/cli/cli_test_runner_single.js | 6 +++--- tests/http/http_test_runner.js | 6 +++--- tests/http/http_test_runner_single.js | 6 +++--- tests/node/node_test_runner.js | 8 ++++---- tests/node/node_test_runner_single.js | 8 ++++---- tests/other/side_by_side.js | 6 +++--- tests/other/stress_test.js | 8 +++++--- 11 files changed, 40 insertions(+), 43 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index a6b96d07..d5a35b8c 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -15,6 +15,8 @@ See LICENSE file in root for details. import { appendFile, existsSync, mkdirSync } from 'fs'; import { join } from 'path'; +import { getNewDate } from './utils.js'; + // The available colors const colors = ['red', 'yellow', 'blue', 'gray', 'green']; @@ -108,11 +110,8 @@ export const log = (...args) => { return; } - // Get rid of the GMT text information - const newDate = new Date().toString().split('(')[0].trim(); - // Create a message's prefix - const prefix = `${newDate} [${levelsDesc[newLevel - 1].title}] -`; + const prefix = `${getNewDate()} [${levelsDesc[newLevel - 1].title}] -`; // Call available log listeners logging.listeners.forEach((fn) => { @@ -154,11 +153,8 @@ export const logWithStack = (newLevel, error, customMessage) => { return; } - // Get rid of the GMT text information - const newDate = new Date().toString().split('(')[0].trim(); - // Create a message's prefix - const prefix = `${newDate} [${levelsDesc[newLevel - 1].title}] -`; + const prefix = `${getNewDate()} [${levelsDesc[newLevel - 1].title}] -`; // If the customMessage exists, we want to display the whole stack message const stackMessage = diff --git a/lib/pool.js b/lib/pool.js index 3362b745..496853f3 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -23,7 +23,7 @@ import { } from './browser.js'; import puppeteerExport from './export.js'; import { log, logWithStack } from './logger.js'; -import { measureTime } from './utils.js'; +import { getNewDateTime, measureTime } from './utils.js'; import ExportError from './errors/ExportError.js'; @@ -56,7 +56,7 @@ const factory = { let page = false; const id = uuid(); - const startDate = new Date().getTime(); + const startDate = getNewDateTime(); try { page = await newPage(); @@ -68,7 +68,7 @@ const factory = { log( 3, `[pool] Successfully created a worker ${id} - took ${ - new Date().getTime() - startDate + getNewDateTime() - startDate } ms.` ); } catch (error) { @@ -298,7 +298,7 @@ export const postWork = async (chart, options) => { } // Save the start time - let workStart = new Date().getTime(); + let workStart = getNewDateTime(); log(4, `[pool] Starting work on pool entry with ID ${workerHandle.id}.`); @@ -337,7 +337,7 @@ export const postWork = async (chart, options) => { // Used for statistics in averageTime and processedWorkCount, which // in turn is used by the /health route. - const workEnd = new Date().getTime(); + const workEnd = getNewDateTime(); const exportTime = workEnd - workStart; stats.timeSpent += exportTime; stats.spentAverage = stats.timeSpent / ++stats.performedExports; diff --git a/lib/server/routes/health.js b/lib/server/routes/health.js index bf831904..5f74cbb6 100644 --- a/lib/server/routes/health.js +++ b/lib/server/routes/health.js @@ -19,7 +19,7 @@ import { log } from '../../logger.js'; import { version } from '../../cache.js'; import { addTimer } from '../../timers.js'; import { getStats, getPoolInfoJSON } from '../../pool.js'; -import { __dirname } from '../../utils.js'; +import { __dirname, getNewDateTime } from '../../utils.js'; const packageFile = JSON.parse(readFileSync(pather(__dirname, 'package.json'))); @@ -84,9 +84,8 @@ export default function addHealthRoutes(app) { status: 'OK', bootTime: serverStartTime, uptime: - Math.floor( - (new Date().getTime() - serverStartTime.getTime()) / 1000 / 60 - ) + ' minutes', + Math.floor((getNewDateTime() - serverStartTime.getTime()) / 1000 / 60) + + ' minutes', version: packageFile.version, highchartsVersion: version(), averageProcessingTime: stats.spentAverage, diff --git a/tests/cli/cli_test_runner.js b/tests/cli/cli_test_runner.js index 5bd02a4c..6a27a589 100644 --- a/tests/cli/cli_test_runner.js +++ b/tests/cli/cli_test_runner.js @@ -19,7 +19,7 @@ import { promisify } from 'util'; import 'colors'; -import { __dirname } from '../../lib/utils.js'; +import { __dirname, getNewDateTime } from '../../lib/utils.js'; // Convert from callback to promise const spawn = promisify(exec); @@ -81,7 +81,7 @@ for (const file of files.filter((file) => file.endsWith('.json'))) { cliCommand = cliCommand.join(' '); // The start date of a CLI command - const startDate = new Date().getTime(); + const startDate = getNewDateTime(); let didFail = false; try { @@ -94,7 +94,7 @@ for (const file of files.filter((file) => file.endsWith('.json'))) { testCounter++; const endMessage = `CLI command from file: ${file}, took ${ - new Date().getTime() - startDate + getNewDateTime() - startDate }ms.`; console.log( diff --git a/tests/cli/cli_test_runner_single.js b/tests/cli/cli_test_runner_single.js index 985f6ea8..4a06d863 100644 --- a/tests/cli/cli_test_runner_single.js +++ b/tests/cli/cli_test_runner_single.js @@ -18,7 +18,7 @@ import { basename, join } from 'path'; import 'colors'; -import { __dirname } from '../../lib/utils.js'; +import { __dirname, getNewDateTime } from '../../lib/utils.js'; // Test runner message console.log( @@ -70,7 +70,7 @@ if (existsSync(file) && file.endsWith('.json')) { cliCommand = cliCommand.join(' '); // The start date of a CLI command - const startDate = new Date().getTime(); + const startDate = getNewDateTime(); // Launch command in a new process spawn(cliCommand); @@ -78,7 +78,7 @@ if (existsSync(file) && file.endsWith('.json')) { // Close event for a process process.on('exit', (code) => { const endMessage = `CLI command from file: ${file}, took ${ - new Date().getTime() - startDate + getNewDateTime() - startDate }ms.`; // If code is 1, it means that export server thrown an error diff --git a/tests/http/http_test_runner.js b/tests/http/http_test_runner.js index fbb0cec0..ff11389a 100644 --- a/tests/http/http_test_runner.js +++ b/tests/http/http_test_runner.js @@ -25,7 +25,7 @@ import { join } from 'path'; import 'colors'; import { fetch } from '../../lib/fetch.js'; -import { __dirname, clearText } from '../../lib/utils.js'; +import { __dirname, clearText, getNewDateTime } from '../../lib/utils.js'; // Test runner message console.log( @@ -87,7 +87,7 @@ fetch(`${url}/health`) return new Promise((resolve) => { // The start date of a POST request - const startDate = new Date().getTime(); + const startDate = getNewDateTime(); const request = http.request( url, { @@ -110,7 +110,7 @@ fetch(`${url}/health`) fileStream.end(); const endMessage = `HTTP request with a payload from file: ${file}, took ${ - new Date().getTime() - startDate + getNewDateTime() - startDate }ms.`; // Based on received status code check if requests failed diff --git a/tests/http/http_test_runner_single.js b/tests/http/http_test_runner_single.js index 12241029..8e6fc4e9 100644 --- a/tests/http/http_test_runner_single.js +++ b/tests/http/http_test_runner_single.js @@ -19,7 +19,7 @@ import { basename, join } from 'path'; import 'colors'; import { fetch } from '../../lib/fetch.js'; -import { __dirname, clearText } from '../../lib/utils.js'; +import { __dirname, clearText, getNewDateTime } from '../../lib/utils.js'; // Test runner message console.log( @@ -64,7 +64,7 @@ fetch(`${url}/health`) ); // The start date of a POST request - const startDate = new Date().getTime(); + const startDate = getNewDateTime(); const request = http.request( url, { @@ -87,7 +87,7 @@ fetch(`${url}/health`) fileStream.end(); const endMessage = `HTTP request with a payload from file: ${file}, took ${ - new Date().getTime() - startDate + getNewDateTime() - startDate }ms.`; // Based on received status code check if requests failed diff --git a/tests/node/node_test_runner.js b/tests/node/node_test_runner.js index 9119aa70..27aaf0cc 100644 --- a/tests/node/node_test_runner.js +++ b/tests/node/node_test_runner.js @@ -24,7 +24,7 @@ import { basename, join } from 'path'; import 'colors'; import exporter from '../../lib/index.js'; -import { __dirname } from '../../lib/utils.js'; +import { __dirname, getNewDateTime } from '../../lib/utils.js'; console.log( 'Highcharts Export Server Node Test Runner'.yellow.bold.underline, @@ -88,7 +88,7 @@ console.log( ); // The start date of a startExport function run - const startTime = new Date().getTime(); + const startTime = getNewDateTime(); // Start the export process exporter @@ -109,7 +109,7 @@ console.log( // Information about the results and the time it took console.log( `[Success] Node module from file: ${file}, took: ${ - new Date().getTime() - startTime + getNewDateTime() - startTime }ms.`.green ); }) @@ -117,7 +117,7 @@ console.log( // Information about the error and the time it took console.log( `[Fail] Node module from file: ${file}, took: ${ - new Date().getTime() - startTime + getNewDateTime() - startTime }ms.`.red ); exporter.setLogLevel(1); diff --git a/tests/node/node_test_runner_single.js b/tests/node/node_test_runner_single.js index af7b1ae2..b1e40d9b 100644 --- a/tests/node/node_test_runner_single.js +++ b/tests/node/node_test_runner_single.js @@ -18,7 +18,7 @@ import { basename, join } from 'path'; import 'colors'; import exporter from '../../lib/index.js'; -import { __dirname } from '../../lib/utils.js'; +import { __dirname, getNewDateTime } from '../../lib/utils.js'; console.log( 'Highcharts Export Server Node Test Runner'.yellow.bold.underline, @@ -73,7 +73,7 @@ console.log( ); // The start date of a startExport function run - const startTime = new Date().getTime(); + const startTime = getNewDateTime(); try { // Start the export process @@ -94,7 +94,7 @@ console.log( // Information about the results and the time it took console.log( `[Success] Node module from file: ${file}, took: ${ - new Date().getTime() - startTime + getNewDateTime() - startTime }ms.`.green ); }); @@ -102,7 +102,7 @@ console.log( // Information about the error and the time it took console.log( `[Fail] Node module from file: ${file}, took: ${ - new Date().getTime() - startTime + getNewDateTime() - startTime }ms.`.red ); } diff --git a/tests/other/side_by_side.js b/tests/other/side_by_side.js index 31f08ece..763b3529 100644 --- a/tests/other/side_by_side.js +++ b/tests/other/side_by_side.js @@ -19,7 +19,7 @@ import { join } from 'path'; import 'colors'; -import { __dirname } from '../../lib/utils.js'; +import { __dirname, getNewDateTime } from '../../lib/utils.js'; // Results paths const resultsPath = join(__dirname, 'tests', 'other', '_results'); @@ -93,7 +93,7 @@ try { ].join(' '); // The start date of a POST request - const startDate = new Date().getTime(); + const startDate = getNewDateTime(); // Launch command in a new process // eslint-disable-next-line no-global-assign @@ -103,7 +103,7 @@ try { process.on('close', () => { const message = `Done with ${ index ? '[PhantomJS]' : '[Puppeteer]' - } ${type} export, took ${new Date().getTime() - startDate}ms.`; + } ${type} export, took ${getNewDateTime() - startDate}ms.`; console.log(index ? message.blue : message.green); }); diff --git a/tests/other/stress_test.js b/tests/other/stress_test.js index 26bd1841..d10f788f 100644 --- a/tests/other/stress_test.js +++ b/tests/other/stress_test.js @@ -12,9 +12,11 @@ See LICENSE file in root for details. *******************************************************************************/ -import { fetch, post } from '../../lib/fetch.js'; import 'colors'; +import { fetch, post } from '../../lib/fetch.js'; +import { getNewDateTime } from '../../lib/utils.js'; + // Test message console.log( 'Highcharts Export Server stress test'.yellow, @@ -45,12 +47,12 @@ const interval = 150; const stressTest = () => { for (let i = 1; i <= requestsNumber; i++) { - const startTime = new Date().getTime(); + const startTime = getNewDateTime(); // Perform a request post(url, requestBody) .then(async (res) => { - const postTime = new Date().getTime() - startTime; + const postTime = getNewDateTime() - startTime; console.log(`${i} request is done, took ${postTime}ms`); console.log(`---\n${res.text}\n---`); })