X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=6d37271ff6382f06c18711aed2e0852ddae61f0c;hb=bf45eb680705907ff1cbb22a7ab94f39dd000213;hp=9969e2294210777d281f43a3278a1dc150b16aa6;hpb=32a1eb7ab66964e5beb8b79082782761329e0705;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 9969e229..6d37271f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,4 +1,6 @@ +import Configuration from './Configuration'; import { WebSocketCloseEventStatusString } from '../types/WebSocket'; +import { WorkerProcessType } from '../types/Worker'; import { v4 as uuid } from 'uuid'; export default class Utils { @@ -11,7 +13,7 @@ export default class Utils { } static secondsToHHMMSS(seconds: number): string { - return new Date(seconds * 1000).toISOString().substr(11, 8); + return Utils.milliSecondsToHHMMSS(seconds * 1000); } static milliSecondsToHHMMSS(milliSeconds: number): string { @@ -34,7 +36,7 @@ export default class Utils { } } - static convertToDate(value): Date { + static convertToDate(value: any): Date { // Check if (!value) { return value; @@ -46,7 +48,7 @@ export default class Utils { return value; } - static convertToInt(value): number { + static convertToInt(value: any): number { let changedValue = value; if (!value) { return 0; @@ -62,7 +64,7 @@ export default class Utils { return changedValue; } - static convertToFloat(value): number { + static convertToFloat(value: any): number { let changedValue = value; if (!value) { return 0; @@ -75,7 +77,7 @@ export default class Utils { return changedValue; } - static convertToBoolean(value): boolean { + static convertToBoolean(value: any): boolean { let result = false; // Check boolean if (value) { @@ -102,14 +104,14 @@ export default class Utils { return Math.floor(Math.random() * max + 1); } - static roundTo(number: number, scale: number): number { + static roundTo(numberValue: number, scale: number): number { const roundPower = Math.pow(10, scale); - return Math.round(number * roundPower) / roundPower; + return Math.round(numberValue * roundPower) / roundPower; } - static truncTo(number: number, scale: number): number { + static truncTo(numberValue: number, scale: number): number { const truncPower = Math.pow(10, scale); - return Math.trunc(number * truncPower) / truncPower; + return Math.trunc(numberValue * truncPower) / truncPower; } static getRandomFloatRounded(max: number, min = 0, scale = 2): number { @@ -128,14 +130,14 @@ export default class Utils { return JSON.parse(JSON.stringify(object)) as T; } - static isIterable(obj): boolean { + static isIterable(obj: T): boolean { if (obj) { return typeof obj[Symbol.iterator] === 'function'; } return false; } - static isEmptyJSon(document): boolean { + static isEmptyJSon(document: any): boolean { // Empty? if (!document) { return true; @@ -148,15 +150,15 @@ export default class Utils { return Object.keys(document).length === 0; } - static isString(value): boolean { + static isString(value: any): boolean { return typeof value === 'string'; } - static isUndefined(value): boolean { + static isUndefined(value: any): boolean { return typeof value === 'undefined'; } - static isNullOrUndefined(value): boolean { + static isNullOrUndefined(value: any): boolean { // eslint-disable-next-line no-eq-null, eqeqeq if (value == null) { return true; @@ -164,7 +166,7 @@ export default class Utils { return false; } - static isEmptyArray(object): boolean { + static isEmptyArray(object: any): boolean { if (!object) { return true; } @@ -174,15 +176,15 @@ export default class Utils { return true; } - static isEmptyObject(obj): boolean { + static isEmptyObject(obj: any): boolean { return !Object.keys(obj).length; } static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`; /** - * @param {number} [retryNumber=0] - * @return {number} - delay in milliseconds + * @param {number} [retryNumber=0] + * @returns {number} delay in milliseconds */ static exponentialDelay(retryNumber = 0): number { const delay = Math.pow(2, retryNumber) * 100; @@ -190,6 +192,12 @@ export default class Utils { return delay + randomSum; } + /** + * Convert websocket error code to human readable string message + * + * @param {number} code websocket error code + * @returns {string} human readable string message + */ static getWebSocketCloseEventStatusString(code: number): string { if (code >= 0 && code <= 999) { return '(Unused)'; @@ -205,8 +213,16 @@ export default class Utils { } } if (!Utils.isUndefined(WebSocketCloseEventStatusString[code])) { - return WebSocketCloseEventStatusString[code]; + return WebSocketCloseEventStatusString[code] as string; } return '(Unknown)'; } + + static workerPoolInUse(): boolean { + return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes(Configuration.getWorkerProcess()); + } + + static workerDynamicPoolInUse(): boolean { + return Configuration.getWorkerProcess() === WorkerProcessType.DYNAMIC_POOL; + } }