-
Notifications
You must be signed in to change notification settings - Fork 10
/
decorators.go
163 lines (147 loc) · 2.9 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package getparty
import (
"fmt"
"math"
"sync"
"sync/atomic"
"time"
"github.com/VividCortex/ewma"
"github.com/vbauerster/mpb/v8/decor"
)
type message struct {
times uint
msg string
done chan struct{}
}
type msgGate struct {
msgCh chan *message
done chan struct{}
msgFlash func(*message)
}
func newMsgGate(quiet bool, prefix string, times uint) *msgGate {
gate := &msgGate{
msgCh: make(chan *message, 8),
done: make(chan struct{}),
msgFlash: func(msg *message) {
if msg.done != nil {
close(msg.done)
msg.done = nil
}
},
}
if !quiet {
sinkFlash := gate.msgFlash
gate.msgFlash = func(msg *message) {
msg.times = times
msg.msg = fmt.Sprintf("%s %s", prefix, msg.msg)
select {
case gate.msgCh <- msg:
case <-gate.done:
sinkFlash(msg)
}
}
}
return gate
}
func (g *msgGate) flash(msg string) {
g.msgFlash(&message{msg: msg})
}
func (g *msgGate) finalFlash(msg string) {
flushed := make(chan struct{})
g.msgFlash(&message{
msg: msg,
done: flushed,
})
<-flushed
}
type mainDecorator struct {
decor.WC
curTry *uint32
name string
format string
finalMsg bool
gate *msgGate
msg *message
}
func newMainDecorator(curTry *uint32, format, name string, gate *msgGate, wc decor.WC) decor.Decorator {
d := &mainDecorator{
WC: wc.Init(),
curTry: curTry,
name: name,
format: format,
gate: gate,
}
return d
}
func (d *mainDecorator) Shutdown() {
close(d.gate.done)
}
func (d *mainDecorator) Decor(stat decor.Statistics) string {
for d.msg == nil {
select {
case d.msg = <-d.gate.msgCh:
default:
name := d.name
if atomic.LoadUint32(&globTry) > 0 {
name = fmt.Sprintf("%s:R%02d", name, atomic.LoadUint32(d.curTry))
}
return d.FormatMsg(fmt.Sprintf(d.format, name, decor.SizeB1024(stat.Total)))
}
}
switch {
case d.finalMsg:
case d.msg.done != nil:
defer func() {
close(d.msg.done)
d.msg.done = nil
}()
d.finalMsg = true
case d.msg.times == 0, stat.Completed, stat.Aborted:
defer func() {
d.msg = nil
}()
}
d.msg.times--
return d.FormatMsg(d.msg.msg)
}
type peak struct {
decor.WC
format string
msg string
min float64
set bool
mean ewma.MovingAverage
once sync.Once
}
func newSpeedPeak(format string, wc decor.WC) decor.Decorator {
d := &peak{
WC: wc.Init(),
format: format,
mean: ewma.NewMovingAverage(17),
}
return d
}
func (s *peak) EwmaUpdate(n int64, dur time.Duration) {
s.mean.Add(float64(dur) / float64(n))
durPerByte := s.mean.Value()
if durPerByte == 0 {
return
}
if !s.set || durPerByte < s.min {
s.min = durPerByte
s.set = true
}
}
func (s *peak) onComplete() {
if s.min == 0 {
s.msg = "N/A"
} else {
s.msg = fmt.Sprintf(s.format, decor.FmtAsSpeed(decor.SizeB1024(math.Round(1e9/s.min))))
}
}
func (s *peak) Decor(stat decor.Statistics) string {
if stat.Completed {
s.once.Do(s.onComplete)
}
return s.FormatMsg(s.msg)
}