Codebase list golang-github-vbauerster-mpb / dc61f81
decor: update doc comments Vladimir Bauer 6 years ago
4 changed file(s) with 64 addition(s) and 97 deletion(s). Raw diff Collapse all Expand all
2222 }
2323
2424 // EwmaETA exponential-weighted-moving-average based ETA decorator.
25 //
26 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
27 //
28 // `age` ewma age
29 //
30 // `wcc` optional WC config
25 // Note that it's necessary to supply bar.Incr* methods with incremental
26 // work duration as second argument, in order for this decorator to
27 // work correctly. This decorator is a wrapper of MovingAverageETA.
3128 func EwmaETA(style TimeStyle, age float64, wcc ...WC) Decorator {
3229 return MovingAverageETA(style, ewma.NewMovingAverage(age), nil, wcc...)
3330 }
3633 //
3734 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
3835 //
39 // `average` available implementations of MovingAverage [ewma.MovingAverage|NewMedian|NewMedianEwma]
36 // `average` implementation of MovingAverage interface
4037 //
4138 // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer]
4239 //
88 // MovingAverage is the interface that computes a moving average over
99 // a time-series stream of numbers. The average may be over a window
1010 // or exponentially decaying.
11 type MovingAverage interface {
12 Add(float64)
13 Value() float64
14 Set(float64)
15 }
11 type MovingAverage = ewma.MovingAverage
1612
1713 type medianWindow [3]float64
1814
4137 func NewMedian() MovingAverage {
4238 return new(medianWindow)
4339 }
44
45 type medianEwma struct {
46 count uint
47 median MovingAverage
48 MovingAverage
49 }
50
51 func (s *medianEwma) Add(v float64) {
52 s.median.Add(v)
53 if s.count >= 2 {
54 s.MovingAverage.Add(s.median.Value())
55 }
56 s.count++
57 }
58
59 // NewMedianEwma is ewma based MovingAverage, which gets its values
60 // from median MovingAverage.
61 func NewMedianEwma(age ...float64) MovingAverage {
62 return &medianEwma{
63 MovingAverage: ewma.NewMovingAverage(age...),
64 median: NewMedian(),
65 }
66 }
4545 io.WriteString(st, res)
4646 }
4747
48 // Percentage returns percentage decorator.
49 //
50 // `wcc` optional WC config
48 // Percentage returns percentage decorator. It's a wrapper of NewPercentage.
5149 func Percentage(wcc ...WC) Decorator {
52 return PercentageFmt("% d", wcc...)
50 return NewPercentage("% d", wcc...)
5351 }
5452
55 // PercentageFmt percentage decorator with custom fmt.
56 // "%.1f" = "1.0%" or "% .1f" = "1.0 %"
57 // "%d" = "1%" or "% d" = "1 %"
58 func PercentageFmt(fmt string, wcc ...WC) Decorator {
53 // NewPercentage percentage decorator with custom fmt string.
54 //
55 // fmt examples:
56 //
57 // fmt="%.1f" output: "1.0%"
58 // fmt="% .1f" output: "1.0 %"
59 // fmt="%d" output: "1%"
60 // fmt="% d" output: "1 %"
61 //
62 func NewPercentage(fmt string, wcc ...WC) Decorator {
5963 var wc WC
6064 for _, widthConf := range wcc {
6165 wc = widthConf
118118 io.WriteString(st, res)
119119 }
120120
121 // EwmaSpeed exponential-weighted-moving-average based speed decorator,
122 // with dynamic unit measure adjustment.
123 //
124 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
125 //
126 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
127 //
128 // `age` ewma age
129 //
130 // `wcc` optional WC config
131 //
132 // unitFormat example if UnitKiB is chosen:
133 //
134 // "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
135 func EwmaSpeed(unit int, unitFormat string, age float64, wcc ...WC) Decorator {
136 return MovingAverageSpeed(unit, unitFormat, ewma.NewMovingAverage(age), wcc...)
121 // EwmaSpeed exponential-weighted-moving-average based speed decorator.
122 // Note that it's necessary to supply bar.Incr* methods with incremental
123 // work duration as second argument, in order for this decorator to
124 // work correctly. This decorator is a wrapper of MovingAverageSpeed.
125 func EwmaSpeed(unit int, fmt string, age float64, wcc ...WC) Decorator {
126 return MovingAverageSpeed(unit, fmt, ewma.NewMovingAverage(age), wcc...)
137127 }
138128
139129 // MovingAverageSpeed decorator relies on MovingAverage implementation
141131 //
142132 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
143133 //
144 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
134 // `fmt` printf compatible verb for value, like "%f" or "%d"
145135 //
146136 // `average` MovingAverage implementation
147137 //
148138 // `wcc` optional WC config
149 func MovingAverageSpeed(unit int, unitFormat string, average MovingAverage, wcc ...WC) Decorator {
139 //
140 // fmt examples:
141 //
142 // unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s"
143 // unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s"
144 // unit=UnitKB, fmt="%.1f" output: "1.0MB/s"
145 // unit=UnitKB, fmt="% .1f" output: "1.0 MB/s"
146 //
147 func MovingAverageSpeed(unit int, fmt string, average MovingAverage, wcc ...WC) Decorator {
150148 var wc WC
151149 for _, widthConf := range wcc {
152150 wc = widthConf
153151 }
154152 wc.Init()
155153 d := &movingAverageSpeed{
156 WC: wc,
157 unit: unit,
158 unitFormat: unitFormat,
159 average: average,
154 WC: wc,
155 unit: unit,
156 fmt: fmt,
157 average: average,
160158 }
161159 return d
162160 }
164162 type movingAverageSpeed struct {
165163 WC
166164 unit int
167 unitFormat string
165 fmt string
168166 average ewma.MovingAverage
169167 msg string
170168 completeMsg *string
181179 speed := d.average.Value()
182180 switch d.unit {
183181 case UnitKiB:
184 d.msg = fmt.Sprintf(d.unitFormat, SpeedKiB(speed))
182 d.msg = fmt.Sprintf(d.fmt, SpeedKiB(speed))
185183 case UnitKB:
186 d.msg = fmt.Sprintf(d.unitFormat, SpeedKB(speed))
187 default:
188 d.msg = fmt.Sprintf(d.unitFormat, speed)
184 d.msg = fmt.Sprintf(d.fmt, SpeedKB(speed))
185 default:
186 d.msg = fmt.Sprintf(d.fmt, speed)
189187 }
190188
191189 return d.FormatMsg(d.msg)
207205 d.completeMsg = &msg
208206 }
209207
210 // AverageSpeed decorator with dynamic unit measure adjustment.
211 //
212 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
213 //
214 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
215 //
216 // `wcc` optional WC config
217 //
218 // unitFormat example if UnitKiB is chosen:
219 //
220 // "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
221 func AverageSpeed(unit int, unitFormat string, wcc ...WC) Decorator {
222 return NewAverageSpeed(unit, unitFormat, time.Now(), wcc...)
208 // AverageSpeed decorator with dynamic unit measure adjustment. It's
209 // a wrapper of NewAverageSpeed.
210 func AverageSpeed(unit int, fmt string, wcc ...WC) Decorator {
211 return NewAverageSpeed(unit, fmt, time.Now(), wcc...)
223212 }
224213
225214 // NewAverageSpeed decorator with dynamic unit measure adjustment and
227216 //
228217 // `unit` one of [0|UnitKiB|UnitKB] zero for no unit
229218 //
230 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
219 // `fmt` printf compatible verb for value, like "%f" or "%d"
231220 //
232221 // `startTime` start time
233222 //
234223 // `wcc` optional WC config
235224 //
236 // unitFormat example if UnitKiB is chosen:
237 //
238 // "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
239 func NewAverageSpeed(unit int, unitFormat string, startTime time.Time, wcc ...WC) Decorator {
225 // fmt examples:
226 //
227 // unit=UnitKiB, fmt="%.1f" output: "1.0MiB/s"
228 // unit=UnitKiB, fmt="% .1f" output: "1.0 MiB/s"
229 // unit=UnitKB, fmt="%.1f" output: "1.0MB/s"
230 // unit=UnitKB, fmt="% .1f" output: "1.0 MB/s"
231 //
232 func NewAverageSpeed(unit int, fmt string, startTime time.Time, wcc ...WC) Decorator {
240233 var wc WC
241234 for _, widthConf := range wcc {
242235 wc = widthConf
243236 }
244237 wc.Init()
245238 d := &averageSpeed{
246 WC: wc,
247 unit: unit,
248 unitFormat: unitFormat,
249 startTime: startTime,
239 WC: wc,
240 unit: unit,
241 fmt: fmt,
242 startTime: startTime,
250243 }
251244 return d
252245 }
254247 type averageSpeed struct {
255248 WC
256249 unit int
257 unitFormat string
250 fmt string
258251 startTime time.Time
259252 msg string
260253 completeMsg *string
273266
274267 switch d.unit {
275268 case UnitKiB:
276 d.msg = fmt.Sprintf(d.unitFormat, SpeedKiB(speed))
269 d.msg = fmt.Sprintf(d.fmt, SpeedKiB(speed))
277270 case UnitKB:
278 d.msg = fmt.Sprintf(d.unitFormat, SpeedKB(speed))
279 default:
280 d.msg = fmt.Sprintf(d.unitFormat, speed)
271 d.msg = fmt.Sprintf(d.fmt, SpeedKB(speed))
272 default:
273 d.msg = fmt.Sprintf(d.fmt, speed)
281274 }
282275
283276 return d.FormatMsg(d.msg)