UNPKG

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