Codebase list golang-github-vbauerster-mpb / 5086a9d
More Statistics fields Vladimir Bauer 9 years ago
1 changed file(s) with 45 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
4646 // Statistics represents statistics of the progress bar.
4747 // Cantains: Total, Current, TimeElapsed, TimePerItemEstimate
4848 type Statistics struct {
49 Total, Current int64
50 TimeElapsed, TimePerItemEstimate time.Duration
49 Completed bool
50 Total int64
51 Current int64
52 IncrAmount int64
53 StartTime time.Time
54 TimeElapsed time.Duration
55 TimePerItemEstimate time.Duration
5156 }
5257
5358 // Eta returns exponential-weighted-moving-average ETA estimator
7176 etaAlpha float64
7277 total int64
7378 current int64
79 incrAmount int64
7480 trimLeftSpace bool
7581 trimRightSpace bool
82 completed bool
83 startTime time.Time
7684 timeElapsed time.Duration
7785 timePerItem time.Duration
7886 appendFuncs []DecoratorFunc
264272 }
265273
266274 func (b *Bar) server(id int, total int64, width int, format string, wg *sync.WaitGroup, cancel <-chan struct{}) {
267 var completed bool
268 timeStarted := time.Now()
269 prevStartTime := timeStarted
270 var blockStartTime time.Time
275 incrStartTime := time.Now()
271276 barState := state{
272 id: id,
273 width: width,
274 format: barFmtRunes{'[', '=', '>', '-', ']'},
275 etaAlpha: 0.25,
276 total: total,
277 id: id,
278 width: width,
279 format: barFmtRunes{'[', '=', '>', '-', ']'},
280 etaAlpha: 0.25,
281 total: total,
282 startTime: incrStartTime,
283 completed: false,
277284 }
278285 if total <= 0 {
279286 barState.simpleSpinner = getSpinner()
286293 }()
287294 for {
288295 select {
289 case i := <-b.incrCh:
290 blockStartTime = time.Now()
291 n := barState.current + i
296 case barState.incrAmount = <-b.incrCh:
297 n := barState.current + barState.incrAmount
292298 if total > 0 && n > total {
293299 barState.current = total
294 completed = true
300 barState.completed = true
295301 break // break out of select
296302 }
297 barState.timeElapsed = time.Since(timeStarted)
298 barState.timePerItem = calcTimePerItemEstimate(barState.timePerItem, prevStartTime, barState.etaAlpha, i)
303 barState.timeElapsed = time.Since(barState.startTime)
304 barState.updateTimePerItemEstimate(incrStartTime)
299305 if n == total {
300 completed = true
306 barState.completed = true
301307 }
302308 barState.current = n
303 prevStartTime = blockStartTime
309 incrStartTime = time.Now()
304310 case data := <-b.dCommandCh:
305311 switch data.action {
306312 case dAppend:
321327 case barState.trimLeftSpace = <-b.trimLeftCh:
322328 case barState.trimRightSpace = <-b.trimRightCh:
323329 case <-b.flushedCh:
324 if completed {
330 if barState.completed {
325331 return
326332 }
327333 case <-b.completeReqCh:
354360 b.removeReqCh <- struct{}{}
355361 }
356362
357 func (s *state) updateFormat(format string) {
358 if format == "" {
359 return
360 }
361 for i, n := 0, 0; len(format) > 0; i++ {
362 s.format[i], n = utf8.DecodeRuneInString(format)
363 format = format[n:]
364 }
365 }
366
367363 func (b *Bar) render(rFn func(chan []byte), termWidth int, prependWs, appendWs *widthSync) <-chan []byte {
368364 ch := make(chan []byte)
369365
376372 }()
377373
378374 return ch
375 }
376
377 func (s *state) updateFormat(format string) {
378 if format == "" {
379 return
380 }
381 for i, n := 0, 0; len(format) > 0; i++ {
382 s.format[i], n = utf8.DecodeRuneInString(format)
383 format = format[n:]
384 }
385 }
386
387 func (s *state) updateTimePerItemEstimate(incrStartTime time.Time) {
388 lastBlockTime := time.Since(incrStartTime) // shorthand for time.Now().Sub(t)
389 lastItemEstimate := float64(lastBlockTime) / float64(s.incrAmount)
390 s.timePerItem = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItem))
379391 }
380392
381393 func draw(s *state, termWidth int, prependWs, appendWs *widthSync) []byte {
493505
494506 func newStatistics(s *state) *Statistics {
495507 return &Statistics{
508 Completed: s.completed,
496509 Total: s.total,
497510 Current: s.current,
511 IncrAmount: s.incrAmount,
512 StartTime: s.startTime,
498513 TimeElapsed: s.timeElapsed,
499514 TimePerItemEstimate: s.timePerItem,
500515 }
508523 fmtBytes[i] = buf
509524 }
510525 return fmtBytes
511 }
512
513 func calcTimePerItemEstimate(tpie time.Duration, blockStartTime time.Time, alpha float64, items int64) time.Duration {
514 lastBlockTime := time.Since(blockStartTime)
515 lastItemEstimate := float64(lastBlockTime) / float64(items)
516 return time.Duration((alpha * lastItemEstimate) + (1-alpha)*float64(tpie))
517526 }
518527
519528 func percentage(total, current int64, ratio int) int {