11'use strict' ;
22
33import Logger from '../../services/logger' ;
4+ import duration from 'duration' ;
45
56class InstallController {
67 constructor ( $scope , $timeout , installerDataSvc ) {
@@ -99,6 +100,8 @@ class InstallController {
99100 }
100101}
101102
103+ const smoothFactor = 0.05 ;
104+
102105class 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 ;
0 commit comments