Skip to content

Commit

Permalink
The utils getNewDate and getNewDateTime functions related corrections…
Browse files Browse the repository at this point in the history
…, touch #456.
  • Loading branch information
PaulDalek committed Jul 11, 2024
1 parent 6c48548 commit 6e24a08
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 43 deletions.
12 changes: 4 additions & 8 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 =
Expand Down
10 changes: 5 additions & 5 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -56,7 +56,7 @@ const factory = {
let page = false;

const id = uuid();
const startDate = new Date().getTime();
const startDate = getNewDateTime();

try {
page = await newPage();
Expand All @@ -68,7 +68,7 @@ const factory = {
log(
3,
`[pool] Successfully created a worker ${id} - took ${
new Date().getTime() - startDate
getNewDateTime() - startDate
} ms.`
);
} catch (error) {
Expand Down Expand Up @@ -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}.`);

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions lib/server/routes/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')));

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions tests/cli/cli_test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/cli/cli_test_runner_single.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -70,15 +70,15 @@ 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);

// 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
Expand Down
6 changes: 3 additions & 3 deletions tests/http/http_test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
{
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/http/http_test_runner_single.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
{
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/node/node_test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -109,15 +109,15 @@ 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
);
})
.catch((error) => {
// 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);
Expand Down
8 changes: 4 additions & 4 deletions tests/node/node_test_runner_single.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -94,15 +94,15 @@ 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
);
});
} catch (error) {
// 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
);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/other/side_by_side.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -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);
});
Expand Down
8 changes: 5 additions & 3 deletions tests/other/stress_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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---`);
})
Expand Down

0 comments on commit 6e24a08

Please sign in to comment.