-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecorators.go
138 lines (124 loc) · 2.61 KB
/
decorators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package getparty
import (
"fmt"
"math"
"sync/atomic"
"time"
"github.com/VividCortex/ewma"
"github.com/vbauerster/mpb/v8/decor"
)
var (
_ decor.Decorator = (*mainDecorator)(nil)
_ decor.Decorator = (*flashDecorator)(nil)
_ decor.Wrapper = (*flashDecorator)(nil)
_ decor.Decorator = (*peak)(nil)
_ decor.EwmaDecorator = (*peak)(nil)
)
func newFlashDecorator(decorator decor.Decorator, msgCh <-chan string, limit uint) decor.Decorator {
if decorator == nil {
return nil
}
d := &flashDecorator{
Decorator: decorator,
msgCh: msgCh,
limit: limit,
}
return d
}
type flashDecorator struct {
decor.Decorator
msgCh <-chan string
limit uint
count uint
msg string
}
func (d *flashDecorator) Unwrap() decor.Decorator {
return d.Decorator
}
func (d *flashDecorator) Decor(stat decor.Statistics) (string, int) {
if d.count == 0 {
select {
case msg := <-d.msgCh:
d.count = d.limit
d.msg = msg
default:
return d.Decorator.Decor(stat)
}
} else {
d.count--
}
return d.Format(d.msg)
}
type mainDecorator struct {
decor.WC
name string
format string
curTry *uint32
}
func newMainDecorator(curTry *uint32, name, format string, wc decor.WC) decor.Decorator {
d := &mainDecorator{
WC: wc.Init(),
name: name,
format: format,
curTry: curTry,
}
return d
}
func (d *mainDecorator) Decor(stat decor.Statistics) (string, int) {
var name string
if atomic.LoadUint32(&globTry) != 0 {
name = fmt.Sprintf("%s:R%02d", d.name, atomic.LoadUint32(d.curTry))
} else {
name = d.name
}
return d.Format(fmt.Sprintf(d.format, name, decor.SizeB1024(stat.Total)))
}
type peak struct {
decor.WC
format string
msg string
min float64
updCount uint8
zDur time.Duration
mean ewma.MovingAverage
}
func newSpeedPeak(format string, wc decor.WC) decor.Decorator {
d := &peak{
WC: wc.Init(),
format: format,
mean: ewma.NewMovingAverage(30),
}
return d
}
func (d *peak) EwmaUpdate(n int64, dur time.Duration) {
if n <= 0 {
d.zDur += dur
return
}
durPerByte := float64(d.zDur+dur) / float64(n)
if math.IsInf(durPerByte, 0) || math.IsNaN(durPerByte) {
d.zDur += dur
return
}
d.zDur = 0
d.mean.Add(durPerByte)
switch d.updCount {
case ewma.WARMUP_SAMPLES:
durPerByte = d.mean.Value()
if d.min == 0 || durPerByte < d.min {
d.min = durPerByte
}
default:
d.updCount++
}
}
func (d *peak) Decor(stat decor.Statistics) (string, int) {
if stat.Completed && d.msg == "" {
if d.min == 0 {
d.msg = "N/A"
} else {
d.msg = fmt.Sprintf(d.format, decor.FmtAsSpeed(decor.SizeB1024(math.Round(1e9/d.min))))
}
}
return d.Format(d.msg)
}