Skip to content

Commit

Permalink
happy lintin' round1
Browse files Browse the repository at this point in the history
  • Loading branch information
pazbardanl committed Jan 17, 2024
1 parent a74b267 commit f6fdee2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 82 deletions.
32 changes: 16 additions & 16 deletions src/lib/mock-observations/helpers/CommonGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 6 in src/lib/mock-observations/helpers/CommonGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Replace `{[key:` with `·{[key:·`
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 {

Check warning on line 13 in src/lib/mock-observations/helpers/CommonGenerator.ts

View workflow job for this annotation

GitHub Actions / build

'_historical' is defined but never used
// TODO PB -- object immutabilty - copy by value here
return this.generateObject

Check failure on line 15 in src/lib/mock-observations/helpers/CommonGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`
}

Check failure on line 17 in src/lib/mock-observations/helpers/CommonGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Delete `↹`

Check failure on line 17 in src/lib/mock-observations/helpers/CommonGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
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;
77 changes: 38 additions & 39 deletions src/lib/mock-observations/helpers/RandIntGenerator.ts
Original file line number Diff line number Diff line change
@@ -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 {

Check failure on line 10 in src/lib/mock-observations/helpers/RandIntGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·[key:·string]:·any·` with `[key:·string]:·any`
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 {

Check warning on line 17 in src/lib/mock-observations/helpers/RandIntGenerator.ts

View workflow job for this annotation

GitHub Actions / build

'_historical' is defined but never used
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 {

Check failure on line 37 in src/lib/mock-observations/helpers/RandIntGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Delete `·`
if (!config.hasOwnProperty(RandIntGenerator.MIN)) {

Check failure on line 38 in src/lib/mock-observations/helpers/RandIntGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Do not access Object.prototype method 'hasOwnProperty' from target object
// TODO PB - custom / more specific error?
throw new Error('config is missing ' + RandIntGenerator.MIN);
}
if (!config.hasOwnProperty(RandIntGenerator.MAX)) {

Check failure on line 42 in src/lib/mock-observations/helpers/RandIntGenerator.ts

View workflow job for this annotation

GitHub Actions / build

Do not access Object.prototype method 'hasOwnProperty' from target object
// TODO PB - custom / more specific error?
throw new Error('config is missing ' + RandIntGenerator.MAX);
}
}
}

export default RandIntGenerator;
59 changes: 32 additions & 27 deletions src/lib/mock-observations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ 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 ?
staticParams: object | undefined;
errorBuilder = buildErrorMessage(MockObservations);
timestampFrom: dayjs.Dayjs | undefined;
timestampTo: dayjs.Dayjs | undefined;
duration: number = 0;
duration = 0;
timeBuckets: dayjs.Dayjs[] = [];
components: Record<string, Record<string, string>> = {}
components: Record<string, Record<string, string>> = {};
// components: object[] = [];
generatorConfigs: object = {};
dateList: dayjs.Dayjs[] = [];
Expand All @@ -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)) {

Check failure on line 34 in src/lib/mock-observations/index.ts

View workflow job for this annotation

GitHub Actions / build

Delete `␍`
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) {

Check failure on line 43 in src/lib/mock-observations/index.ts

View workflow job for this annotation

GitHub Actions / build

Delete `␍`
Expand Down Expand Up @@ -69,42 +68,42 @@ export class MockObservations implements ModelPluginInterface {
): Promise<ModelPluginInterface> {
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) {
this.duration = staticParams['duration'] as number;
}
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<string, Record<string, string>>;
this.components = staticParams['components'] as Record<
string,
Record<string, string>>;
}
else {
throw new InputValidationError(
this.errorBuilder({ message: 'components missing from input data' })
this.errorBuilder({message: 'components missing from input data'})
);
}
if ('generators' in staticParams) {
Expand All @@ -113,26 +112,32 @@ 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
this.staticParams = staticParams;
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');
}
return timeBuckets;
}

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) {
Expand All @@ -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);
}
}
});
Expand Down

0 comments on commit f6fdee2

Please sign in to comment.