fix: fix performance statistics storage
[e-mobility-charging-stations-simulator.git] / src / utils / MessageChannelUtils.ts
index 0306d60edd743e037f02221e9587065074541265..bd7036aca60c851e9a25075f196ff0224bf019cf 100644 (file)
@@ -1,11 +1,12 @@
+import type { CircularBuffer } from 'mnemonist'
+
 import type { ChargingStation } from '../charging-station/index.js'
 import {
   type ChargingStationData,
   type ChargingStationWorkerMessage,
   ChargingStationWorkerMessageEvents,
-  type InternalTemplateStatistics,
   type Statistics,
-  type TemplateStatistics
+  type TimestampedData
 } from '../types/index.js'
 import {
   buildChargingStationAutomaticTransactionGeneratorConfiguration,
@@ -13,7 +14,6 @@ import {
   buildEvsesStatus,
   OutputFormat
 } from './ChargingStationConfigurationUtils.js'
-import { clone } from './Utils.js'
 
 export const buildAddedMessage = (
   chargingStation: ChargingStation
@@ -63,15 +63,26 @@ export const buildUpdatedMessage = (
 export const buildPerformanceStatisticsMessage = (
   statistics: Statistics
 ): ChargingStationWorkerMessage<Statistics> => {
+  const statisticsData = [...statistics.statisticsData].map(([key, value]) => {
+    value.measurementTimeSeries = (
+      value.measurementTimeSeries as CircularBuffer<TimestampedData>
+    ).toArray() as TimestampedData[]
+    return [key, value]
+  })
   return {
     event: ChargingStationWorkerMessageEvents.performanceStatistics,
-    data: statistics
+    data: {
+      id: statistics.id,
+      name: statistics.name,
+      uri: statistics.uri,
+      createdAt: statistics.createdAt,
+      updatedAt: statistics.updatedAt,
+      statisticsData
+    }
   }
 }
 
-export const buildChargingStationDataPayload = (
-  chargingStation: ChargingStation
-): ChargingStationData => {
+const buildChargingStationDataPayload = (chargingStation: ChargingStation): ChargingStationData => {
   return {
     started: chargingStation.started,
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -89,13 +100,3 @@ export const buildChargingStationDataPayload = (
     })
   }
 }
-
-export const buildTemplateStatisticsPayload = (
-  map: Map<string, InternalTemplateStatistics>
-): Record<string, TemplateStatistics> => {
-  map = clone(map)
-  for (const value of map.values()) {
-    (value as unknown as TemplateStatistics).indexes = [...value.indexes]
-  }
-  return Object.fromEntries(map.entries() as unknown as Array<[string, TemplateStatistics]>)
-}