1 |
|
2 |
|
3 | class ETA{
|
4 |
|
5 | constructor(length, initTime, initValue){
|
6 |
|
7 | this.etaBufferLength = length || 100;
|
8 |
|
9 |
|
10 | this.valueBuffer = [initValue];
|
11 | this.timeBuffer = [initTime];
|
12 |
|
13 |
|
14 | this.eta = '0';
|
15 | }
|
16 |
|
17 |
|
18 | update(time, value, total){
|
19 | this.valueBuffer.push(value);
|
20 | this.timeBuffer.push(time);
|
21 |
|
22 |
|
23 | this.calculate(total-value);
|
24 | }
|
25 |
|
26 |
|
27 | getTime(){
|
28 | return this.eta;
|
29 | }
|
30 |
|
31 |
|
32 | calculate(remaining){
|
33 |
|
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 |
|
41 | const vt_rate = v_diff/t_diff;
|
42 |
|
43 |
|
44 | this.valueBuffer = this.valueBuffer.slice(-this.etaBufferLength);
|
45 | this.timeBuffer = this.timeBuffer.slice(-this.etaBufferLength);
|
46 |
|
47 |
|
48 | const eta = Math.ceil(remaining/vt_rate/1000);
|
49 |
|
50 |
|
51 | if (isNaN(eta)){
|
52 | this.eta = 'NULL';
|
53 |
|
54 |
|
55 | }else if (!isFinite(eta)){
|
56 | this.eta = 'INF';
|
57 |
|
58 |
|
59 | }else if (eta > 100000){
|
60 | this.eta = 'INF';
|
61 |
|
62 |
|
63 | }else if (eta < 0){
|
64 | this.eta = 0;
|
65 |
|
66 | }else{
|
67 |
|
68 | this.eta = eta;
|
69 | }
|
70 | }
|
71 | }
|
72 |
|
73 | module.exports = ETA; |
\ | No newline at end of file |