default etaAlpha = 0.12
Vladimir Bauer
8 years ago
| 21 | 21 | |
| 22 | 22 | const ( |
| 23 | 23 | formatLen = 5 |
| 24 | etaAlpha = 0.25 | |
| 24 | etaAlpha = 0.12 | |
| 25 | 25 | ) |
| 26 | 26 | |
| 27 | 27 | type barRunes [formatLen]rune |
| 255 | 255 | now := time.Now() |
| 256 | 256 | select { |
| 257 | 257 | case b.operateState <- func(s *bState) { |
| 258 | if s.current == 0 { | |
| 259 | s.startTime = now | |
| 260 | } else { | |
| 261 | s.timeElapsed = now.Sub(s.startTime) | |
| 262 | } | |
| 263 | 258 | s.current += int64(n) |
| 264 | s.updateETA(n, now.Sub(s.blockStartTime)) | |
| 259 | s.timeElapsed = now.Sub(s.startTime) | |
| 260 | s.timeRemaining = s.calcETA(n, now.Sub(s.blockStartTime)) | |
| 265 | 261 | if s.dynamic { |
| 266 | 262 | curp := decor.CalcPercentage(s.total, s.current, 100) |
| 267 | 263 | if 100-curp <= s.totalAutoIncrTrigger { |
| 289 | 285 | |
| 290 | 286 | func (b *Bar) serve(wg *sync.WaitGroup, s *bState, cancel <-chan struct{}) { |
| 291 | 287 | defer wg.Done() |
| 288 | s.startTime = time.Now() | |
| 289 | s.blockStartTime = s.startTime | |
| 292 | 290 | for { |
| 293 | 291 | select { |
| 294 | 292 | case op := <-b.operateState: |
| 432 | 430 | } |
| 433 | 431 | } |
| 434 | 432 | |
| 435 | func (s *bState) updateETA(n int, lastBlockTime time.Duration) { | |
| 433 | func (s *bState) calcETA(n int, lastBlockTime time.Duration) time.Duration { | |
| 436 | 434 | lastItemEstimate := float64(lastBlockTime) / float64(n) |
| 437 | 435 | s.timePerItemEstimate = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItemEstimate)) |
| 438 | s.timeRemaining = time.Duration(s.total-s.current) * s.timePerItemEstimate | |
| 436 | return time.Duration(s.total-s.current) * s.timePerItemEstimate | |
| 439 | 437 | } |
| 440 | 438 | |
| 441 | 439 | func newStatistics(s *bState) *decor.Statistics { |