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

Setting curr property breaks :eta and :rate #159

Open
kaatt opened this issue Aug 12, 2017 · 2 comments
Open

Setting curr property breaks :eta and :rate #159

kaatt opened this issue Aug 12, 2017 · 2 comments

Comments

@kaatt
Copy link

kaatt commented Aug 12, 2017

Setting the curr property breaks the :eta and :rate tokens.

To reproduce, edit examples/customtokens.js to include the curr property:

var bar = new ProgressBar(':percent eta: :eta downloading :current/:total :file :rate', {
  total: list.length, curr: 2
})
$ node examples/customtokens.js # Before
30% eta: 2.4 downloading 3/10 image03.jpg 3
^C
$ node examples/customtokens.js # After
100% eta: 0.0 downloading 10/10 image10.jpg NaN

:eta is always 0.0 and :rate is NaN

The bug isn't present when curr: 0

@FoxxMD
Copy link

FoxxMD commented Aug 24, 2017

Experiencing this as well.

Workaround is not setting curr and then just ticking the bar the value that would have been set to curr. Not great but not bad for long-running operations.

@mildmojo
Copy link

mildmojo commented Feb 24, 2020

The problem is that the library doesn't record the download start time when you specify curr. When that's fixed, the rate and eta calculations will need some work to take a starting curr value into account, too.

Workaround

The most basic workaround is to set the ProgressBar instance's start attribute to Date.now(). That'll keep :rate and :eta from erroring, but the calculations will act like you made curr amount of progress in zero time at an infinite rate, and it'll take a long time for the rate and ETA to settle.

Here's a workaround using custom tokens :myRateKB and :myETA. I'm displaying file transfer progress. The downit library provides a progress callback with total-bytes-received in gotBytes, and I'm diffing that value against the last-seen gotBytes to get a delta to feed to bar.tick().

  const lastDownloadSize = startingFileDiskSize;

  bar = new ProgressBar(`  [:bar] :myRateKB KiBps :percent :myETAs remaining`, {
    width: 40,
    curr: startingFileDiskSize,
    total: remoteSize
  });
  
  // Just setting `bar.start` will make :rate and :eta work, but will act like
  // the first `curr` bytes were transferred in zero time at infinite rate.
  bar.start = Date.now();

  downit(url, outPath, {
    onprogress: gotBytes => {
      const elapsedSecs = (Date.now() - bar.start) / 1000;
      const sessionTotalSize = remoteSize - startingFileDiskSize;
      const sessionProgress = gotBytes - startingFileDiskSize;
      bar.tick(gotBytes - lastDownloadSize, {
        myRateKB: Math.round((sessionProgress / 1024) / elapsedSecs),
        myETA: Math.round(elapsedSecs * (sessionTotalSize / sessionProgress - 1))
      });
      lastDownloadSize = got;
    }
  });

zhizhuxialiwen pushed a commit to zhizhuxialiwen/node-progress that referenced this issue Dec 11, 2020
Setting `curr` property breaks `:eta`, `:rate`, `:elapsed`. For example, the input `curr` value is 2, but the output `:eta`, `:rate`, `:elapsed` values respectively are 0.0, NaN, 0.0. Current required nature number and is less than or equal to total.

Closes visionmedia#159
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants