Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc time extrapolation functions #352

Merged
merged 13 commits into from
Dec 21, 2023
Merged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"js-yaml": "^4.1.0",
"moment": "^2.29.4",
"moment-range": "^4.0.2",
"ts-command-line-args": "^2.5.1",
"typescript": "^5.1.6",
"zod": "^3.22.4"
Expand Down
73 changes: 69 additions & 4 deletions src/models/time-sync.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import moment = require('moment');
import {extendMoment} from 'moment-range';
const momentRange = extendMoment(moment);

import {STRINGS} from '../config';

import {ERRORS} from '../util/errors';
import {UnitsDealer} from '../util/units-dealer';

import {ModelParams, ModelPluginInterface} from '../types/model-interface';
import {TimeNormalizerConfig} from '../types/time-sync';
import {PaddingReceipt, TimeNormalizerConfig} from '../types/time-sync';
import {UnitsDealerUsage} from '../types/units-dealer';
import {UnitKeyName} from '../types/units';

Expand Down Expand Up @@ -110,7 +112,7 @@ export class TimeSyncModel implements ModelPluginInterface {
}

const method = dealer.askToGiveMethodFor(metric);
acc[method] = method === 'avg' || method === 'sum' ? 0 : input[metric];
acc[metric] = method === 'avg' || method === 'sum' ? 0 : input[metric];

return acc;
}, {} as ModelParams);
Expand All @@ -133,23 +135,86 @@ export class TimeSyncModel implements ModelPluginInterface {
}
}

/**
* Checks if padding is needed either at start of the timeline or the end and returns status.
*/
private checkPadding(inputs: ModelParams[]): PaddingReceipt {
const startDiffInSeconds =
moment(inputs[0].timestamp).diff(moment(this.startTime)) / 1000;

const lastInput = inputs[inputs.length - 1];
const endDiffInSeconds =
moment(lastInput.timestamp)
.add(lastInput.duration, 'seconds')
.diff(moment(this.endTime)) / 1000;

return {
start: startDiffInSeconds > 0,
end: endDiffInSeconds < 0,
};
}

/**
* Pads zeroish inputs from the beginning or at the end of the inputs if needed.
*/
private padInputs(
inputs: ModelParams[],
pad: PaddingReceipt,
dealer: UnitsDealerUsage
): ModelParams[] {
const {start, end} = pad;
const paddedFromBeginning = [];

if (start) {
const dateRange = momentRange.range(
moment(this.startTime),
moment(inputs[0].timestamp).subtract(1, 'second')
);

for (const second of dateRange.by('second')) {
paddedFromBeginning.push(
this.fillWithZeroishInput(inputs[0], second.valueOf(), dealer)
); // check if converting to value of is needed
}
}

const paddedArray = paddedFromBeginning.concat(inputs);

if (end) {
const lastInput = inputs[inputs.length - 1];
const dateRange = momentRange.range(
moment(lastInput.timestamp).add(lastInput.duration + 1, 'seconds'),
moment(this.endTime)
);

for (const second of dateRange.by('second')) {
paddedArray.push(
this.fillWithZeroishInput(lastInput, second.valueOf(), dealer)
);
}
}
return paddedArray;
}

/**
* Normalizes provided time window according to time configuration.
*/
async execute(inputs: ModelParams[]): Promise<ModelParams[]> {
this.validateParams();

const dealer = await UnitsDealer();
const pad = this.checkPadding(inputs);
const paddedInputs = this.padInputs(inputs, pad, dealer);

return inputs
return paddedInputs
.reduce((acc, input, index) => {
const currentMoment = moment(input.timestamp);

/**
* Checks if not the first input, then check consistency with previous ones.
*/
if (index > 0) {
const previousInput = inputs[index - 1];
const previousInput = paddedInputs[index - 1];
const previousInputTimestamp = moment(previousInput.timestamp);
const compareableTime = previousInputTimestamp.add(
previousInput.duration,
Expand Down
5 changes: 5 additions & 0 deletions src/types/time-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ export type TimeNormalizerConfig = {
'end-time': string;
interval: number;
};

export type PaddingReceipt = {
start: boolean;
end: boolean;
};