X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Futils%2FConfiguration.ts;h=3fec05a882ba2803339307c83a9edbcf625ea9f2;hb=cf2a5d9b1e3f929ef9805d3fb7e4022cf4f9632d;hp=0120fdbaadb102855f8d289bd0d6d155b6546911;hpb=3fa0f0edb5e3bb9fff2b343fb4ad7fc6c7c8df34;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 0120fdba..3fec05a8 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,25 +1,27 @@ import ConfigurationData, { + ServerOptions, StationTemplateUrl, StorageConfiguration, SupervisionUrlDistribution, - UIWebSocketServerConfiguration, + UIServerConfiguration, + WorkerConfiguration, } from '../types/ConfigurationData'; import Constants from './Constants'; import { EmptyObject } from '../types/EmptyObject'; +import { FileType } from '../types/FileType'; import { HandleErrorParams } from '../types/Error'; -import { ServerOptions } from 'ws'; import { StorageType } from '../types/Storage'; -import type { WorkerChoiceStrategy } from 'poolifier'; import WorkerConstants from '../worker/WorkerConstants'; import { WorkerProcessType } from '../types/Worker'; import chalk from 'chalk'; +import { fileURLToPath } from 'url'; import fs from 'fs'; import path from 'path'; export default class Configuration { - private static configurationFilePath = path.join( - path.resolve(__dirname, '../'), + private static configurationFile = path.join( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'assets', 'config.json' ); @@ -41,44 +43,46 @@ export default class Configuration { // Read conf return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logStatisticsInterval') ? Configuration.getConfig().logStatisticsInterval - : 60; + : Constants.DEFAULT_LOG_STATISTICS_INTERVAL; } - static getUIWebSocketServer(): UIWebSocketServerConfiguration { + static getUIServer(): UIServerConfiguration { + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) { + console.error( + 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 uiWebSocketServerConfiguration: UIWebSocketServerConfiguration = { + let uiServerConfiguration: UIServerConfiguration = { enabled: true, options, }; - if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) { - if ( - Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer, 'options') - ) { + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) { + if (Configuration.objectHasOwnProperty(Configuration.getConfig().uiServer, 'options')) { options = { ...options, ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().uiWebSocketServer.options, + Configuration.getConfig().uiServer.options, 'host' - ) && { host: Configuration.getConfig().uiWebSocketServer.options.host }), + ) && { host: Configuration.getConfig().uiServer.options.host }), ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().uiWebSocketServer.options, + Configuration.getConfig().uiServer.options, 'port' - ) && { port: Configuration.getConfig().uiWebSocketServer.options.port }), + ) && { port: Configuration.getConfig().uiServer.options.port }), }; } - uiWebSocketServerConfiguration = { - ...uiWebSocketServerConfiguration, - ...(Configuration.objectHasOwnProperty( - Configuration.getConfig().uiWebSocketServer, - 'enabled' - ) && { enabled: Configuration.getConfig().uiWebSocketServer.enabled }), + uiServerConfiguration = { + ...uiServerConfiguration, + ...(Configuration.objectHasOwnProperty(Configuration.getConfig().uiServer, 'enabled') && { + enabled: Configuration.getConfig().uiServer.enabled, + }), options, }; } - return uiWebSocketServerConfiguration; + return uiServerConfiguration; } static getPerformanceStorage(): StorageConfiguration { @@ -157,57 +161,89 @@ export default class Configuration { return Configuration.getConfig().stationTemplateUrls; } - static getWorkerProcess(): WorkerProcessType { + static getWorker(): WorkerConfiguration { Configuration.warnDeprecatedConfigurationKey( - 'useWorkerPool;', + 'useWorkerPool', null, - "Use 'workerProcess' to define the type of worker process 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" + ); + const 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, + }; + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'worker')) { + return { ...workerConfiguration, ...Configuration.getConfig().worker }; + } + return workerConfiguration; } static getLogConsole(): boolean { @@ -296,12 +332,12 @@ export default class Configuration { sectionName?: string, logMsgToAppend = '' ) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if ( sectionName && !Configuration.isUndefined(Configuration.getConfig()[sectionName]) && - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - !Configuration.isUndefined(Configuration.getConfig()[sectionName][key]) + !Configuration.isUndefined( + (Configuration.getConfig()[sectionName] as Record)[key] + ) ) { console.error( chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${ @@ -322,13 +358,13 @@ export default class Configuration { if (!Configuration.configuration) { try { Configuration.configuration = JSON.parse( - fs.readFileSync(Configuration.configurationFilePath, 'utf8') + fs.readFileSync(Configuration.configurationFile, 'utf8') ) as ConfigurationData; } catch (error) { Configuration.handleFileException( Configuration.logPrefix(), - 'Configuration', - Configuration.configurationFilePath, + FileType.Configuration, + Configuration.configurationFile, error as NodeJS.ErrnoException ); } @@ -341,7 +377,7 @@ export default class Configuration { private static getConfigurationFileWatcher(): fs.FSWatcher { try { - return fs.watch(Configuration.configurationFilePath, (event, filename): void => { + return fs.watch(Configuration.configurationFile, (event, filename): void => { if (filename && event === 'change') { // Nullify to force configuration file reading Configuration.configuration = null; @@ -355,9 +391,9 @@ export default class Configuration { } catch (error) { Configuration.handleFileException( Configuration.logPrefix(), - 'Configuration', - Configuration.configurationFilePath, - error as Error + FileType.Configuration, + Configuration.configurationFile, + error as NodeJS.ErrnoException ); } } @@ -367,11 +403,14 @@ export default class Configuration { switch (storageType) { case StorageType.JSON_FILE: return `file://${path.join( - path.resolve(__dirname, '../../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME )}`; case StorageType.SQLITE: - return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`; + return `file://${path.join( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'), + SQLiteFileName + )}`; default: throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`); } @@ -387,7 +426,7 @@ export default class Configuration { private static handleFileException( logPrefix: string, - fileType: string, + fileType: FileType, filePath: string, error: NodeJS.ErrnoException, params: HandleErrorParams = { throwError: true }