More Statistics fields
Vladimir Bauer
9 years ago
| 46 | 46 | // Statistics represents statistics of the progress bar. |
| 47 | 47 | // Cantains: Total, Current, TimeElapsed, TimePerItemEstimate |
| 48 | 48 | 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 | |
| 51 | 56 | } |
| 52 | 57 | |
| 53 | 58 | // Eta returns exponential-weighted-moving-average ETA estimator |
| 71 | 76 | etaAlpha float64 |
| 72 | 77 | total int64 |
| 73 | 78 | current int64 |
| 79 | incrAmount int64 | |
| 74 | 80 | trimLeftSpace bool |
| 75 | 81 | trimRightSpace bool |
| 82 | completed bool | |
| 83 | startTime time.Time | |
| 76 | 84 | timeElapsed time.Duration |
| 77 | 85 | timePerItem time.Duration |
| 78 | 86 | appendFuncs []DecoratorFunc |
| 264 | 272 | } |
| 265 | 273 | |
| 266 | 274 | 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() | |
| 271 | 276 | 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, | |
| 277 | 284 | } |
| 278 | 285 | if total <= 0 { |
| 279 | 286 | barState.simpleSpinner = getSpinner() |
| 286 | 293 | }() |
| 287 | 294 | for { |
| 288 | 295 | 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 | |
| 292 | 298 | if total > 0 && n > total { |
| 293 | 299 | barState.current = total |
| 294 | completed = true | |
| 300 | barState.completed = true | |
| 295 | 301 | break // break out of select |
| 296 | 302 | } |
| 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) | |
| 299 | 305 | if n == total { |
| 300 | completed = true | |
| 306 | barState.completed = true | |
| 301 | 307 | } |
| 302 | 308 | barState.current = n |
| 303 | prevStartTime = blockStartTime | |
| 309 | incrStartTime = time.Now() | |
| 304 | 310 | case data := <-b.dCommandCh: |
| 305 | 311 | switch data.action { |
| 306 | 312 | case dAppend: |
| 321 | 327 | case barState.trimLeftSpace = <-b.trimLeftCh: |
| 322 | 328 | case barState.trimRightSpace = <-b.trimRightCh: |
| 323 | 329 | case <-b.flushedCh: |
| 324 | if completed { | |
| 330 | if barState.completed { | |
| 325 | 331 | return |
| 326 | 332 | } |
| 327 | 333 | case <-b.completeReqCh: |
| 354 | 360 | b.removeReqCh <- struct{}{} |
| 355 | 361 | } |
| 356 | 362 | |
| 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 | ||
| 367 | 363 | func (b *Bar) render(rFn func(chan []byte), termWidth int, prependWs, appendWs *widthSync) <-chan []byte { |
| 368 | 364 | ch := make(chan []byte) |
| 369 | 365 | |
| 376 | 372 | }() |
| 377 | 373 | |
| 378 | 374 | 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)) | |
| 379 | 391 | } |
| 380 | 392 | |
| 381 | 393 | func draw(s *state, termWidth int, prependWs, appendWs *widthSync) []byte { |
| 493 | 505 | |
| 494 | 506 | func newStatistics(s *state) *Statistics { |
| 495 | 507 | return &Statistics{ |
| 508 | Completed: s.completed, | |
| 496 | 509 | Total: s.total, |
| 497 | 510 | Current: s.current, |
| 511 | IncrAmount: s.incrAmount, | |
| 512 | StartTime: s.startTime, | |
| 498 | 513 | TimeElapsed: s.timeElapsed, |
| 499 | 514 | TimePerItemEstimate: s.timePerItem, |
| 500 | 515 | } |
| 508 | 523 | fmtBytes[i] = buf |
| 509 | 524 | } |
| 510 | 525 | 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)) | |
| 517 | 526 | } |
| 518 | 527 | |
| 519 | 528 | func percentage(total, current int64, ratio int) int { |