From f6fdee23ccb5ddd2ffe4b6130b976521a60eda28 Mon Sep 17 00:00:00 2001 From: Paz Barda Date: Wed, 17 Jan 2024 13:40:31 +0200 Subject: [PATCH] happy lintin' round1 --- .../helpers/CommonGenerator.ts | 32 ++++---- .../helpers/RandIntGenerator.ts | 77 +++++++++---------- src/lib/mock-observations/index.ts | 59 +++++++------- 3 files changed, 86 insertions(+), 82 deletions(-) diff --git a/src/lib/mock-observations/helpers/CommonGenerator.ts b/src/lib/mock-observations/helpers/CommonGenerator.ts index 4d3814b..bfda831 100644 --- a/src/lib/mock-observations/helpers/CommonGenerator.ts +++ b/src/lib/mock-observations/helpers/CommonGenerator.ts @@ -3,29 +3,29 @@ import {Generator} from '../interfaces'; class CommonGenerator implements Generator { private name = ''; private generateObject: {} = {}; - initialise(name: string, config:{ [key: string]: any }): void { + initialise(name: string, config:{[key:string]: any}): void { this.name = this.validateName(name); // TODO PB -- validate config is not null, not empty and a valid yml object, use yaml.parse(input) // TODO PB -- object immutabilty - copy by value here this.generateObject = config; - } + } - next(_historical: Object[]): Object { - // TODO PB -- object immutabilty - copy by value here - return this.generateObject - } + next(_historical: Object[]): Object { + // TODO PB -- object immutabilty - copy by value here + return this.generateObject + } - public getName(): String { - return this.name; + public getName(): String { + return this.name; + } + + private validateName(name: string | null): string { + if (name === null || name.trim() === '') { + // TODO PB - custom / more specific error? + throw new Error('name is empty or null'); } - - private validateName(name: string | null): string { - if (name === null || name.trim() === '') { - // TODO PB - custom / more specific error? - throw new Error('name is empty or null'); - } - return name; - } + return name; + } } export default CommonGenerator; diff --git a/src/lib/mock-observations/helpers/RandIntGenerator.ts b/src/lib/mock-observations/helpers/RandIntGenerator.ts index eb81026..3668817 100644 --- a/src/lib/mock-observations/helpers/RandIntGenerator.ts +++ b/src/lib/mock-observations/helpers/RandIntGenerator.ts @@ -1,50 +1,49 @@ -import { Generator } from '../interfaces'; +import {Generator} from '../interfaces'; class RandIntGenerator implements Generator { - private static readonly MIN: string = 'min'; - private static readonly MAX: string = 'max'; - - private fieldToPopulate: string = ''; - private min: number = 0; - private max: number = 0; - + private static readonly MIN: string = 'min'; + private static readonly MAX: string = 'max'; + private fieldToPopulate = ''; + private min = 0; + private max = 0; + initialise(fieldToPopulate: string, config: { [key: string]: any }): void { this.fieldToPopulate = this.validateName(fieldToPopulate); - this.validateConfig(config); - this.min = config[RandIntGenerator.MIN]; - this.max = config[RandIntGenerator.MAX]; - } + this.validateConfig(config); + this.min = config[RandIntGenerator.MIN]; + this.max = config[RandIntGenerator.MAX]; + } - next(_historical: Object[]): Object { - const randomNumber = Math.random(); - var scaledNumber = randomNumber * (this.max - this.min) + this.min; - var truncatedNumber = Math.trunc(scaledNumber); + next(_historical: Object[]): Object { + const randomNumber = Math.random(); + const scaledNumber = randomNumber * (this.max - this.min) + this.min; + const truncatedNumber = Math.trunc(scaledNumber); const retObject = { - [this.fieldToPopulate]: truncatedNumber - }; - return retObject; - } - - // TODO PB: extract to a utils class? - private validateName(name: string | null): string { - if (name === null || name.trim() === '') { - // TODO PB - custom / more specific error? - throw new Error('name is empty or null'); - } - return name; + [this.fieldToPopulate]: truncatedNumber, + }; + return retObject; } - // TODO PB: extract to a utils class? - private validateConfig(config: { [key: string]: any }): void { - if (!config.hasOwnProperty(RandIntGenerator.MIN)) { - // TODO PB - custom / more specific error? - throw new Error('config is missing ' + RandIntGenerator.MIN); - } - if (!config.hasOwnProperty(RandIntGenerator.MAX)) { - // TODO PB - custom / more specific error? - throw new Error('config is missing ' + RandIntGenerator.MAX); - } - } + // TODO PB: extract to a utils class? + private validateName(name: string | null): string { + if (name === null || name.trim() === '') { + // TODO PB - custom / more specific error? + throw new Error('name is empty or null'); + } + return name; + } + + // TODO PB: extract to a utils class? + private validateConfig(config: {[key: string]: any }): void { + if (!config.hasOwnProperty(RandIntGenerator.MIN)) { + // TODO PB - custom / more specific error? + throw new Error('config is missing ' + RandIntGenerator.MIN); + } + if (!config.hasOwnProperty(RandIntGenerator.MAX)) { + // TODO PB - custom / more specific error? + throw new Error('config is missing ' + RandIntGenerator.MAX); + } + } } export default RandIntGenerator; diff --git a/src/lib/mock-observations/index.ts b/src/lib/mock-observations/index.ts index 5e6dd33..6dba392 100644 --- a/src/lib/mock-observations/index.ts +++ b/src/lib/mock-observations/index.ts @@ -3,14 +3,13 @@ import {ERRORS} from '../../util/errors'; import {buildErrorMessage} from '../../util/helpers'; import {ModelParams} from '../../types/common'; -import { ModelPluginInterface } from '../../interfaces'; +import {ModelPluginInterface} from '../../interfaces'; import * as dayjs from 'dayjs'; import CommonGenerator from './helpers/CommonGenerator'; import RandIntGenerator from './helpers/RandIntGenerator'; -import Generator from './interfaces/index'; - -const { InputValidationError } = ERRORS; +import Generator from './interfaces/index'; +const {InputValidationError} = ERRORS; export class MockObservations implements ModelPluginInterface { // TODO PB - private members ? @@ -18,9 +17,9 @@ export class MockObservations implements ModelPluginInterface { errorBuilder = buildErrorMessage(MockObservations); timestampFrom: dayjs.Dayjs | undefined; timestampTo: dayjs.Dayjs | undefined; - duration: number = 0; + duration = 0; timeBuckets: dayjs.Dayjs[] = []; - components: Record> = {} + components: Record> = {}; // components: object[] = []; generatorConfigs: object = {}; dateList: dayjs.Dayjs[] = []; @@ -30,15 +29,15 @@ export class MockObservations implements ModelPluginInterface { console.log(inputs); // TODO PB - consider making generators a member and creating them at config() const generators = this.createGenerators(this.generatorConfigs); - let observations: ModelParams[] = []; + const observations: ModelParams[] = []; for (const componentKey in this.components) { - if (this.components.hasOwnProperty(componentKey)) { + if (Object.prototype.hasOwnProperty.call(this.components, componentKey)) { const component = this.components[componentKey]; for (const timeBucket of this.timeBuckets) { - let observation: ModelParams = { + const observation: ModelParams = { timestamp: timeBucket.format('YYYY-MM-DD HH:mm:ss'), // TODO PB -- this is not always true, the last timebucket might be shorter than the global duration. so duratio should be a property of timebucket (define a DTO for this) - duration: this.duration + duration: this.duration, }; // TODO PB -- consider this way to copy key-value pairs from component to observation, it looks like an overkill for (const key in component) { @@ -69,23 +68,21 @@ export class MockObservations implements ModelPluginInterface { ): Promise { if (staticParams === undefined) { throw new InputValidationError( - this.errorBuilder({ message: 'Input data is missing' }) + this.errorBuilder({message: 'Input data is missing'}) ); } if ('timestamp-from' in staticParams) { this.timestampFrom = dayjs(staticParams['timestamp-from'] as string); - } - else { + } else { throw new InputValidationError( - this.errorBuilder({ message: 'timestamp-from missing from input data' }) + this.errorBuilder({message: 'timestamp-from missing from input data'}) ); } if ('timestamp-to' in staticParams) { this.timestampTo = dayjs(staticParams['timestamp-to'] as string); - } - else { + } else { throw new InputValidationError( - this.errorBuilder({ message: 'timestamp-to missing from input data' }) + this.errorBuilder({message: 'timestamp-to missing from input data'}) ); } if ('duration' in staticParams) { @@ -93,18 +90,20 @@ export class MockObservations implements ModelPluginInterface { } else { throw new InputValidationError( - this.errorBuilder({ message: 'duration missing from input data' }) + this.errorBuilder({message: 'duration missing from input data'}) ); } this.timeBuckets = this.createTimeBuckets(this.timestampFrom, this.timestampTo, this.duration); if ('components' in staticParams) { // TODO PB -- is this casting needed? - this.components = staticParams['components'] as Record>; + this.components = staticParams['components'] as Record< + string, + Record>; } else { throw new InputValidationError( - this.errorBuilder({ message: 'components missing from input data' }) + this.errorBuilder({message: 'components missing from input data'}) ); } if ('generators' in staticParams) { @@ -113,7 +112,7 @@ export class MockObservations implements ModelPluginInterface { } else { throw new InputValidationError( - this.errorBuilder({ message: 'generators missing from input data' }) + this.errorBuilder({message: 'generators missing from input data'}) ); } // TODO PB -- remove dummy line @@ -121,10 +120,16 @@ export class MockObservations implements ModelPluginInterface { return this; } - private createTimeBuckets(timestampFrom: dayjs.Dayjs, timestampTo: dayjs.Dayjs, duration: number): dayjs.Dayjs[] { - let timeBuckets: dayjs.Dayjs[] = [] + private createTimeBuckets( + timestampFrom: dayjs.Dayjs, + timestampTo: dayjs.Dayjs, + duration: number): dayjs.Dayjs[] { + const timeBuckets: dayjs.Dayjs[] = []; let currTimestamp: dayjs.Dayjs = timestampFrom; - while (currTimestamp.isBefore(timestampTo) || currTimestamp.isSame(timestampTo, 'second')) { + while ( + currTimestamp.isBefore(timestampTo) || + currTimestamp.isSame(timestampTo, 'second') + ) { timeBuckets.push(currTimestamp); currTimestamp = currTimestamp.add(duration, 'second'); } @@ -132,7 +137,7 @@ export class MockObservations implements ModelPluginInterface { } private createGenerators(generatorsConfig: object): Generator[] { - let generators: Generator[] = []; + const generators: Generator[] = []; Object.entries(generatorsConfig).forEach(([key, value]) => { //console.log(`generator name: ${key}, generator config: ${value}`); if ('common' === key) { @@ -142,9 +147,9 @@ export class MockObservations implements ModelPluginInterface { } if ('randint' === key) { for (const fieldToPopulate in value) { - const randIntGenerator = new RandIntGenerator() + const randIntGenerator = new RandIntGenerator(); randIntGenerator.initialise(fieldToPopulate, value[fieldToPopulate]); - generators.push(randIntGenerator) + generators.push(randIntGenerator); } } });