Fix rounding helper
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 24 Nov 2020 09:48:27 +0000 (10:48 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 24 Nov 2020 09:56:39 +0000 (10:56 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/StationWorker.ts
src/charging-station/Worker.ts
src/start.ts
src/utils/Utils.ts

index f54334e952d4cb86e0f50557209e87757c68b2a1..68c3ea24c0f96c4def2f5c187050bd5e816dc04b 100644 (file)
@@ -3,6 +3,6 @@ import { isMainThread, workerData } from 'worker_threads';
 import ChargingStation from './ChargingStation';
 
 if (!isMainThread) {
-  const station = new ChargingStation(workerData.index, workerData.templateFile);
+  const station = new ChargingStation(workerData.index as number, workerData.templateFile as string);
   station.start();
 }
index 93aade5046b44284ee4604e1b17df0e3a9f5d46d..d91942021184855f282892b4c9467a6beee9b5cc 100644 (file)
@@ -1,38 +1,37 @@
 import Configuration from '../utils/Configuration';
 import Pool from 'worker-threads-pool';
 import { Worker } from 'worker_threads';
+import WorkerData from '../types/WorkerData';
 
 export default class Wrk {
-  private _workerData;
-  private _workerScript;
-  private _pool;
+  private _workerScript: string;
+  private _workerData: WorkerData;
+  private _pool: Pool;
   private _concurrentWorkers: number;
 
   /**
    * Create a new `Wrk`.
    *
-   * @param {String} workerScript
-   * @param {Object} workerData
-   * @param {Number} numConcurrentWorkers
+   * @param {string} workerScript
+   * @param {WorkerData} workerData
+   * @param {number} numConcurrentWorkers
    */
-  constructor(workerScript, workerData, numConcurrentWorkers) {
+  constructor(workerScript: string, workerData: WorkerData, numConcurrentWorkers: number) {
     this._workerData = workerData;
     this._workerScript = workerScript;
-    this._numConcurrentWorkers = numConcurrentWorkers;
     if (Configuration.useWorkerPool()) {
+      this._concurrentWorkers = Configuration.getWorkerPoolSize();
       this._pool = new Pool({ max: Configuration.getWorkerPoolSize() });
+    } else {
+      this._concurrentWorkers = numConcurrentWorkers;
     }
   }
 
   /**
-   * @param {Number} numConcurrentWorkers
-   * @private
+   * @return {number}
+   * @public
    */
-  set _numConcurrentWorkers(numConcurrentWorkers: number) {
-    this._concurrentWorkers = numConcurrentWorkers;
-  }
-
-  get _numConcurrentWorkers(): number {
+  public get concurrentWorkers(): number {
     return this._concurrentWorkers;
   }
 
index 1c91dea361f5e5b9224ce6f8d6529e5758d524b4..e841b57ec63692d173ea6f8e9d69703be546e668 100644 (file)
@@ -6,6 +6,7 @@ class Bootstrap {
   static start() {
     try {
       let numStationsTotal = 0;
+      let numConcurrentWorkers = 0;
       // Start each ChargingStation object in a worker thread
       if (Configuration.getStationTemplateURLs()) {
         Configuration.getStationTemplateURLs().forEach((stationURL: StationTemplateURL) => {
@@ -18,6 +19,7 @@ class Bootstrap {
                 templateFile: stationURL.file,
               }, numStationsTotal);
               worker.start().catch(() => {});
+              numConcurrentWorkers = worker.concurrentWorkers;
             }
           } catch (error) {
             // eslint-disable-next-line no-console
@@ -30,7 +32,7 @@ class Bootstrap {
       if (numStationsTotal === 0) {
         console.log('No charging station template enabled in configuration, exiting');
       } else {
-        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s)');
+        console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
       }
     } catch (error) {
       // eslint-disable-next-line no-console
index 4876f46db8c0483b72e0d81e6affd76941673e46..69ccc318143b7f481b0a0f059d5c5aacff1f19dc 100644 (file)
@@ -102,7 +102,8 @@ export default class Utils {
   }
 
   static roundTo(number: number, scale: number): number {
-    return Utils.convertToFloat(number.toFixed(scale));
+    const roundPower = Math.pow(10, scale);
+    return Math.round(number * roundPower) / roundPower;
   }
 
   static getRandomFloatRounded(max: number, min = 0, scale = 2): number {