-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtask_exec_tui.go
More file actions
529 lines (425 loc) · 13 KB
/
task_exec_tui.go
File metadata and controls
529 lines (425 loc) · 13 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package ui
import (
"context"
"fmt"
"slices"
"strings"
"sync"
"time"
"github.com/sourcegraph/go-diff/diff"
"github.com/sourcegraph/src-cli/internal/batches/executor"
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
"github.com/sourcegraph/sourcegraph/lib/batches/git"
"github.com/sourcegraph/sourcegraph/lib/output"
)
type taskStatus struct {
displayName string
startedAt time.Time
finishedAt time.Time
currentlyExecuting string
// err is set if executing the Task lead to an error.
err error
}
func (ts *taskStatus) FinishedExecution() bool {
return !ts.startedAt.IsZero() && !ts.finishedAt.IsZero()
}
func (ts *taskStatus) ExecutionTime() time.Duration {
return ts.finishedAt.Sub(ts.startedAt).Truncate(time.Millisecond)
}
func (ts *taskStatus) String() string {
var statusText string
if ts.FinishedExecution() {
if ts.err != nil {
if texter, ok := ts.err.(statusTexter); ok {
statusText = texter.StatusText()
} else {
statusText = ts.err.Error()
}
} else {
statusText = "Done!"
}
} else {
if ts.currentlyExecuting != "" {
lines := strings.Split(ts.currentlyExecuting, "\n")
escapedLine := strings.ReplaceAll(lines[0], "%", "%%")
if len(lines) > 1 {
statusText = fmt.Sprintf("%s ...", escapedLine)
} else {
statusText = escapedLine
}
} else {
statusText = "..."
}
}
return statusText
}
type clock func() time.Time
var defaultClock = time.Now
func newTaskExecTUI(out *output.Output, verbose bool, numParallelism int) *taskExecTUI {
return &taskExecTUI{
out: out,
verbose: verbose,
numParallelism: numParallelism,
clock: defaultClock,
statuses: map[*executor.Task]*taskStatus{},
statusBars: map[int]*taskStatus{},
}
}
type taskExecTUI struct {
// Used in tests only
forceNoSpinner bool
out *output.Output
verbose bool
progress output.ProgressWithStatusBars
numStatusBars int
maxRepoName int
numParallelism int
mu sync.Mutex
clock clock
statuses map[*executor.Task]*taskStatus
statusBars map[int]*taskStatus
finished int
errored int
}
var _ executor.TaskExecutionUI = &taskExecTUI{}
func (ui *taskExecTUI) Start(tasks []*executor.Task) {
for _, t := range tasks {
status := &taskStatus{}
if t.Path != "" {
status.displayName = t.Repository.Name + ":" + t.Path
} else {
status.displayName = t.Repository.Name
}
if len(status.displayName) > ui.maxRepoName {
ui.maxRepoName = len(status.displayName)
}
ui.statuses[t] = status
}
ui.numStatusBars = min(len(tasks), ui.numParallelism)
statusBars := make([]*output.StatusBar, 0, ui.numStatusBars)
for i := 0; i < ui.numStatusBars; i++ {
statusBars = append(statusBars, output.NewStatusBar())
}
progressBars := []output.ProgressBar{
{
Label: fmt.Sprintf("Executing... (0/%d, 0 errored)", len(tasks)),
Max: float64(len(tasks)),
},
}
opts := output.DefaultProgressTTYOpts.WithNoSpinner(ui.forceNoSpinner)
ui.progress = ui.out.ProgressWithStatusBars(progressBars, statusBars, opts)
}
func (ui *taskExecTUI) Success() {
ui.progress.Complete()
}
func (ui *taskExecTUI) Failed(err error) {
// noop right now
}
func (ui *taskExecTUI) useFreeStatusBar(ts *taskStatus) (bar int, found bool) {
for i := 0; i < ui.numStatusBars; i++ {
if _, ok := ui.statusBars[i]; !ok {
ui.statusBars[i] = ts
bar = i
found = true
return bar, found
}
}
return bar, found
}
func (ui *taskExecTUI) findStatusBar(ts *taskStatus) (bar int, found bool) {
for i := 0; i < ui.numStatusBars; i++ {
if status, ok := ui.statusBars[i]; ok {
if ts == status {
bar = i
found = true
return bar, found
}
}
}
return bar, found
}
func (ui *taskExecTUI) TaskStarted(task *executor.Task) {
ui.mu.Lock()
defer ui.mu.Unlock()
ts, ok := ui.statuses[task]
if !ok {
ui.out.Verbose("warning: task not found in internal 'statuses'")
return
}
ts.startedAt = ui.clock()
// Find free slot
bar, found := ui.useFreeStatusBar(ts)
if !found {
ui.out.Verbose("warning: no free status bar found to display task status")
return
}
ui.progress.StatusBarResetf(bar, ts.displayName, ts.String())
}
func (ui *taskExecTUI) TaskCurrentlyExecuting(task *executor.Task, message string) {
ui.mu.Lock()
defer ui.mu.Unlock()
ts, ok := ui.statuses[task]
if !ok {
ui.out.Verbose("warning: task not found in internal 'statuses'")
return
}
ts.currentlyExecuting = message
bar, found := ui.findStatusBar(ts)
if !found {
ui.out.Verbose("warning: no free status bar found to display task status")
return
}
ui.progress.StatusBarUpdatef(bar, ts.String())
}
func (ui *taskExecTUI) StepsExecutionUI(task *executor.Task) executor.StepsExecutionUI {
ui.mu.Lock()
defer ui.mu.Unlock()
ts, ok := ui.statuses[task]
if !ok {
ui.out.Verbose("warning: task not found in internal 'statuses'")
return executor.NoopStepsExecUI{}
}
bar, found := ui.findStatusBar(ts)
if !found {
ui.out.Verbose("warning: no free status bar found to display task status")
return executor.NoopStepsExecUI{}
}
return &stepsExecTUI{
out: ui.out,
task: task,
updateStatusBar: func(message string) {
ts.currentlyExecuting = message
ui.progress.StatusBarUpdatef(bar, ts.String())
},
}
}
func (ui *taskExecTUI) TaskFinished(task *executor.Task, err error) {
ui.mu.Lock()
defer ui.mu.Unlock()
ts, ok := ui.statuses[task]
if !ok {
ui.out.Verbose("warning: task not found in internal 'statuses'")
return
}
ts.finishedAt = ui.clock()
ts.err = err
ui.finished += 1
if ts.err != nil {
ui.errored += 1
}
ui.updateProgressBar(ui.finished, ui.errored, len(ui.statuses))
bar, found := ui.findStatusBar(ts)
if !found {
ui.out.Verbose("warning: no free status bar found to display task status")
return
}
if ts.err != nil {
ui.progress.StatusBarFailf(bar, ts.String())
} else {
ui.progress.StatusBarCompletef(bar, ts.String())
}
delete(ui.statusBars, bar)
}
func (ui *taskExecTUI) TaskChangesetSpecsBuilt(task *executor.Task, specs []*batcheslib.ChangesetSpec) {
if !ui.verbose {
return
}
ui.mu.Lock()
defer ui.mu.Unlock()
ts, ok := ui.statuses[task]
if !ok {
ui.out.Verbose("warning: task not found in internal 'statuses'")
return
}
var fileDiffs []*diff.FileDiff
for _, spec := range specs {
fd, err := diff.ParseMultiFileDiff(spec.Commits[0].Diff)
if err != nil {
ui.progress.Verbosef("%-*s failed to display status: %s", ui.maxRepoName, ts.displayName, err)
return
}
fileDiffs = append(fileDiffs, fd...)
}
ui.progress.VerboseLine(output.Linef("", output.StylePending, "%s", ts.displayName))
if len(fileDiffs) == 0 {
ui.progress.Verbosef(" No changes")
} else {
lines, err := verboseDiffSummary(fileDiffs)
if err != nil {
ui.progress.Verbosef("%-*s failed to display status: %s", ui.maxRepoName, ts.displayName, err)
return
}
for _, line := range lines {
ui.progress.Verbose(line)
}
}
if len(specs) > 1 {
ui.progress.Verbosef(" %d changeset specs generated", len(specs))
}
ui.progress.Verbosef(" Execution took %s", ts.ExecutionTime())
ui.progress.Verbose("")
}
func (ui *taskExecTUI) updateProgressBar(completed, errored, total int) {
ui.progress.SetValue(0, float64(completed))
label := fmt.Sprintf("Executing... (%d/%d, %d errored)", completed, total, errored)
ui.progress.SetLabelAndRecalc(0, label)
}
type statusTexter interface {
StatusText() string
}
func verboseDiffSummary(fileDiffs []*diff.FileDiff) ([]string, error) {
var (
lines []string
maxFilenameLen int
sumInsertions int
sumDeletions int
)
fileStats := make(map[string]string, len(fileDiffs))
fileNames := make([]string, len(fileDiffs))
for i, f := range fileDiffs {
name := diffDisplayName(f)
fileNames[i] = name
if len(name) > maxFilenameLen {
maxFilenameLen = len(name)
}
stat := f.Stat()
sumInsertions += int(stat.Added) + int(stat.Changed)
sumDeletions += int(stat.Deleted) + int(stat.Changed)
num := stat.Added + 2*stat.Changed + stat.Deleted
fileStats[name] = fmt.Sprintf("%d %s", num, diffStatDiagram(stat))
}
slices.Sort(fileNames)
for _, name := range fileNames {
stats := fileStats[name]
lines = append(lines, fmt.Sprintf("\t%-*s | %s", maxFilenameLen, name, stats))
}
var insertionsPlural string
if sumInsertions != 0 {
insertionsPlural = "s"
}
var deletionsPlural string
if sumDeletions != 1 {
deletionsPlural = "s"
}
lines = append(lines, fmt.Sprintf(" %s, %s, %s",
diffStatDescription(fileDiffs),
fmt.Sprintf("%d insertion%s", sumInsertions, insertionsPlural),
fmt.Sprintf("%d deletion%s", sumDeletions, deletionsPlural),
))
return lines, nil
}
func diffDisplayName(f *diff.FileDiff) string {
name := f.NewName
if name == "/dev/null" {
name = f.OrigName
}
return name
}
func diffStatDescription(fileDiffs []*diff.FileDiff) string {
var plural string
if len(fileDiffs) > 1 {
plural = "s"
}
return fmt.Sprintf("%d file%s changed", len(fileDiffs), plural)
}
func diffStatDiagram(stat diff.Stat) string {
const maxWidth = 20
added := float64(stat.Added + stat.Changed)
deleted := float64(stat.Deleted + stat.Changed)
if total := added + deleted; total > maxWidth {
x := float64(20) / total
added *= x
deleted *= x
}
return fmt.Sprintf("%s%s%s%s%s",
output.StyleLinesAdded, strings.Repeat("+", int(added)),
output.StyleLinesDeleted, strings.Repeat("-", int(deleted)),
output.StyleReset,
)
}
type stepsExecTUI struct {
out *output.Output
task *executor.Task
updateStatusBar func(string)
}
func (ui stepsExecTUI) ArchiveDownloadStarted() {
ui.updateStatusBar("Downloading archive")
ui.out.Verbosef("[%s] Downloading repository archive...", ui.task.Repository.Name)
}
func (ui stepsExecTUI) ArchiveDownloadFinished(err error) {
if err != nil {
ui.out.Verbosef("[%s] Archive download failed: %v", ui.task.Repository.Name, err)
} else {
ui.out.Verbosef("[%s] Archive download completed", ui.task.Repository.Name)
}
}
func (ui stepsExecTUI) WorkspaceInitializationStarted() {
ui.updateStatusBar("Initializing workspace")
ui.out.Verbosef("[%s] Initializing workspace...", ui.task.Repository.Name)
}
func (ui stepsExecTUI) WorkspaceInitializationFinished() {
ui.out.Verbosef("[%s] Workspace initialization completed", ui.task.Repository.Name)
}
func (ui stepsExecTUI) SkippingStepsUpto(startStep int) {
switch startStep {
case 1:
ui.updateStatusBar("Skipping step 1. Found cached result.")
ui.out.Verbosef("[%s] Skipping step 1 (cached result found)", ui.task.Repository.Name)
default:
ui.updateStatusBar(fmt.Sprintf("Skipping steps 1 to %d. Found cached results.", startStep))
ui.out.Verbosef("[%s] Skipping steps 1 to %d (cached results found)", ui.task.Repository.Name, startStep)
}
}
func (ui stepsExecTUI) StepSkipped(step int) {
ui.updateStatusBar(fmt.Sprintf("Skipping step %d", step))
ui.out.Verbosef("[%s] Step %d skipped", ui.task.Repository.Name, step)
}
func (ui stepsExecTUI) StepPreparingStart(step int) {
ui.updateStatusBar(fmt.Sprintf("Preparing step %d", step))
ui.out.Verbosef("[%s] Preparing step %d...", ui.task.Repository.Name, step)
}
func (ui stepsExecTUI) StepPreparingSuccess(step int) {
ui.out.Verbosef("[%s] Step %d preparation completed", ui.task.Repository.Name, step)
}
func (ui stepsExecTUI) StepPreparingFailed(step int, err error) {
ui.out.Verbosef("[%s] Step %d preparation failed: %v", ui.task.Repository.Name, step, err)
}
func (ui stepsExecTUI) StepStarted(step int, runScript string, env map[string]string) {
ui.updateStatusBar(runScript)
ui.out.Verbosef("[%s] Step %d started: %s", ui.task.Repository.Name, step, truncateScript(runScript, 100))
if len(env) > 0 {
ui.out.Verbosef("[%s] Step %d environment variables: %d set", ui.task.Repository.Name, step, len(env))
}
}
func (ui stepsExecTUI) StepOutputWriter(ctx context.Context, task *executor.Task, step int) executor.StepOutputWriter {
return executor.NoopStepOutputWriter{}
}
func (ui stepsExecTUI) StepFinished(idx int, diff []byte, changes git.Changes, outputs map[string]any) {
ui.out.Verbosef("[%s] Step %d finished successfully", ui.task.Repository.Name, idx)
if len(diff) > 0 {
ui.out.Verbosef("[%s] Step %d produced %d bytes of diff", ui.task.Repository.Name, idx, len(diff))
}
if len(changes.Modified)+len(changes.Added)+len(changes.Deleted)+len(changes.Renamed) > 0 {
ui.out.Verbosef("[%s] Step %d changes: %d modified, %d added, %d deleted, %d renamed",
ui.task.Repository.Name, idx,
len(changes.Modified), len(changes.Added), len(changes.Deleted), len(changes.Renamed))
}
if len(outputs) > 0 {
ui.out.Verbosef("[%s] Step %d outputs: %d variables set", ui.task.Repository.Name, idx, len(outputs))
}
}
func (ui stepsExecTUI) StepFailed(idx int, err error, exitCode int) {
ui.out.Verbosef("[%s] Step %d failed (exit code %d): %v", ui.task.Repository.Name, idx, exitCode, err)
}
// truncateScript truncates a script string for display purposes
func truncateScript(script string, maxLen int) string {
lines := strings.Split(script, "\n")
firstLine := lines[0]
if len(firstLine) > maxLen {
return firstLine[:maxLen] + "..."
}
if len(lines) > 1 {
return firstLine + " ..."
}
return firstLine
}