Skip to content

Commit 7145fe9

Browse files
jrichter1dgolovin
authored andcommitted
JBDS-4375 Show remaining time for downloads
1 parent e1d6e5a commit 7145fe9

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

browser/pages/install/controller.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
import Logger from '../../services/logger';
4+
import duration from 'duration';
45

56
class InstallController {
67
constructor($scope, $timeout, installerDataSvc) {
@@ -99,6 +100,8 @@ class InstallController {
99100
}
100101
}
101102

103+
const smoothFactor = 0.05;
104+
102105
class ProgressState {
103106
constructor(key, productName, productVersion, productDesc, $scope, $timeout, minValue=0, maxValue=100) {
104107
this.key = key;
@@ -111,9 +114,12 @@ class ProgressState {
111114
this.label = '';
112115
this.status = '';
113116
this.currentAmount = 0;
117+
this.lastAmount = 0;
114118
this.totalSize = 0;
115119
this.min = minValue;
116120
this.max = maxValue;
121+
this.lastTime = Date.now();
122+
this.averageSpeed = 0;
117123
}
118124

119125
setTotalDownloadSize(size) {
@@ -122,13 +128,29 @@ class ProgressState {
122128

123129
setCurrent(newVal) {
124130
if (newVal > this.currentAmount && newVal <= this.totalSize) {
131+
this.lastAmount = this.currentAmount;
125132
this.currentAmount = newVal;
133+
134+
let remaining = this.calculateTime();
135+
let labelLevel = remaining.seconds > 59 ? 2 : 1;
136+
let remainingLabel = (remaining.seconds > 0) ? remaining.toString(1, labelLevel) + ' left -- ' : '';
137+
126138
this.current = Math.round(this.currentAmount / this.totalSize * 100);
127-
this.label = this.sizeInKB(this.currentAmount) + ' / ' + this.sizeInKB(this.totalSize) + ' KB (' + this.current + '%)';
139+
this.label = remainingLabel + this.sizeInKB(this.currentAmount) + ' / ' + this.sizeInKB(this.totalSize) + ' KB (' + this.current + '%)';
128140
this.$timeout(()=>this.$scope.$apply());
129141
}
130142
}
131143

144+
calculateTime() {
145+
let currentTime = new Date();
146+
this.lastSpeed = (this.currentAmount - this.lastAmount) / (currentTime.getTime() - this.lastTime);
147+
this.lastTime = currentTime;
148+
this.averageSpeed = this.averageSpeed === 0 ? this.lastSpeed : smoothFactor * this.lastSpeed + (1 - smoothFactor) * this.averageSpeed;
149+
150+
let end = new Date(currentTime.getTime() + (this.totalSize - this.currentAmount) / this.averageSpeed);
151+
return duration(currentTime, end);
152+
}
153+
132154
setStatus(newStatus) {
133155
if (newStatus === this.status) {
134156
return;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"angular-mocks": "1.6.4",
3737
"angular-ui-router": "0.4.2",
3838
"del": "2.2.2",
39+
"duration": "0.2.0",
3940
"fs-extra": "2.1.2",
4041
"glob": "7.1.1",
4142
"globby": "6.1.0",

0 commit comments

Comments
 (0)