correct Decor methods
need to comply with Decorator interface
Vladimir Bauer
2 years ago
| 67 | 67 |
producer func(time.Duration) string
|
| 68 | 68 |
}
|
| 69 | 69 |
|
| 70 | |
func (d *movingAverageETA) Decor(s Statistics) string {
|
|
70 |
func (d *movingAverageETA) Decor(s Statistics) (string, int) {
|
| 71 | 71 |
v := math.Round(d.average.Value())
|
| 72 | 72 |
remaining := time.Duration((s.Total - s.Current) * int64(v))
|
| 73 | 73 |
if d.normalizer != nil {
|
| 74 | 74 |
remaining = d.normalizer.Normalize(remaining)
|
| 75 | 75 |
}
|
| 76 | |
return d.FormatMsg(d.producer(remaining))
|
|
76 |
return d.Format(d.producer(remaining))
|
| 77 | 77 |
}
|
| 78 | 78 |
|
| 79 | 79 |
func (d *movingAverageETA) EwmaUpdate(n int64, dur time.Duration) {
|
|
| 119 | 119 |
producer func(time.Duration) string
|
| 120 | 120 |
}
|
| 121 | 121 |
|
| 122 | |
func (d *averageETA) Decor(s Statistics) string {
|
|
122 |
func (d *averageETA) Decor(s Statistics) (string, int) {
|
| 123 | 123 |
var remaining time.Duration
|
| 124 | 124 |
if s.Current != 0 {
|
| 125 | 125 |
durPerItem := float64(time.Since(d.startTime)) / float64(s.Current)
|
|
| 129 | 129 |
remaining = d.normalizer.Normalize(remaining)
|
| 130 | 130 |
}
|
| 131 | 131 |
}
|
| 132 | |
return d.FormatMsg(d.producer(remaining))
|
|
132 |
return d.Format(d.producer(remaining))
|
| 133 | 133 |
}
|
| 134 | 134 |
|
| 135 | 135 |
func (d *averageETA) AverageAdjust(startTime time.Time) {
|
| 81 | 81 |
msg string
|
| 82 | 82 |
}
|
| 83 | 83 |
|
| 84 | |
func (d *movingAverageSpeed) Decor(s Statistics) string {
|
|
84 |
func (d *movingAverageSpeed) Decor(s Statistics) (string, int) {
|
| 85 | 85 |
if !s.Completed {
|
| 86 | 86 |
var speed float64
|
| 87 | 87 |
if v := d.average.Value(); v > 0 {
|
|
| 89 | 89 |
}
|
| 90 | 90 |
d.msg = d.producer(speed * 1e9)
|
| 91 | 91 |
}
|
| 92 | |
return d.FormatMsg(d.msg)
|
|
92 |
return d.Format(d.msg)
|
| 93 | 93 |
}
|
| 94 | 94 |
|
| 95 | 95 |
func (d *movingAverageSpeed) EwmaUpdate(n int64, dur time.Duration) {
|
|
| 139 | 139 |
msg string
|
| 140 | 140 |
}
|
| 141 | 141 |
|
| 142 | |
func (d *averageSpeed) Decor(s Statistics) string {
|
|
142 |
func (d *averageSpeed) Decor(s Statistics) (string, int) {
|
| 143 | 143 |
if !s.Completed {
|
| 144 | 144 |
speed := float64(s.Current) / float64(time.Since(d.startTime))
|
| 145 | 145 |
d.msg = d.producer(speed * 1e9)
|
| 146 | 146 |
}
|
| 147 | |
return d.FormatMsg(d.msg)
|
|
147 |
return d.Format(d.msg)
|
| 148 | 148 |
}
|
| 149 | 149 |
|
| 150 | 150 |
func (d *averageSpeed) AverageAdjust(startTime time.Time) {
|
| 132 | 132 |
stat := Statistics{
|
| 133 | 133 |
Current: tc.current,
|
| 134 | 134 |
}
|
| 135 | |
res := decor.Decor(stat)
|
|
135 |
res, _ := decor.Decor(stat)
|
| 136 | 136 |
if res != tc.expected {
|
| 137 | 137 |
t.Fatalf("expected: %q, got: %q\n", tc.expected, res)
|
| 138 | 138 |
}
|
|
| 268 | 268 |
stat := Statistics{
|
| 269 | 269 |
Current: tc.current,
|
| 270 | 270 |
}
|
| 271 | |
res := decor.Decor(stat)
|
|
271 |
res, _ := decor.Decor(stat)
|
| 272 | 272 |
if res != tc.expected {
|
| 273 | 273 |
t.Fatalf("expected: %q, got: %q\n", tc.expected, res)
|
| 274 | 274 |
}
|
| 30 | 30 |
}
|
| 31 | 31 |
|
| 32 | 32 |
for _, test := range tests {
|
| 33 | |
got := test.decorator.Decor(decor.Statistics{})
|
|
33 |
got, _ := test.decorator.Decor(decor.Statistics{})
|
| 34 | 34 |
if got != test.want {
|
| 35 | 35 |
t.Errorf("Want: %q, Got: %q\n", test.want, got)
|
| 36 | 36 |
}
|
|
| 187 | 187 |
step := step
|
| 188 | 188 |
ch := make(chan string)
|
| 189 | 189 |
go func() {
|
| 190 | |
ch <- step.decorator.Decor(step.stat)
|
|
190 |
str, _ := step.decorator.Decor(step.stat)
|
|
191 |
ch <- str
|
| 191 | 192 |
}()
|
| 192 | 193 |
results = append(results, ch)
|
| 193 | 194 |
}
|