Codebase list golang-github-vbauerster-mpb / 69a46ee
TimeNormalizer Vladimir Bauer 7 years ago
1 changed file(s) with 52 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
88 "github.com/vbauerster/mpb/internal"
99 )
1010
11 type TimeNormalizer func(time.Duration) time.Duration
12
1113 // EwmaETA exponential-weighted-moving-average based ETA decorator.
1214 //
1315 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
1618 //
1719 // `wcc` optional WC config
1820 func EwmaETA(style int, age float64, wcc ...WC) Decorator {
19 return MovingAverageETA(style, ewma.NewMovingAverage(age), wcc...)
21 return MovingAverageETA(style, ewma.NewMovingAverage(age), NopTimeNormalizer(), wcc...)
2022 }
2123
2224 // MovingAverageETA decorator relies on MovingAverage implementation to calculate its average.
2628 // `average` MovingAverage implementation
2729 //
2830 // `wcc` optional WC config
29 func MovingAverageETA(style int, average MovingAverage, wcc ...WC) Decorator {
31 func MovingAverageETA(style int, average MovingAverage, normalizer TimeNormalizer, wcc ...WC) Decorator {
3032 var wc WC
3133 for _, widthConf := range wcc {
3234 wc = widthConf
3335 }
3436 wc.Init()
3537 d := &movingAverageETA{
36 WC: wc,
37 style: style,
38 average: average,
38 WC: wc,
39 style: style,
40 average: average,
41 normalizer: normalizer,
3942 }
4043 return d
4144 }
4548 style int
4649 average ewma.MovingAverage
4750 completeMsg *string
51 normalizer TimeNormalizer
4852 }
4953
5054 func (d *movingAverageETA) Decor(st *Statistics) string {
5660 if math.IsInf(v, 0) || math.IsNaN(v) {
5761 v = 0
5862 }
59 remaining := time.Duration((st.Total - st.Current) * int64(v))
63 remaining := d.normalizer(time.Duration((st.Total - st.Current) * int64(v)))
6064 hours := int64((remaining / time.Hour) % 60)
6165 minutes := int64((remaining / time.Minute) % 60)
6266 seconds := int64((remaining / time.Second) % 60)
148152 func (d *averageETA) OnCompleteMessage(msg string) {
149153 d.completeMsg = &msg
150154 }
155
156 func MaxTolerateTimeNormalizer(maxTolerate time.Duration) TimeNormalizer {
157 var normalized time.Duration
158 var lastCall time.Time
159 return func(remaining time.Duration) time.Duration {
160 if diff := normalized - remaining; diff <= 0 || diff >= maxTolerate || remaining <= maxTolerate {
161 normalized = remaining
162 lastCall = time.Now()
163 return remaining
164 }
165 normalized -= time.Since(lastCall)
166 lastCall = time.Now()
167 return normalized
168 }
169 }
170
171 func FixedIntervalTimeNormalizer(updInterval int) TimeNormalizer {
172 var normalized time.Duration
173 var lastCall time.Time
174 var count int
175 return func(remaining time.Duration) time.Duration {
176 if count == 0 || remaining <= time.Duration(15*time.Second) {
177 count = updInterval
178 normalized = remaining
179 lastCall = time.Now()
180 return remaining
181 }
182 count--
183 normalized -= time.Since(lastCall)
184 lastCall = time.Now()
185 if normalized > 0 {
186 return normalized
187 }
188 return remaining
189 }
190 }
191
192 func NopTimeNormalizer() TimeNormalizer {
193 return func(remaining time.Duration) time.Duration {
194 return remaining
195 }
196 }