@@ -12,17 +12,46 @@ import (
12
12
13
13
// Progress between done items and total items.
14
14
type Progress struct {
15
+ Name string
15
16
Done int64
16
17
Total int64
17
18
}
18
19
19
- func (p Progress ) String () string {
20
+ func (p Progress ) totalString () string {
20
21
var total = "?"
21
22
if p .Total > 0 {
22
23
total = fmt .Sprint (p .Total )
23
24
}
25
+ return total
26
+ }
27
+
28
+ // TableProgress keeps track of a table progress, and for each of its partitions
29
+ type TableProgress struct {
30
+ Progress
31
+ PartitionsProgress map [string ]PartitionProgress
32
+ }
33
+
34
+ func NewTableProgress (name string , total int64 ) TableProgress {
35
+ return TableProgress {
36
+ Progress : Progress {
37
+ Name : name ,
38
+ Total : total ,
39
+ },
40
+ PartitionsProgress : make (map [string ]PartitionProgress ),
41
+ }
42
+ }
43
+
44
+ func (p TableProgress ) String () string {
45
+ return fmt .Sprintf ("%s (%d/%s partitions)" , p .Name , p .Done , p .totalString ())
46
+ }
24
47
25
- return fmt .Sprintf ("%d/%s" , p .Done , total )
48
+ // PartitionProgress keeps track of a partition progress
49
+ type PartitionProgress struct {
50
+ Progress
51
+ }
52
+
53
+ func (p PartitionProgress ) String () string {
54
+ return fmt .Sprintf ("%s (%d/%s rows)" , p .Name , p .Done , p .totalString ())
26
55
}
27
56
28
57
// ProcessType is the type of process.
@@ -53,7 +82,7 @@ type Process struct {
53
82
User string
54
83
Type ProcessType
55
84
Query string
56
- Progress map [string ]Progress
85
+ Progress map [string ]TableProgress
57
86
StartedAt time.Time
58
87
Kill context.CancelFunc
59
88
}
@@ -108,7 +137,7 @@ func (pl *ProcessList) AddProcess(
108
137
Connection : ctx .ID (),
109
138
Type : typ ,
110
139
Query : query ,
111
- Progress : make (map [string ]Progress ),
140
+ Progress : make (map [string ]TableProgress ),
112
141
User : ctx .Session .Client ().User ,
113
142
StartedAt : time .Now (),
114
143
Kill : cancel ,
@@ -117,9 +146,9 @@ func (pl *ProcessList) AddProcess(
117
146
return ctx , nil
118
147
}
119
148
120
- // UpdateProgress updates the progress of the item with the given name for the
149
+ // UpdateTableProgress updates the progress of the table with the given name for the
121
150
// process with the given pid.
122
- func (pl * ProcessList ) UpdateProgress (pid uint64 , name string , delta int64 ) {
151
+ func (pl * ProcessList ) UpdateTableProgress (pid uint64 , name string , delta int64 ) {
123
152
pl .mu .Lock ()
124
153
defer pl .mu .Unlock ()
125
154
@@ -130,16 +159,41 @@ func (pl *ProcessList) UpdateProgress(pid uint64, name string, delta int64) {
130
159
131
160
progress , ok := p .Progress [name ]
132
161
if ! ok {
133
- progress = Progress { Total : - 1 }
162
+ progress = NewTableProgress ( name , - 1 )
134
163
}
135
164
136
165
progress .Done += delta
137
166
p .Progress [name ] = progress
138
167
}
139
168
140
- // AddProgressItem adds a new item to track progress from to the process with
169
+ // UpdatePartitionProgress updates the progress of the table partition with the
170
+ // given name for the process with the given pid.
171
+ func (pl * ProcessList ) UpdatePartitionProgress (pid uint64 , tableName , partitionName string , delta int64 ) {
172
+ pl .mu .Lock ()
173
+ defer pl .mu .Unlock ()
174
+
175
+ p , ok := pl .procs [pid ]
176
+ if ! ok {
177
+ return
178
+ }
179
+
180
+ tablePg , ok := p .Progress [tableName ]
181
+ if ! ok {
182
+ return
183
+ }
184
+
185
+ partitionPg , ok := tablePg .PartitionsProgress [partitionName ]
186
+ if ! ok {
187
+ partitionPg = PartitionProgress {Progress : Progress {Name : partitionName , Total : - 1 }}
188
+ }
189
+
190
+ partitionPg .Done += delta
191
+ tablePg .PartitionsProgress [partitionName ] = partitionPg
192
+ }
193
+
194
+ // AddTableProgress adds a new item to track progress from to the process with
141
195
// the given pid. If the pid does not exist, it will do nothing.
142
- func (pl * ProcessList ) AddProgressItem (pid uint64 , name string , total int64 ) {
196
+ func (pl * ProcessList ) AddTableProgress (pid uint64 , name string , total int64 ) {
143
197
pl .mu .Lock ()
144
198
defer pl .mu .Unlock ()
145
199
@@ -152,13 +206,38 @@ func (pl *ProcessList) AddProgressItem(pid uint64, name string, total int64) {
152
206
pg .Total = total
153
207
p .Progress [name ] = pg
154
208
} else {
155
- p .Progress [name ] = Progress { Total : total }
209
+ p .Progress [name ] = NewTableProgress ( name , total )
156
210
}
157
211
}
158
212
159
- // RemoveProgressItem removes an existing item tracking progress from the
213
+ // AddPartitionProgress adds a new item to track progress from to the process with
214
+ // the given pid. If the pid or the table does not exist, it will do nothing.
215
+ func (pl * ProcessList ) AddPartitionProgress (pid uint64 , tableName , partitionName string , total int64 ) {
216
+ pl .mu .Lock ()
217
+ defer pl .mu .Unlock ()
218
+
219
+ p , ok := pl .procs [pid ]
220
+ if ! ok {
221
+ return
222
+ }
223
+
224
+ tablePg , ok := p .Progress [tableName ]
225
+ if ! ok {
226
+ return
227
+ }
228
+
229
+ if pg , ok := tablePg .PartitionsProgress [partitionName ]; ok {
230
+ pg .Total = total
231
+ tablePg .PartitionsProgress [partitionName ] = pg
232
+ } else {
233
+ tablePg .PartitionsProgress [partitionName ] =
234
+ PartitionProgress {Progress : Progress {Name : partitionName , Total : total }}
235
+ }
236
+ }
237
+
238
+ // RemoveTableProgress removes an existing item tracking progress from the
160
239
// process with the given pid, if it exists.
161
- func (pl * ProcessList ) RemoveProgressItem (pid uint64 , name string ) {
240
+ func (pl * ProcessList ) RemoveTableProgress (pid uint64 , name string ) {
162
241
pl .mu .Lock ()
163
242
defer pl .mu .Unlock ()
164
243
@@ -170,6 +249,25 @@ func (pl *ProcessList) RemoveProgressItem(pid uint64, name string) {
170
249
delete (p .Progress , name )
171
250
}
172
251
252
+ // RemovePartitionProgress removes an existing item tracking progress from the
253
+ // process with the given pid, if it exists.
254
+ func (pl * ProcessList ) RemovePartitionProgress (pid uint64 , tableName , partitionName string ) {
255
+ pl .mu .Lock ()
256
+ defer pl .mu .Unlock ()
257
+
258
+ p , ok := pl .procs [pid ]
259
+ if ! ok {
260
+ return
261
+ }
262
+
263
+ tablePg , ok := p .Progress [tableName ]
264
+ if ! ok {
265
+ return
266
+ }
267
+
268
+ delete (tablePg .PartitionsProgress , partitionName )
269
+ }
270
+
173
271
// Kill terminates all queries for a given connection id.
174
272
func (pl * ProcessList ) Kill (connID uint32 ) {
175
273
pl .mu .Lock ()
@@ -220,7 +318,7 @@ func (pl *ProcessList) Processes() []Process {
220
318
221
319
for _ , proc := range pl .procs {
222
320
p := * proc
223
- var progress = make (map [string ]Progress , len (p .Progress ))
321
+ var progress = make (map [string ]TableProgress , len (p .Progress ))
224
322
for n , p := range p .Progress {
225
323
progress [n ] = p
226
324
}
0 commit comments