|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/sourcegraph/go-diff/diff" |
| 8 | + "github.com/sourcegraph/src-cli/internal/campaigns" |
| 9 | + "github.com/sourcegraph/src-cli/internal/output" |
| 10 | +) |
| 11 | + |
| 12 | +func newCampaignProgressPrinter(out *output.Output, numParallelism int) *campaignProgressPrinter { |
| 13 | + return &campaignProgressPrinter{ |
| 14 | + out: out, |
| 15 | + |
| 16 | + numParallelism: numParallelism, |
| 17 | + |
| 18 | + completedTasks: map[string]bool{}, |
| 19 | + runningTasks: map[string]*campaigns.TaskStatus{}, |
| 20 | + |
| 21 | + repoStatusBar: map[string]int{}, |
| 22 | + statusBarRepo: map[int]string{}, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +type campaignProgressPrinter struct { |
| 27 | + out *output.Output |
| 28 | + progress output.ProgressWithStatusBars |
| 29 | + |
| 30 | + maxRepoName int |
| 31 | + numParallelism int |
| 32 | + |
| 33 | + completedTasks map[string]bool |
| 34 | + runningTasks map[string]*campaigns.TaskStatus |
| 35 | + |
| 36 | + repoStatusBar map[string]int |
| 37 | + statusBarRepo map[int]string |
| 38 | +} |
| 39 | + |
| 40 | +func (p *campaignProgressPrinter) initProgressBar(statuses []*campaigns.TaskStatus) { |
| 41 | + statusBars := []*output.StatusBar{} |
| 42 | + for i := 0; i < p.numParallelism; i++ { |
| 43 | + statusBars = append(statusBars, output.NewStatusBarWithLabel("Starting worker...")) |
| 44 | + } |
| 45 | + |
| 46 | + p.progress = p.out.ProgressWithStatusBars([]output.ProgressBar{{ |
| 47 | + Label: fmt.Sprintf("Executing steps in %d repositories", len(statuses)), |
| 48 | + Max: float64(len(statuses)), |
| 49 | + }}, statusBars, nil) |
| 50 | +} |
| 51 | + |
| 52 | +func (p *campaignProgressPrinter) Complete() { |
| 53 | + if p.progress != nil { |
| 54 | + p.progress.Complete() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (p *campaignProgressPrinter) PrintStatuses(statuses []*campaigns.TaskStatus) { |
| 59 | + if p.progress == nil { |
| 60 | + p.initProgressBar(statuses) |
| 61 | + } |
| 62 | + |
| 63 | + newlyCompleted := []*campaigns.TaskStatus{} |
| 64 | + currentlyRunning := []*campaigns.TaskStatus{} |
| 65 | + |
| 66 | + for _, ts := range statuses { |
| 67 | + if len(ts.RepoName) > p.maxRepoName { |
| 68 | + p.maxRepoName = len(ts.RepoName) |
| 69 | + } |
| 70 | + |
| 71 | + if ts.IsCompleted() { |
| 72 | + if !p.completedTasks[ts.RepoName] { |
| 73 | + p.completedTasks[ts.RepoName] = true |
| 74 | + newlyCompleted = append(newlyCompleted, ts) |
| 75 | + } |
| 76 | + |
| 77 | + if _, ok := p.runningTasks[ts.RepoName]; ok { |
| 78 | + delete(p.runningTasks, ts.RepoName) |
| 79 | + |
| 80 | + // Free slot |
| 81 | + idx := p.repoStatusBar[ts.RepoName] |
| 82 | + delete(p.statusBarRepo, idx) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if ts.IsRunning() { |
| 87 | + currentlyRunning = append(currentlyRunning, ts) |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + p.progress.SetValue(0, float64(len(p.completedTasks))) |
| 93 | + |
| 94 | + newlyStarted := map[string]*campaigns.TaskStatus{} |
| 95 | + statusBarIndex := 0 |
| 96 | + for _, ts := range currentlyRunning { |
| 97 | + if _, ok := p.runningTasks[ts.RepoName]; ok { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + newlyStarted[ts.RepoName] = ts |
| 102 | + p.runningTasks[ts.RepoName] = ts |
| 103 | + |
| 104 | + // Find free slot |
| 105 | + _, ok := p.statusBarRepo[statusBarIndex] |
| 106 | + for ok { |
| 107 | + statusBarIndex += 1 |
| 108 | + _, ok = p.statusBarRepo[statusBarIndex] |
| 109 | + } |
| 110 | + |
| 111 | + p.statusBarRepo[statusBarIndex] = ts.RepoName |
| 112 | + p.repoStatusBar[ts.RepoName] = statusBarIndex |
| 113 | + } |
| 114 | + |
| 115 | + for _, ts := range newlyCompleted { |
| 116 | + statusText, err := taskStatusText(ts) |
| 117 | + if err != nil { |
| 118 | + p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err) |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + p.progress.Verbosef("%-*s %s", p.maxRepoName, ts.RepoName, statusText) |
| 123 | + |
| 124 | + if idx, ok := p.repoStatusBar[ts.RepoName]; ok { |
| 125 | + // Log that this task completed, but only if there is no |
| 126 | + // currently executing one in this bar, to avoid flicker. |
| 127 | + if _, ok := p.statusBarRepo[idx]; !ok { |
| 128 | + p.progress.StatusBarCompletef(idx, statusText) |
| 129 | + } |
| 130 | + delete(p.repoStatusBar, ts.RepoName) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + for statusBar, repo := range p.statusBarRepo { |
| 135 | + ts, ok := p.runningTasks[repo] |
| 136 | + if !ok { |
| 137 | + // This should not happen |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + statusText, err := taskStatusText(ts) |
| 142 | + if err != nil { |
| 143 | + p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err) |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + if _, ok := newlyStarted[repo]; ok { |
| 148 | + p.progress.StatusBarResetf(statusBar, ts.RepoName, statusText) |
| 149 | + } else { |
| 150 | + p.progress.StatusBarUpdatef(statusBar, statusText) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func taskStatusText(ts *campaigns.TaskStatus) (string, error) { |
| 156 | + var statusText string |
| 157 | + |
| 158 | + if ts.IsCompleted() { |
| 159 | + if ts.ChangesetSpec == nil { |
| 160 | + statusText = "No changes" |
| 161 | + } else { |
| 162 | + fileDiffs, err := diff.ParseMultiFileDiff([]byte(ts.ChangesetSpec.Commits[0].Diff)) |
| 163 | + if err != nil { |
| 164 | + return "", err |
| 165 | + } |
| 166 | + |
| 167 | + statusText = diffStatDescription(fileDiffs) + " " + diffStatDiagram(sumDiffStats(fileDiffs)) |
| 168 | + } |
| 169 | + |
| 170 | + if ts.Cached { |
| 171 | + statusText += " (cached)" |
| 172 | + } |
| 173 | + } else if ts.IsRunning() { |
| 174 | + if ts.CurrentlyExecuting != "" { |
| 175 | + lines := strings.Split(ts.CurrentlyExecuting, "\n") |
| 176 | + escapedLine := strings.ReplaceAll(lines[0], "%", "%%") |
| 177 | + if len(lines) > 1 { |
| 178 | + statusText = fmt.Sprintf("%s ...", escapedLine) |
| 179 | + } else { |
| 180 | + statusText = fmt.Sprintf("%s", escapedLine) |
| 181 | + } |
| 182 | + } else { |
| 183 | + statusText = fmt.Sprintf("...") |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return statusText, nil |
| 188 | +} |
0 commit comments