refactoring eta vars
Vladimir Bauer
8 years ago
| 238 | 238 | if n < 1 { |
| 239 | 239 | return |
| 240 | 240 | } |
| 241 | now := time.Now() | |
| 241 | 242 | select { |
| 242 | 243 | case b.operateState <- func(s *bState) { |
| 243 | 244 | if s.toComplete { |
| 244 | 245 | return |
| 245 | 246 | } |
| 246 | next := time.Now() | |
| 247 | 247 | if s.current == 0 { |
| 248 | s.startTime = next | |
| 249 | s.blockStartTime = next | |
| 248 | s.startTime = now | |
| 249 | s.blockStartTime = now | |
| 250 | 250 | } else { |
| 251 | now := time.Now() | |
| 252 | s.updateTimePerItemEstimate(n, now, next) | |
| 251 | s.updateTimePerItemEstimate(n, now) | |
| 253 | 252 | s.timeElapsed = now.Sub(s.startTime) |
| 254 | 253 | } |
| 255 | 254 | s.current += int64(n) |
| 423 | 422 | } |
| 424 | 423 | } |
| 425 | 424 | |
| 426 | func (s *bState) updateTimePerItemEstimate(amount int, now, next time.Time) { | |
| 425 | func (s *bState) updateTimePerItemEstimate(amount int, now time.Time) { | |
| 427 | 426 | lastBlockTime := now.Sub(s.blockStartTime) |
| 428 | 427 | lastItemEstimate := float64(lastBlockTime) / float64(amount) |
| 429 | 428 | s.timePerItem = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItem)) |
| 430 | s.blockStartTime = next | |
| 429 | s.blockStartTime = now | |
| 431 | 430 | } |
| 432 | 431 | |
| 433 | 432 | func newStatistics(s *bState) *decor.Statistics { |