/
tracker.go
184 lines (160 loc) · 4.35 KB
/
tracker.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package progress
import (
"math"
"sync"
"time"
)
// Tracker helps track the progress of a single task. The way to use it is to
// instantiate a Tracker with a valid Message, a valid (expected) Total, and
// Units values. This should then be fed to the Progress Writer with the
// Writer.AppendTracker() method. When the task that is being done has progress,
// increment the value using the Tracker.Increment(value) method.
type Tracker struct {
// Message should contain a short description of the "task"
Message string
// ExpectedDuration tells how long this task is expected to take; and will
// be used in calculation of the ETA value
ExpectedDuration time.Duration
// Total should be set to the (expected) Total/Final value to be reached
Total int64
// Units defines the type of the "value" being tracked
Units Units
done bool
err bool
mutex sync.RWMutex
timeStart time.Time
timeStop time.Time
value int64
}
// ETA returns the expected time of "arrival" or completion of this tracker. It
// is an estimate and is not guaranteed.
func (t *Tracker) ETA() time.Duration {
t.mutex.RLock()
defer t.mutex.RUnlock()
timeTaken := time.Since(t.timeStart)
if t.ExpectedDuration > time.Duration(0) && t.ExpectedDuration > timeTaken {
return t.ExpectedDuration - timeTaken
}
pDone := int64(t.PercentDone())
if pDone == 0 {
return time.Duration(0)
}
return time.Duration((int64(timeTaken) / pDone) * (100 - pDone))
}
// Increment updates the current value of the task being tracked.
func (t *Tracker) Increment(value int64) {
t.mutex.Lock()
t.incrementWithoutLock(value)
t.mutex.Unlock()
}
// IsDone returns true if the tracker is done (value has reached the expected
// Total set during initialization).
func (t *Tracker) IsDone() bool {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.done
}
// IsErrored true if MarkAsErrored() was called before the tracker was done
func (t *Tracker) IsErrored() bool {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.err
}
// IsIndeterminate returns true if the tracker is indeterminate; i.e., the total
// is unknown and it is impossible to auto-calculate if tracking is done.
func (t *Tracker) IsIndeterminate() bool {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.Total == 0
}
// MarkAsDone forces completion of the tracker by updating the current value as
// the expected Total value.
func (t *Tracker) MarkAsDone() {
t.mutex.Lock()
t.Total = t.value
t.stop()
t.mutex.Unlock()
}
// MarkAsErrored forces completion of the tracker by updating the current value as
// the expected Total value, and recording as error. The error state can only be
// unset via Reset
func (t *Tracker) MarkAsErrored() {
t.mutex.Lock()
// only update error if not done and if not previously set
if !t.done {
t.Total = t.value
t.err = true
t.stop()
}
t.mutex.Unlock()
}
// PercentDone returns the currently completed percentage value.
func (t *Tracker) PercentDone() float64 {
t.mutex.RLock()
defer t.mutex.RUnlock()
if t.Total == 0 {
return 0
}
return float64(t.value) * 100.0 / float64(t.Total)
}
// Reset resets the tracker to its initial state.
func (t *Tracker) Reset() {
t.mutex.Lock()
t.done = false
t.err = false
t.timeStart = time.Time{}
t.timeStop = time.Time{}
t.value = 0
t.mutex.Unlock()
}
// SetValue sets the value of the tracker and re-calculates if the tracker is
// "done". The tracker will still be "done" if an error was previously set
func (t *Tracker) SetValue(value int64) {
t.mutex.Lock()
if !t.err {
t.done = false
}
t.timeStop = time.Time{}
t.value = 0
t.incrementWithoutLock(value)
t.mutex.Unlock()
}
// Value returns the current value of the tracker.
func (t *Tracker) Value() int64 {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.value
}
func (t *Tracker) valueAndTotal() (int64, int64) {
t.mutex.RLock()
value := t.value
total := t.Total
t.mutex.RUnlock()
return value, total
}
func (t *Tracker) incrementWithoutLock(value int64) {
if !t.done {
t.value += value
if t.Total > 0 && t.value >= t.Total {
t.stop()
}
}
}
func (t *Tracker) start() {
t.mutex.Lock()
if t.Total < 0 {
t.Total = math.MaxInt64
}
t.done = false
t.err = false
t.timeStart = time.Now()
t.mutex.Unlock()
}
// this must be called with the mutex held with a write lock
func (t *Tracker) stop() {
t.done = true
t.timeStop = time.Now()
if t.value > t.Total {
t.Total = t.value
}
}