MCPcopy Create free account
hub / github.com/npkgz/cli-progress / ETA

Class ETA

lib/eta.js:3–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 // > 10M s ? - set upper display limit ~115days (1e7/60/60/24)
59 }else if (eta > 1e7){
60 this.eta = 'INF';

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected