feat: allow to provision number of stations by template
[e-mobility-charging-stations-simulator.git] / src / charging-station / SharedLRUCache.ts
index ba0a47d59b70962ab75517aab0215dda6b0b3424..0da4558d4d46f4c4bd3d98f92eb2a7677856517d 100644 (file)
-import LRUCache from 'mnemonist/lru-map-with-delete.js';
+import { LRUMapWithDelete as LRUCache } from 'mnemonist'
+import { isEmpty } from 'rambda'
 
-import { Bootstrap } from './Bootstrap';
-import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types';
-import { Utils } from '../utils';
+import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js'
+import { isNotEmptyArray, isNotEmptyString } from '../utils/index.js'
+import { Bootstrap } from './Bootstrap.js'
 
 enum CacheType {
   chargingStationTemplate = 'chargingStationTemplate',
-  chargingStationConfiguration = 'chargingStationConfiguration',
+  chargingStationConfiguration = 'chargingStationConfiguration'
 }
 
-type CacheValueType = ChargingStationTemplate | ChargingStationConfiguration;
+type CacheValueType = ChargingStationTemplate | ChargingStationConfiguration
 
 export class SharedLRUCache {
-  private static instance: SharedLRUCache | null = null;
-  private readonly lruCache: LRUCache<string, CacheValueType>;
+  private static instance: SharedLRUCache | null = null
+  private readonly lruCache: LRUCache<string, CacheValueType>
 
-  private constructor() {
+  private constructor () {
     this.lruCache = new LRUCache<string, CacheValueType>(
       Bootstrap.getInstance().numberOfChargingStationTemplates +
-        Bootstrap.getInstance().numberOfChargingStations
-    );
+        Bootstrap.getInstance().numberOfConfiguredChargingStations +
+        Bootstrap.getInstance().numberOfProvisionedChargingStations
+    )
   }
 
-  public static getInstance(): SharedLRUCache {
+  public static getInstance (): SharedLRUCache {
     if (SharedLRUCache.instance === null) {
-      SharedLRUCache.instance = new SharedLRUCache();
+      SharedLRUCache.instance = new SharedLRUCache()
     }
-    return SharedLRUCache.instance;
+    return SharedLRUCache.instance
   }
 
-  public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean {
-    return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
+  public hasChargingStationConfiguration (chargingStationConfigurationHash: string): boolean {
+    return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash))
   }
 
-  public setChargingStationConfiguration(
+  public setChargingStationConfiguration (
     chargingStationConfiguration: ChargingStationConfiguration
   ): void {
     if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) {
       this.set(
-        this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash),
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!),
         chargingStationConfiguration
-      );
+      )
     }
   }
 
-  public getChargingStationConfiguration(
+  public getChargingStationConfiguration (
     chargingStationConfigurationHash: string
   ): ChargingStationConfiguration {
     return this.get(
       this.getChargingStationConfigurationKey(chargingStationConfigurationHash)
-    ) as ChargingStationConfiguration;
+    ) as ChargingStationConfiguration
   }
 
-  public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void {
-    this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
+  public deleteChargingStationConfiguration (chargingStationConfigurationHash: string): void {
+    this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash))
   }
 
-  public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean {
-    return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash));
+  public hasChargingStationTemplate (chargingStationTemplateHash: string): boolean {
+    return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash))
   }
 
-  public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void {
+  public setChargingStationTemplate (chargingStationTemplate: ChargingStationTemplate): void {
     this.set(
-      this.getChargingStationTemplateKey(chargingStationTemplate.templateHash),
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!),
       chargingStationTemplate
-    );
+    )
   }
 
-  public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate {
+  public getChargingStationTemplate (chargingStationTemplateHash: string): ChargingStationTemplate {
     return this.get(
       this.getChargingStationTemplateKey(chargingStationTemplateHash)
-    ) as ChargingStationTemplate;
+    ) as ChargingStationTemplate
   }
 
-  public deleteChargingStationTemplate(chargingStationTemplateHash: string): void {
-    this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash));
+  public deleteChargingStationTemplate (chargingStationTemplateHash: string): void {
+    this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash))
   }
 
-  public clear(): void {
-    this.lruCache.clear();
+  public clear (): void {
+    this.lruCache.clear()
   }
 
-  private getChargingStationConfigurationKey(hash: string): string {
-    return `${CacheType.chargingStationConfiguration}${hash}`;
+  private getChargingStationConfigurationKey (hash: string): string {
+    return `${CacheType.chargingStationConfiguration}${hash}`
   }
 
-  private getChargingStationTemplateKey(hash: string): string {
-    return `${CacheType.chargingStationTemplate}${hash}`;
+  private getChargingStationTemplateKey (hash: string): string {
+    return `${CacheType.chargingStationTemplate}${hash}`
   }
 
-  private has(key: string): boolean {
-    return this.lruCache.has(key);
+  private has (key: string): boolean {
+    return this.lruCache.has(key)
   }
 
-  private get(key: string): CacheValueType | undefined {
-    return this.lruCache.get(key);
+  private get (key: string): CacheValueType | undefined {
+    return this.lruCache.get(key)
   }
 
-  private set(key: string, value: CacheValueType): void {
-    this.lruCache.set(key, value);
+  private set (key: string, value: CacheValueType): void {
+    this.lruCache.set(key, value)
   }
 
-  private delete(key: string): void {
-    this.lruCache.delete(key);
+  private delete (key: string): void {
+    this.lruCache.delete(key)
   }
 
-  private isChargingStationConfigurationCacheable(
+  private isChargingStationConfigurationCacheable (
     chargingStationConfiguration: ChargingStationConfiguration
   ): boolean {
     return (
-      Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false &&
-      Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false &&
-      Utils.isNullOrUndefined(chargingStationConfiguration?.automaticTransactionGenerator) ===
-        false &&
-      Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false &&
-      Utils.isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true &&
-      Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false &&
-      Utils.isEmptyObject(chargingStationConfiguration?.automaticTransactionGenerator) === false &&
-      Utils.isNotEmptyString(chargingStationConfiguration?.configurationHash) === true
-    );
+      chargingStationConfiguration.configurationKey != null &&
+      chargingStationConfiguration.stationInfo != null &&
+      chargingStationConfiguration.automaticTransactionGenerator != null &&
+      chargingStationConfiguration.configurationHash != null &&
+      isNotEmptyArray(chargingStationConfiguration.configurationKey) &&
+      !isEmpty(chargingStationConfiguration.stationInfo) &&
+      !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) &&
+      isNotEmptyString(chargingStationConfiguration.configurationHash)
+    )
   }
 }