X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils%2FUtils.ts;h=f453c8220fcb7a24c182ba602efe95dbdaff6ea3;hb=6f9f188b8335f5cc9fcb0fe9ba2e72525b70231a;hp=a1f28155f26b21460cf43beb141be2b34ba513d4;hpb=032d6efcb5be418f04d38c39533e7d73d0337195;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index a1f28155..f453c822 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,6 +1,13 @@ +import Configuration from './Configuration'; +import { WebSocketCloseEventStatusString } from '../types/WebSocket'; +import { WorkerProcessType } from '../types/Worker'; import { v4 as uuid } from 'uuid'; export default class Utils { + static logPrefix(prefixString = ''): string { + return new Date().toLocaleString() + prefixString; + } + static generateUUID(): string { return uuid(); } @@ -10,14 +17,14 @@ 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 { return new Date(milliSeconds).toISOString().substr(11, 8); } - static removeExtraEmptyLines(tab): void { + static removeExtraEmptyLines(tab: string[]): void { // Start from the end for (let i = tab.length - 1; i > 0; i--) { // Two consecutive empty lines? @@ -33,7 +40,7 @@ export default class Utils { } } - static convertToDate(value): Date { + static convertToDate(value: any): Date { // Check if (!value) { return value; @@ -45,36 +52,36 @@ export default class Utils { return value; } - static convertToInt(value): number { - let changedValue = value; + static convertToInt(value: any): number { + let changedValue: number = value; if (!value) { return 0; } if (Number.isSafeInteger(value)) { - return value; + return changedValue; } // Check - if (typeof value === 'string') { + if (Utils.isString(value)) { // Create Object changedValue = parseInt(value); } return changedValue; } - static convertToFloat(value): number { - let changedValue = value; + static convertToFloat(value: any): number { + let changedValue: number = value; if (!value) { return 0; } // Check - if (typeof value === 'string') { + if (Utils.isString(value)) { // Create Object changedValue = parseFloat(value); } return changedValue; } - static convertToBoolean(value): boolean { + static convertToBoolean(value: any): boolean { let result = false; // Check boolean if (value) { @@ -101,8 +108,14 @@ export default class Utils { return Math.floor(Math.random() * max + 1); } - static roundTo(number: number, scale: number): number { - return Utils.convertToFloat(number.toFixed(scale)); + static roundTo(numberValue: number, scale: number): number { + const roundPower = Math.pow(10, scale); + return Math.round(numberValue * roundPower) / roundPower; + } + + static truncTo(numberValue: number, scale: number): number { + const truncPower = Math.pow(10, scale); + return Math.trunc(numberValue * truncPower) / truncPower; } static getRandomFloatRounded(max: number, min = 0, scale = 2): number { @@ -112,27 +125,26 @@ export default class Utils { return Utils.roundTo(Utils.getRandomFloat(max), scale); } - static logPrefix(prefixString = ''): string { - const date = new Date(); - return date.toLocaleString() + prefixString; - } - - static objectHasOwnProperty(object, property): boolean { - return Object.prototype.hasOwnProperty.call(object, property); + static getRandomFloatFluctuatedRounded(staticValue: number, fluctuationPercent: number, scale = 2): number { + if (fluctuationPercent === 0) { + return Utils.roundTo(staticValue, scale); + } + const fluctuationRatio = fluctuationPercent / 100; + return Utils.getRandomFloatRounded(staticValue * (1 + fluctuationRatio), staticValue * (1 - fluctuationRatio), scale); } - static cloneObject(object) { - return JSON.parse(JSON.stringify(object)); + static cloneObject(object: T): T { + 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; @@ -145,23 +157,23 @@ 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 { - // eslint-disable-next-line no-eq-null + static isNullOrUndefined(value: any): boolean { + // eslint-disable-next-line no-eq-null, eqeqeq if (value == null) { return true; } return false; } - static isEmptyArray(object): boolean { + static isEmptyArray(object: any): boolean { if (!object) { return true; } @@ -171,19 +183,53 @@ 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; const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay 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)'; + } else if (code >= 1016) { + if (code <= 1999) { + return '(For WebSocket standard)'; + } else if (code <= 2999) { + return '(For WebSocket extensions)'; + } else if (code <= 3999) { + return '(For libraries and frameworks)'; + } else if (code <= 4999) { + return '(For applications)'; + } + } + if (!Utils.isUndefined(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; + } }