Spaces:
Sleeping
Sleeping
File size: 1,973 Bytes
1070765 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// ETA calculation
class ETA{
constructor(length, initTime, initValue){
// size of eta buffer
this.etaBufferLength = length || 100;
// eta buffer with initial values
this.valueBuffer = [initValue];
this.timeBuffer = [initTime];
// eta time value
this.eta = '0';
}
// add new values to calculation buffer
update(time, value, total){
this.valueBuffer.push(value);
this.timeBuffer.push(time);
// trigger recalculation
this.calculate(total-value);
}
// fetch estimated time
getTime(){
return this.eta;
}
// eta calculation - request number of remaining events
calculate(remaining){
// get number of samples in eta buffer
const currentBufferSize = this.valueBuffer.length;
const buffer = Math.min(this.etaBufferLength, currentBufferSize);
const v_diff = this.valueBuffer[currentBufferSize - 1] - this.valueBuffer[currentBufferSize - buffer];
const t_diff = this.timeBuffer[currentBufferSize - 1] - this.timeBuffer[currentBufferSize - buffer];
// get progress per ms
const vt_rate = v_diff/t_diff;
// strip past elements
this.valueBuffer = this.valueBuffer.slice(-this.etaBufferLength);
this.timeBuffer = this.timeBuffer.slice(-this.etaBufferLength);
// eq: vt_rate *x = total
const eta = Math.ceil(remaining/vt_rate/1000);
// check values
if (isNaN(eta)){
this.eta = 'NULL';
// +/- Infinity --- NaN already handled
}else if (!isFinite(eta)){
this.eta = 'INF';
// > 10M s ? - set upper display limit ~115days (1e7/60/60/24)
}else if (eta > 1e7){
this.eta = 'INF';
// negative ?
}else if (eta < 0){
this.eta = 0;
}else{
// assign
this.eta = eta;
}
}
}
module.exports = ETA; |