Codebase list golang-github-vbauerster-mpb / cd426b2
TimeNormalizer option for average eta Vladimir Bauer 6 years ago
1 changed file(s) with 18 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
102102 //
103103 // `wcc` optional WC config
104104 func AverageETA(style TimeStyle, wcc ...WC) Decorator {
105 return NewAverageETA(style, time.Now(), wcc...)
105 return NewAverageETA(style, time.Now(), nil, wcc...)
106106 }
107107
108108 // NewAverageETA decorator with user provided start time.
111111 //
112112 // `startTime` start time
113113 //
114 // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer]
115 //
114116 // `wcc` optional WC config
115 func NewAverageETA(style TimeStyle, startTime time.Time, wcc ...WC) Decorator {
117 func NewAverageETA(style TimeStyle, startTime time.Time, normalizer TimeNormalizer, wcc ...WC) Decorator {
116118 var wc WC
117119 for _, widthConf := range wcc {
118120 wc = widthConf
119121 }
120122 wc.Init()
121123 d := &averageETA{
122 WC: wc,
123 startTime: startTime,
124 producer: chooseTimeProducer(style),
124 WC: wc,
125 startTime: startTime,
126 normalizer: normalizer,
127 producer: chooseTimeProducer(style),
125128 }
126129 return d
127130 }
129132 type averageETA struct {
130133 WC
131134 startTime time.Time
135 normalizer TimeNormalizer
132136 producer func(time.Duration) string
133137 completeMsg *string
134138 }
138142 return d.FormatMsg(*d.completeMsg)
139143 }
140144
141 timeElapsed := time.Since(d.startTime)
142 v := float64(timeElapsed) / float64(st.Current)
143 if math.IsInf(v, 0) || math.IsNaN(v) {
144 v = 0
145 }
146 remaining := time.Duration((st.Total - st.Current) * int64(math.Round(v)))
145 var remaining time.Duration
146 if st.Current != 0 {
147 durPerItem := float64(time.Since(d.startTime)) / float64(st.Current)
148 durPerItem = math.Round(durPerItem)
149 remaining = time.Duration((st.Total - st.Current) * int64(durPerItem))
150 if d.normalizer != nil {
151 remaining = d.normalizer.Normalize(remaining)
152 }
153 }
147154 return d.FormatMsg(d.producer(remaining))
148155 }
149156