Browse Source

Merge pull request #197 from goto-bus-stop/bugfix/time-remaining

Fix ETA when multiple files are being uploaded, closes #188.
Artur Paikin 8 years ago
parent
commit
f5f32cb39f
2 changed files with 15 additions and 7 deletions
  1. 6 1
      src/core/Utils.js
  2. 9 6
      src/plugins/Dashboard/index.js

+ 6 - 1
src/core/Utils.js

@@ -288,11 +288,15 @@ function getSpeed (fileProgress) {
   return uploadSpeed
 }
 
+function getBytesRemaining (fileProgress) {
+  return fileProgress.bytesTotal - fileProgress.bytesUploaded
+}
+
 function getETA (fileProgress) {
   if (!fileProgress.bytesUploaded) return 0
 
   const uploadSpeed = getSpeed(fileProgress)
-  const bytesRemaining = fileProgress.bytesTotal - fileProgress.bytesUploaded
+  const bytesRemaining = getBytesRemaining(fileProgress)
   const secondsRemaining = Math.round(bytesRemaining / uploadSpeed * 10) / 10
 
   return secondsRemaining
@@ -397,6 +401,7 @@ module.exports = {
   dataURItoBlob,
   dataURItoFile,
   getSpeed,
+  getBytesRemaining,
   getETA,
   // makeWorker,
   // makeCachingFunction,

+ 9 - 6
src/plugins/Dashboard/index.js

@@ -3,7 +3,7 @@ const Translator = require('../../core/Translator')
 const dragDrop = require('drag-drop')
 const Dashboard = require('./Dashboard')
 const { getSpeed } = require('../../core/Utils')
-const { getETA } = require('../../core/Utils')
+const { getBytesRemaining } = require('../../core/Utils')
 const { prettyETA } = require('../../core/Utils')
 const { findDOMElement } = require('../../core/Utils')
 const prettyBytes = require('prettier-bytes')
@@ -288,13 +288,16 @@ module.exports = class DashboardUI extends Plugin {
   }
 
   getTotalETA (files) {
-    let totalSeconds = 0
+    const totalSpeed = this.getTotalSpeed(files)
+    if (totalSpeed === 0) {
+      return 0
+    }
 
-    files.forEach((file) => {
-      totalSeconds = totalSeconds + getETA(file.progress)
-    })
+    const totalBytesRemaining = files.reduce((total, file) => {
+      return total + getBytesRemaining(file.progress)
+    }, 0)
 
-    return totalSeconds
+    return Math.round(totalBytesRemaining / totalSpeed * 10) / 10
   }
 
   render (state) {