X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FConfiguration.ts;h=f1f43bcfff188f353c10534199895533a34b25fe;hb=44eb6026079c8dc2c77b10a96a42d0c0b2da7c8f;hp=716e53095b8f7d76a4572751b7d04b9fc428d017;hpb=0d8140bd623b67d6d7fddeda288fd945af0cb168;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 716e5309..f1f43bcf 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,23 +1,27 @@ -import ConfigurationData, { - ServerOptions, - StationTemplateUrl, - StorageConfiguration, - SupervisionUrlDistribution, - UIServerConfiguration, -} from '../types/ConfigurationData'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +import chalk from 'chalk'; +import merge from 'just-merge'; +import { WorkerChoiceStrategies } from 'poolifier'; import Constants from './Constants'; -import { EmptyObject } from '../types/EmptyObject'; +import { + type ConfigurationData, + type StationTemplateUrl, + type StorageConfiguration, + SupervisionUrlDistribution, + type UIServerConfiguration, + type WorkerConfiguration, +} from '../types/ConfigurationData'; +import type { EmptyObject } from '../types/EmptyObject'; +import type { HandleErrorParams } from '../types/Error'; import { FileType } from '../types/FileType'; -import { HandleErrorParams } from '../types/Error'; import { StorageType } from '../types/Storage'; -import type { WorkerChoiceStrategy } from 'poolifier'; -import WorkerConstants from '../worker/WorkerConstants'; +import { ApplicationProtocol } from '../types/UIProtocol'; import { WorkerProcessType } from '../types/Worker'; -import chalk from 'chalk'; -import { fileURLToPath } from 'url'; -import fs from 'fs'; -import path from 'path'; +import WorkerConstants from '../worker/WorkerConstants'; export default class Configuration { private static configurationFile = path.join( @@ -30,6 +34,10 @@ export default class Configuration { private static configuration: ConfigurationData | null = null; private static configurationChangeCallback: () => Promise; + private constructor() { + // This is intentional + } + static setConfigurationChangeCallback(cb: () => Promise): void { Configuration.configurationChangeCallback = cb; } @@ -52,35 +60,20 @@ export default class Configuration { chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration section 'uiWebSocketServer' usage. Use 'uiServer' instead}` ); } - let options: ServerOptions = { - host: Constants.DEFAULT_UI_WEBSOCKET_SERVER_HOST, - port: Constants.DEFAULT_UI_WEBSOCKET_SERVER_PORT, - }; let uiServerConfiguration: UIServerConfiguration = { - enabled: true, - options, + enabled: false, + type: ApplicationProtocol.WS, + options: { + host: Constants.DEFAULT_UI_SERVER_HOST, + port: Constants.DEFAULT_UI_SERVER_PORT, + }, }; if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) { - if (Configuration.objectHasOwnProperty(Configuration.getConfig().uiServer, 'options')) { - options = { - ...options, - ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().uiServer.options, - 'host' - ) && { host: Configuration.getConfig().uiServer.options.host }), - ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().uiServer.options, - 'port' - ) && { port: Configuration.getConfig().uiServer.options.port }), - }; - } - uiServerConfiguration = { - ...uiServerConfiguration, - ...(Configuration.objectHasOwnProperty(Configuration.getConfig().uiServer, 'enabled') && { - enabled: Configuration.getConfig().uiServer.enabled, - }), - options, - }; + uiServerConfiguration = merge(uiServerConfiguration, Configuration.getConfig().uiServer); + } + if (Configuration.isCFEnvironment() === true) { + delete uiServerConfiguration.options.host; + uiServerConfiguration.options.port = parseInt(process.env.PORT); } return uiServerConfiguration; } @@ -95,22 +88,7 @@ export default class Configuration { if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'performanceStorage')) { storageConfiguration = { ...storageConfiguration, - ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().performanceStorage, - 'enabled' - ) && { enabled: Configuration.getConfig().performanceStorage.enabled }), - ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().performanceStorage, - 'type' - ) && { type: Configuration.getConfig().performanceStorage.type }), - ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().performanceStorage, - 'uri' - ) && { - uri: this.getDefaultPerformanceStorageUri( - Configuration.getConfig()?.performanceStorage?.type ?? StorageType.JSON_FILE - ), - }), + ...Configuration.getConfig().performanceStorage, }; } return storageConfiguration; @@ -161,57 +139,90 @@ export default class Configuration { return Configuration.getConfig().stationTemplateUrls; } - static getWorkerProcess(): WorkerProcessType { + static getWorker(): WorkerConfiguration { Configuration.warnDeprecatedConfigurationKey( 'useWorkerPool', null, - "Use 'workerProcess' to define the type of worker process model to use instead" + "Use 'worker' section to define the type of worker process model instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerProcess', + null, + "Use 'worker' section to define the type of worker process model instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerStartDelay', + null, + "Use 'worker' section to define the worker start delay instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'chargingStationsPerWorker', + null, + "Use 'worker' section to define the number of element(s) per worker instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'elementStartDelay', + null, + "Use 'worker' section to define the worker's element start delay instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerPoolMinSize', + null, + "Use 'worker' section to define the worker pool minimum size instead" ); - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') - ? Configuration.getConfig().workerProcess - : WorkerProcessType.WORKER_SET; - } - - static getWorkerStartDelay(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') - ? Configuration.getConfig().workerStartDelay - : WorkerConstants.DEFAULT_WORKER_START_DELAY; - } - - static getElementStartDelay(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay') - ? Configuration.getConfig().elementStartDelay - : WorkerConstants.DEFAULT_ELEMENT_START_DELAY; - } - - static getWorkerPoolMinSize(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize') - ? Configuration.getConfig().workerPoolMinSize - : WorkerConstants.DEFAULT_POOL_MIN_SIZE; - } - - static getWorkerPoolMaxSize(): number { Configuration.warnDeprecatedConfigurationKey( 'workerPoolSize;', null, - "Use 'workerPoolMaxSize' instead" + "Use 'worker' section to define the worker pool maximum size instead" ); - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize') - ? Configuration.getConfig().workerPoolMaxSize - : WorkerConstants.DEFAULT_POOL_MAX_SIZE; - } - - static getWorkerPoolStrategy(): WorkerChoiceStrategy { - return Configuration.getConfig().workerPoolStrategy; - } - - static getChargingStationsPerWorker(): number { - return Configuration.objectHasOwnProperty( - Configuration.getConfig(), - 'chargingStationsPerWorker' - ) - ? Configuration.getConfig().chargingStationsPerWorker - : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + Configuration.warnDeprecatedConfigurationKey( + 'workerPoolMaxSize;', + null, + "Use 'worker' section to define the worker pool maximum size instead" + ); + Configuration.warnDeprecatedConfigurationKey( + 'workerPoolStrategy;', + null, + "Use 'worker' section to define the worker pool strategy instead" + ); + let workerConfiguration: WorkerConfiguration = { + processType: Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') + ? Configuration.getConfig().workerProcess + : WorkerProcessType.WORKER_SET, + startDelay: Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') + ? Configuration.getConfig().workerStartDelay + : WorkerConstants.DEFAULT_WORKER_START_DELAY, + elementsPerWorker: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'chargingStationsPerWorker' + ) + ? Configuration.getConfig().chargingStationsPerWorker + : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, + elementStartDelay: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'elementStartDelay' + ) + ? Configuration.getConfig().elementStartDelay + : WorkerConstants.DEFAULT_ELEMENT_START_DELAY, + poolMinSize: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'workerPoolMinSize' + ) + ? Configuration.getConfig().workerPoolMinSize + : WorkerConstants.DEFAULT_POOL_MIN_SIZE, + poolMaxSize: Configuration.objectHasOwnProperty( + Configuration.getConfig(), + 'workerPoolMaxSize' + ) + ? Configuration.getConfig().workerPoolMaxSize + : WorkerConstants.DEFAULT_POOL_MAX_SIZE, + poolStrategy: + Configuration.getConfig().workerPoolStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN, + }; + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'worker')) { + workerConfiguration = { ...workerConfiguration, ...Configuration.getConfig().worker }; + } + return workerConfiguration; } static getLogConsole(): boolean { @@ -233,10 +244,18 @@ export default class Configuration { : true; } - static getLogMaxFiles(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') - ? Configuration.getConfig().logMaxFiles - : 7; + static getLogMaxFiles(): number | string | undefined { + return ( + Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') && + Configuration.getConfig().logMaxFiles + ); + } + + static getLogMaxSize(): number | string | undefined { + return ( + Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') && + Configuration.getConfig().logMaxSize + ); } static getLogLevel(): string { @@ -309,13 +328,13 @@ export default class Configuration { ) { console.error( chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${ - logMsgToAppend && '. ' + logMsgToAppend + logMsgToAppend && `. ${logMsgToAppend}` }}` ); } else if (!Configuration.isUndefined(Configuration.getConfig()[key])) { console.error( chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage${ - logMsgToAppend && '. ' + logMsgToAppend + logMsgToAppend && `. ${logMsgToAppend}` }}` ); } @@ -366,8 +385,11 @@ export default class Configuration { } } + private static isCFEnvironment(): boolean { + return process.env.VCAP_APPLICATION !== undefined; + } + private static getDefaultPerformanceStorageUri(storageType: StorageType) { - const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; switch (storageType) { case StorageType.JSON_FILE: return `file://${path.join( @@ -377,7 +399,7 @@ export default class Configuration { case StorageType.SQLITE: return `file://${path.join( path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'), - SQLiteFileName + `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` )}`; default: throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`);