Skip to content

Commit

Permalink
AdjustAverageDecorators for resume-able tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Jul 14, 2019
1 parent db5cf0f commit c15cd9d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 24 deletions.
35 changes: 34 additions & 1 deletion bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type bState struct {
noPop bool
aDecorators []decor.Decorator
pDecorators []decor.Decorator
mDecorators []decor.Decorator
amountReceivers []decor.AmountReceiver
shutdownListeners []decor.ShutdownListener
bufP, bufB, bufA *bytes.Buffer
Expand Down Expand Up @@ -170,14 +171,46 @@ func (b *Bar) Current() int64 {
}

// SetRefill sets refill, if supported by underlying Filler.
// Useful for resume-able tasks.
func (b *Bar) SetRefill(amount int64) {
type refiller interface {
SetRefill(int64)
}
b.operateState <- func(s *bState) {
if f, ok := s.filler.(interface{ SetRefill(int64) }); ok {
if f, ok := s.filler.(refiller); ok {
f.SetRefill(amount)
}
}
}

// AdjustAverageDecorators updates start time of all average decorators.
// Useful for resume-able tasks.
func (b *Bar) AdjustAverageDecorators(startTime time.Time) {
type adjustable interface {
AverageAdjust(time.Time)
}
b.UpdateDecorators(func(d decor.Decorator) {
if d, ok := d.(adjustable); ok {
d.AverageAdjust(startTime)
}
})
}

// UpdateDecorators general helper func.
func (b *Bar) UpdateDecorators(cb decor.UpdateFunc) {
b.operateState <- func(s *bState) {
for _, decorators := range [...][]decor.Decorator{
s.pDecorators,
s.aDecorators,
s.mDecorators,
} {
for _, d := range decorators {
cb(d)
}
}
}
}

// SetTotal sets total dynamically.
// Set complete to true, to trigger bar complete event now.
func (b *Bar) SetTotal(total int64, complete bool) {
Expand Down
25 changes: 5 additions & 20 deletions bar_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,15 @@ import (
// BarOption is a function option which changes the default behavior of a bar.
type BarOption func(*bState)

type merger interface {
CompoundDecorators() []decor.Decorator
}

func (s *bState) appendAmountReceiver(d decor.Decorator) {
if ar, ok := d.(decor.AmountReceiver); ok {
s.amountReceivers = append(s.amountReceivers, ar)
}
}

func (s *bState) appendShutdownListener(d decor.Decorator) {
if sl, ok := d.(decor.ShutdownListener); ok {
s.shutdownListeners = append(s.shutdownListeners, sl)
}
type mergeWrapper interface {
MergeUnwrap() []decor.Decorator
}

func (s *bState) addDecorators(dest *[]decor.Decorator, decorators ...decor.Decorator) {
for _, decorator := range decorators {
s.appendAmountReceiver(decorator)
s.appendShutdownListener(decorator)
if m, ok := decorator.(merger); ok {
dd := m.CompoundDecorators()
s.appendAmountReceiver(dd[0])
s.appendShutdownListener(dd[0])
if mw, ok := decorator.(mergeWrapper); ok {
dd := mw.MergeUnwrap()
s.mDecorators = append(s.mDecorators, dd[0])
*dest = append(*dest, dd[1:]...)
}
*dest = append(*dest, decorator)
Expand Down
3 changes: 3 additions & 0 deletions decor/decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type ShutdownListener interface {
Shutdown()
}

// UpdateFunc convenience func type
type UpdateFunc func(Decorator)

// Global convenience shortcuts
var (
WCSyncWidth = WC{C: DSyncWidth}
Expand Down
17 changes: 16 additions & 1 deletion decor/eta.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ func (d *movingAverageETA) OnCompleteMessage(msg string) {
//
// `wcc` optional WC config
func AverageETA(style TimeStyle, wcc ...WC) Decorator {
return NewAverageETA(style, time.Now(), wcc...)
}

// NewAverageETA decorator with user provided start time.
//
// `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
//
// `startTime` start time
//
// `wcc` optional WC config
func NewAverageETA(style TimeStyle, startTime time.Time, wcc ...WC) Decorator {
var wc WC
for _, widthConf := range wcc {
wc = widthConf
Expand All @@ -128,7 +139,7 @@ func AverageETA(style TimeStyle, wcc ...WC) Decorator {
d := &averageETA{
WC: wc,
style: style,
startTime: time.Now(),
startTime: startTime,
}
return d
}
Expand Down Expand Up @@ -178,6 +189,10 @@ func (d *averageETA) OnCompleteMessage(msg string) {
d.completeMsg = &msg
}

func (d *averageETA) AverageAdjust(startTime time.Time) {
d.startTime = startTime
}

func MaxTolerateTimeNormalizer(maxTolerate time.Duration) TimeNormalizer {
var normalized time.Duration
var lastCall time.Time
Expand Down
24 changes: 23 additions & 1 deletion decor/speed.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ func (d *movingAverageSpeed) OnCompleteMessage(msg string) {
//
// "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
func AverageSpeed(unit int, unitFormat string, wcc ...WC) Decorator {
return NewAverageSpeed(unit, unitFormat, time.Now(), wcc...)
}

// NewAverageSpeed decorator with dynamic unit measure adjustment and
// user provided start time.
//
// `unit` one of [0|UnitKiB|UnitKB] zero for no unit
//
// `unitFormat` printf compatible verb for value, like "%f" or "%d"
//
// `startTime` start time
//
// `wcc` optional WC config
//
// unitFormat example if UnitKiB is chosen:
//
// "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
func NewAverageSpeed(unit int, unitFormat string, startTime time.Time, wcc ...WC) Decorator {
var wc WC
for _, widthConf := range wcc {
wc = widthConf
Expand All @@ -229,7 +247,7 @@ func AverageSpeed(unit int, unitFormat string, wcc ...WC) Decorator {
WC: wc,
unit: unit,
unitFormat: unitFormat,
startTime: time.Now(),
startTime: startTime,
}
return d
}
Expand Down Expand Up @@ -269,3 +287,7 @@ func (d *averageSpeed) Decor(st *Statistics) string {
func (d *averageSpeed) OnCompleteMessage(msg string) {
d.completeMsg = &msg
}

func (d *averageSpeed) AverageAdjust(startTime time.Time) {
d.startTime = startTime
}
18 changes: 17 additions & 1 deletion progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/vbauerster/mpb/v4/cwriter"
"github.com/vbauerster/mpb/v4/decor"
)

const (
Expand Down Expand Up @@ -143,7 +144,22 @@ func (p *Progress) Add(total int64, filler Filler, options ...BarOption) *Bar {
ps.idCount++
result <- bar
}:
return <-result
var amountReceivers []decor.AmountReceiver
var shutdownListeners []decor.ShutdownListener
bar := <-result
bar.UpdateDecorators(func(d decor.Decorator) {
if d, ok := d.(decor.AmountReceiver); ok {
amountReceivers = append(amountReceivers, d)
}
if d, ok := d.(decor.ShutdownListener); ok {
shutdownListeners = append(shutdownListeners, d)
}
})
bar.operateState <- func(s *bState) {
s.amountReceivers = amountReceivers
s.shutdownListeners = shutdownListeners
}
return bar
case <-p.done:
p.bwg.Done()
return nil
Expand Down

0 comments on commit c15cd9d

Please sign in to comment.