diff --git a/decor/eta.go b/decor/eta.go index ff911d8..b845c04 100644 --- a/decor/eta.go +++ b/decor/eta.go @@ -23,12 +23,9 @@ } // EwmaETA exponential-weighted-moving-average based ETA decorator. -// -// `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] -// -// `age` ewma age -// -// `wcc` optional WC config +// Note that it's necessary to supply bar.Incr* methods with incremental +// work duration as second argument, in order for this decorator to +// work correctly. This decorator is a wrapper of MovingAverageETA. func EwmaETA(style TimeStyle, age float64, wcc ...WC) Decorator { return MovingAverageETA(style, ewma.NewMovingAverage(age), nil, wcc...) } @@ -37,7 +34,7 @@ // // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] // -// `average` available implementations of MovingAverage [ewma.MovingAverage|NewMedian|NewMedianEwma] +// `average` implementation of MovingAverage interface // // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer] // diff --git a/decor/moving-average.go b/decor/moving-average.go index fcd2689..933b1f2 100644 --- a/decor/moving-average.go +++ b/decor/moving-average.go @@ -9,11 +9,7 @@ // MovingAverage is the interface that computes a moving average over // a time-series stream of numbers. The average may be over a window // or exponentially decaying. -type MovingAverage interface { - Add(float64) - Value() float64 - Set(float64) -} +type MovingAverage = ewma.MovingAverage type medianWindow [3]float64 @@ -42,26 +38,3 @@ func NewMedian() MovingAverage { return new(medianWindow) } - -type medianEwma struct { - count uint - median MovingAverage - MovingAverage -} - -func (s *medianEwma) Add(v float64) { - s.median.Add(v) - if s.count >= 2 { - s.MovingAverage.Add(s.median.Value()) - } - s.count++ -} - -// NewMedianEwma is ewma based MovingAverage, which gets its values -// from median MovingAverage. -func NewMedianEwma(age ...float64) MovingAverage { - return &medianEwma{ - MovingAverage: ewma.NewMovingAverage(age...), - median: NewMedian(), - } -} diff --git a/decor/percentage.go b/decor/percentage.go index 8930f50..38bacf3 100644 --- a/decor/percentage.go +++ b/decor/percentage.go @@ -46,17 +46,21 @@ io.WriteString(st, res) } -// Percentage returns percentage decorator. -// -// `wcc` optional WC config +// Percentage returns percentage decorator. It's a wrapper of NewPercentage. func Percentage(wcc ...WC) Decorator { - return PercentageFmt("% d", wcc...) + return NewPercentage("% d", wcc...) } -// PercentageFmt percentage decorator with custom fmt. -// "%.1f" = "1.0%" or "% .1f" = "1.0 %" -// "%d" = "1%" or "% d" = "1 %" -func PercentageFmt(fmt string, wcc ...WC) Decorator { +// NewPercentage percentage decorator with custom fmt string. +// +// fmt examples: +// +// fmt="%.1f" output: "1.0%" +// fmt="% .1f" output: "1.0 %" +// fmt="%d" output: "1%" +// fmt="% d" output: "1 %" +// +func NewPercentage(fmt string, wcc ...WC) Decorator { var wc WC for _, widthConf := range wcc { wc = widthConf diff --git a/decor/speed.go b/decor/speed.go index 1f24ebc..21b687d 100644 --- a/decor/speed.go +++ b/decor/speed.go @@ -119,22 +119,12 @@ io.WriteString(st, res) } -// EwmaSpeed exponential-weighted-moving-average based speed decorator, -// with dynamic unit measure adjustment. -// -// `unit` one of [0|UnitKiB|UnitKB] zero for no unit -// -// `unitFormat` printf compatible verb for value, like "%f" or "%d" -// -// `age` ewma age -// -// `wcc` optional WC config -// -// unitFormat example if UnitKiB is chosen: -// -// "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s" -func EwmaSpeed(unit int, unitFormat string, age float64, wcc ...WC) Decorator { - return MovingAverageSpeed(unit, unitFormat, ewma.NewMovingAverage(age), wcc...) +// EwmaSpeed exponential-weighted-moving-average based speed decorator. +// Note that it's necessary to supply bar.Incr* methods with incremental +// work duration as second argument, in order for this decorator to +// work correctly. This decorator is a wrapper of MovingAverageSpeed. +func EwmaSpeed(unit int, fmt string, age float64, wcc ...WC) Decorator { + return MovingAverageSpeed(unit, fmt, ewma.NewMovingAverage(age), wcc...) } // MovingAverageSpeed decorator relies on MovingAverage implementation @@ -142,22 +132,30 @@ // // `unit` one of [0|UnitKiB|UnitKB] zero for no unit // -// `unitFormat` printf compatible verb for value, like "%f" or "%d" +// `fmt` printf compatible verb for value, like "%f" or "%d" // // `average` MovingAverage implementation // // `wcc` optional WC config -func MovingAverageSpeed(unit int, unitFormat string, average MovingAverage, wcc ...WC) Decorator { +// +// fmt examples: +// +// unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s" +// unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s" +// unit=UnitKB, fmt="%.1f" output: "1.0MB/s" +// unit=UnitKB, fmt="% .1f" output: "1.0 MB/s" +// +func MovingAverageSpeed(unit int, fmt string, average MovingAverage, wcc ...WC) Decorator { var wc WC for _, widthConf := range wcc { wc = widthConf } wc.Init() d := &movingAverageSpeed{ - WC: wc, - unit: unit, - unitFormat: unitFormat, - average: average, + WC: wc, + unit: unit, + fmt: fmt, + average: average, } return d } @@ -165,7 +163,7 @@ type movingAverageSpeed struct { WC unit int - unitFormat string + fmt string average ewma.MovingAverage msg string completeMsg *string @@ -182,11 +180,11 @@ speed := d.average.Value() switch d.unit { case UnitKiB: - d.msg = fmt.Sprintf(d.unitFormat, SpeedKiB(speed)) + d.msg = fmt.Sprintf(d.fmt, SpeedKiB(speed)) case UnitKB: - d.msg = fmt.Sprintf(d.unitFormat, SpeedKB(speed)) - default: - d.msg = fmt.Sprintf(d.unitFormat, speed) + d.msg = fmt.Sprintf(d.fmt, SpeedKB(speed)) + default: + d.msg = fmt.Sprintf(d.fmt, speed) } return d.FormatMsg(d.msg) @@ -208,19 +206,10 @@ d.completeMsg = &msg } -// AverageSpeed decorator with dynamic unit measure adjustment. -// -// `unit` one of [0|UnitKiB|UnitKB] zero for no unit -// -// `unitFormat` printf compatible verb for value, like "%f" or "%d" -// -// `wcc` optional WC config -// -// unitFormat example if UnitKiB is chosen: -// -// "%.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...) +// AverageSpeed decorator with dynamic unit measure adjustment. It's +// a wrapper of NewAverageSpeed. +func AverageSpeed(unit int, fmt string, wcc ...WC) Decorator { + return NewAverageSpeed(unit, fmt, time.Now(), wcc...) } // NewAverageSpeed decorator with dynamic unit measure adjustment and @@ -228,26 +217,30 @@ // // `unit` one of [0|UnitKiB|UnitKB] zero for no unit // -// `unitFormat` printf compatible verb for value, like "%f" or "%d" +// `fmt` 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 { +// fmt examples: +// +// unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s" +// unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s" +// unit=UnitKB, fmt="%.1f" output: "1.0MB/s" +// unit=UnitKB, fmt="% .1f" output: "1.0 MB/s" +// +func NewAverageSpeed(unit int, fmt string, startTime time.Time, wcc ...WC) Decorator { var wc WC for _, widthConf := range wcc { wc = widthConf } wc.Init() d := &averageSpeed{ - WC: wc, - unit: unit, - unitFormat: unitFormat, - startTime: startTime, + WC: wc, + unit: unit, + fmt: fmt, + startTime: startTime, } return d } @@ -255,7 +248,7 @@ type averageSpeed struct { WC unit int - unitFormat string + fmt string startTime time.Time msg string completeMsg *string @@ -274,11 +267,11 @@ switch d.unit { case UnitKiB: - d.msg = fmt.Sprintf(d.unitFormat, SpeedKiB(speed)) + d.msg = fmt.Sprintf(d.fmt, SpeedKiB(speed)) case UnitKB: - d.msg = fmt.Sprintf(d.unitFormat, SpeedKB(speed)) - default: - d.msg = fmt.Sprintf(d.unitFormat, speed) + d.msg = fmt.Sprintf(d.fmt, SpeedKB(speed)) + default: + d.msg = fmt.Sprintf(d.fmt, speed) } return d.FormatMsg(d.msg)