a bit accurate ETA
Vladimir Bauer
8 years ago
| 56 | 56 | etaAlpha float64 |
| 57 | 57 | total int64 |
| 58 | 58 | current int64 |
| 59 | lastIncrement int64 | |
| 59 | 60 | totalAutoIncrTrigger int64 |
| 60 | 61 | totalAutoIncrBy int64 |
| 61 | 62 | trimLeftSpace bool |
| 249 | 250 | case b.operateState <- func(s *bState) { |
| 250 | 251 | if s.current == 0 { |
| 251 | 252 | s.startTime = now |
| 252 | s.blockStartTime = now | |
| 253 | 253 | } else { |
| 254 | s.updateETA(int64(n), now) | |
| 254 | s.updateETA(now.Sub(s.blockStartTime)) | |
| 255 | 255 | s.timeElapsed = now.Sub(s.startTime) |
| 256 | 256 | } |
| 257 | s.current += int64(n) | |
| 257 | s.blockStartTime = now | |
| 258 | s.lastIncrement = int64(n) | |
| 259 | s.current += s.lastIncrement | |
| 258 | 260 | if s.dynamic { |
| 259 | 261 | curp := decor.CalcPercentage(s.total, s.current, 100) |
| 260 | 262 | if 100-curp <= s.totalAutoIncrTrigger { |
| 290 | 292 | s.toComplete = true |
| 291 | 293 | cancel = nil |
| 292 | 294 | case <-b.shutdown: |
| 295 | s.updateETA(time.Since(s.blockStartTime)) | |
| 293 | 296 | b.cacheState = s |
| 294 | 297 | close(b.done) |
| 295 | 298 | return |
| 425 | 428 | } |
| 426 | 429 | } |
| 427 | 430 | |
| 428 | func (s *bState) updateETA(amount int64, now time.Time) { | |
| 429 | lastBlockTime := now.Sub(s.blockStartTime) | |
| 430 | lastItemEstimate := float64(lastBlockTime) / float64(amount) | |
| 431 | func (s *bState) updateETA(lastBlockTime time.Duration) { | |
| 432 | lastItemEstimate := float64(lastBlockTime) / float64(s.lastIncrement) | |
| 431 | 433 | s.timePerItemEstimate = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItemEstimate)) |
| 432 | s.timeRemaining = time.Duration(s.total-s.current+amount) * s.timePerItemEstimate | |
| 433 | s.blockStartTime = now | |
| 434 | s.timeRemaining = time.Duration(s.total-s.current) * s.timePerItemEstimate | |
| 434 | 435 | } |
| 435 | 436 | |
| 436 | 437 | func newStatistics(s *bState) *decor.Statistics { |