fix: ensure inflight requests id cannot be duplicated
[e-mobility-charging-stations-simulator.git] / src / utils / MessageChannelUtils.ts
index 2fb952560b49e25b82cfd50fff951cbfe27bcccf..f9d1e14980017606a7c1f64ec0135eb47bf8e80b 100644 (file)
@@ -1,11 +1,12 @@
-import { clone } from 'rambda'
+import { CircularBuffer } from 'mnemonist'
 
 import type { ChargingStation } from '../charging-station/index.js'
 import {
   type ChargingStationData,
   type ChargingStationWorkerMessage,
   ChargingStationWorkerMessageEvents,
-  type Statistics
+  type Statistics,
+  type TimestampedData
 } from '../types/index.js'
 import {
   buildChargingStationAutomaticTransactionGeneratorConfiguration,
@@ -62,10 +63,22 @@ export const buildUpdatedMessage = (
 export const buildPerformanceStatisticsMessage = (
   statistics: Statistics
 ): ChargingStationWorkerMessage<Statistics> => {
+  const statisticsData = [...statistics.statisticsData].map(([key, value]) => {
+    if (value.measurementTimeSeries instanceof CircularBuffer) {
+      value.measurementTimeSeries = value.measurementTimeSeries.toArray() as TimestampedData[]
+    }
+    return [key, value]
+  })
   return {
     event: ChargingStationWorkerMessageEvents.performanceStatistics,
-    // FIXME: CircularBuffer is not structured-cloneable, rambda clone strips the whole statisticsData Map
-    data: clone(statistics)
+    data: {
+      id: statistics.id,
+      name: statistics.name,
+      uri: statistics.uri,
+      createdAt: statistics.createdAt,
+      updatedAt: statistics.updatedAt,
+      statisticsData
+    }
   }
 }