refactor: add type for worker configuration attribute
[e-mobility-charging-stations-simulator.git] / src / utils / ConfigurationUtils.ts
1 import { dirname, join, resolve } from 'node:path';
2 import { fileURLToPath } from 'node:url';
3
4 import chalk from 'chalk';
5
6 import { Constants } from './Constants';
7 import { isNotEmptyString, logPrefix as utilsLogPrefix } from './Utils';
8 import { FileType, StorageType } from '../types';
9 import type { elementsPerWorkerType } from '../types/ConfigurationData';
10 import { WorkerProcessType } from '../worker';
11
12 export const logPrefix = (): string => {
13 return utilsLogPrefix(' Simulator configuration |');
14 };
15
16 export const buildPerformanceUriFilePath = (file: string) => {
17 return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}`;
18 };
19
20 export const getDefaultPerformanceStorageUri = (storageType: StorageType) => {
21 switch (storageType) {
22 case StorageType.JSON_FILE:
23 return buildPerformanceUriFilePath(
24 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME}`,
25 );
26 case StorageType.SQLITE:
27 return buildPerformanceUriFilePath(
28 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`,
29 );
30 default:
31 throw new Error(`Unsupported storage type '${storageType}'`);
32 }
33 };
34
35 export const handleFileException = (
36 file: string,
37 fileType: FileType,
38 error: NodeJS.ErrnoException,
39 logPfx: string,
40 ): void => {
41 const prefix = isNotEmptyString(logPfx) ? `${logPfx} ` : '';
42 let logMsg: string;
43 switch (error.code) {
44 case 'ENOENT':
45 logMsg = `${fileType} file ${file} not found: `;
46 break;
47 case 'EEXIST':
48 logMsg = `${fileType} file ${file} already exists: `;
49 break;
50 case 'EACCES':
51 logMsg = `${fileType} file ${file} access denied: `;
52 break;
53 case 'EPERM':
54 logMsg = `${fileType} file ${file} permission denied: `;
55 break;
56 default:
57 logMsg = `${fileType} file ${file} error: `;
58 }
59 console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error);
60 throw error;
61 };
62
63 export const checkWorkerProcessType = (workerProcessType: WorkerProcessType): void => {
64 if (!Object.values(WorkerProcessType).includes(workerProcessType)) {
65 throw new SyntaxError(
66 `Invalid worker process type '${workerProcessType}' defined in configuration`,
67 );
68 }
69 };
70
71 export const checkWorkerElementsPerWorker = (
72 elementsPerWorker: elementsPerWorkerType | undefined,
73 ): void => {
74 if (
75 elementsPerWorker !== undefined &&
76 elementsPerWorker !== 'auto' &&
77 elementsPerWorker !== 'all' &&
78 !Number.isSafeInteger(elementsPerWorker)
79 ) {
80 throw new SyntaxError(
81 `Invalid number of elements per worker '${elementsPerWorker}' defined in configuration`,
82 );
83 }
84 if (Number.isSafeInteger(elementsPerWorker) && (elementsPerWorker as number) <= 0) {
85 throw RangeError(
86 `Invalid negative or zero number of elements per worker '${elementsPerWorker}' defined in configuration`,
87 );
88 }
89 };