From: Jérôme Benoit Date: Fri, 23 Oct 2020 10:42:33 +0000 (+0200) Subject: Make statistics class a singleton. X-Git-Tag: v1.0.1-0~248 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=560bcf5b8e2a406e7de3e7e330da36e93dd6eea9;p=e-mobility-charging-stations-simulator.git Make statistics class a singleton. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.js b/src/charging-station/ChargingStation.js index f5a67fe9..42430593 100644 --- a/src/charging-station/ChargingStation.js +++ b/src/charging-station/ChargingStation.js @@ -105,8 +105,8 @@ class ChargingStation { } } this._stationInfo.powerDivider = this._getPowerDivider(); - // FIXME: Conditionally initialize or use singleton design pattern per charging station - this._statistics = new Statistics(this._stationInfo.name); + this._statistics = Statistics.getInstance(); + this._statistics.objName = this._stationInfo.name; this._performanceObserver = new PerformanceObserver((list) => { const entry = list.getEntries()[0]; this._statistics.logPerformance(entry, 'ChargingStation'); diff --git a/src/utils/Statistics.js b/src/utils/Statistics.js index 743cb0b7..d53cbe45 100644 --- a/src/utils/Statistics.js +++ b/src/utils/Statistics.js @@ -3,11 +3,23 @@ const logger = require('./Logger'); const Utils = require('./Utils'); class Statistics { - constructor(objName) { - this._objName = objName; + static instance; + + constructor() { this._statistics = {}; } + set objName(objName) { + this._objName = objName; + } + + static getInstance() { + if (!Statistics.instance) { + Statistics.instance = new Statistics(); + } + return Statistics.instance; + } + _logPrefix() { return Utils.logPrefix(` ${this._objName} Statistics:`); } diff --git a/src/utils/Utils.js b/src/utils/Utils.js index 78640572..bd8c8036 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -15,18 +15,6 @@ class Utils { return date.toISOString().substr(11, 8); } - static convertToDate(date) { - // Check - if (!date) { - return date; - } - // Check Type - if (!(date instanceof Date)) { - return new Date(date); - } - return date; - } - static isIterable(obj) { if (obj) { return typeof obj[Symbol.iterator] === 'function'; @@ -63,6 +51,18 @@ class Utils { } } + static convertToDate(date) { + // Check + if (!date) { + return date; + } + // Check Type + if (!(date instanceof Date)) { + return new Date(date); + } + return date; + } + static convertToObjectID(id) { let changedID = id; // Check @@ -119,11 +119,29 @@ class Utils { return result; } - static getRandomInt(max, min) { + static getRandomFloat(max, min = 0) { + if (min) { + return Math.random() * (max - min + 1) + min; + } + return Math.random() * max + 1; + } + + static getRandomInt(max, min = 0) { if (min) { - return Math.floor((Math.random() * (max - min + 1)) + min); + return Math.floor(Utils.getRandomFloat(max, min)); } - return Math.floor((Math.random() * max + 1)); + return Math.floor(Utils.getRandomFloat(max)); + } + + static roundTo(number, scale) { + return Utils.convertToFloat(number.toFixed(scale)); + } + + static getRandomFloatRounded(max, min = 0, scale = 2) { + if (min) { + return Utils.roundTo(Utils.getRandomFloat(max, min), scale); + } + return Utils.roundTo(Utils.getRandomFloat(max), scale); } static logPrefix(prefixString = '') { @@ -139,6 +157,10 @@ class Utils { return JSON.parse(JSON.stringify(jsonDocument)); } + static isString(value) { + return typeof value === 'string'; + } + static isUndefined(value) { return typeof value === 'undefined'; }