gunk-dev/the-valley

queue.go

source6bf8ca46b311main's published tip history
7.9 KiB
1package main
2
3// The operator's queue: the ready changes that wait on an approval, in the
4// order they would land, and the ready changes that move without one.
5// valley pending prints it (pending.go), and valley review with no change
6// walks it (walk.go).
7//
8// A ready change whose active version has not landed is moving when a
9// current restack report says its restack stopped short of complete, when
10// one of its required checks failed, or when it is approved: its active
11// version has an integration request at its head, or an earlier version's
12// request carries to it (addCarried, pending.go). Every other one waits on
13// an approval. Waiting changes are stacked by ancestry among themselves
14// (stack): a waiting change that another's head contains folds into that
15// change's row, since approving the top lands every layer. A moving change
16// is a row of its own whatever it is stacked on, since it moves on its own.
17//
18// Rows are in landing order. The check worker builds ready versions in
19// the order it first saw them ready, then by project, then by change ID
20// (worker.go), and the integrator lands an approved request once its
21// evidence is complete. So a row's place is when its top's active version
22// became ready, oldest first, then its project and change ID. A record
23// carries no time, so when a version became ready is read as the commit
24// time of the change's record: the last write to the record of a ready
25// change is the one that readied its active version. The worker's own
26// order is the scan in which its pass first saw each version, which only
27// that pass holds, so versions readied between the same two scans may
28// land in another order than their rows.
29
30import (
31 "fmt"
32 "io"
33 "sort"
34 "strings"
35
36 "the-valley/change"
37)
38
39// queue is a repository's rows: the stacks of changes that wait on an
40// approval, each by its top, and the moving changes, each with its glyph
41// and what it says. Neither is in landing order yet (byLanding). With
42// measured, each entry is measured for its block (measure).
43func (r listed) queue(view jsonRepo, h handleMap, l listing, measured bool) (waiting, moving []*entry) {
44 states := r.states()
45 var wait []*entry
46 for _, c := range view.Changes {
47 e := r.changeEntry("ready", c, h, l, states)
48 v := c.Versions[c.Active-1]
49 switch {
50 case c.Restack != nil && c.Restack.Current && c.Restack.Report.Result != change.RestackComplete:
51 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
52 e.glyph, e.colour = "◇", colourWaiting
53 if c.Restack.Report.Result == change.RestackConflict {
54 e.glyph, e.colour = "✗", colourRefused
55 }
56 e.says = "restack: " + readable(restackSays(c.Restack.Report, name))
57 case v.Checks != nil && v.Checks.State == checkFailed:
58 e.glyph, e.colour, e.says = "✗", colourRefused, failedNames(v.Checks)+" failed"
59 if c.Approved != nil && *c.Approved {
60 e.says = "approved · " + e.says
61 }
62 case c.Approved != nil && *c.Approved:
63 e.glyph, e.colour, e.says = "◐", colourWorking, "approved"
64 if n := checkCount(v.Checks); n != "" {
65 e.says += " · " + n
66 }
67 default:
68 wait = append(wait, e)
69 continue
70 }
71 moving = append(moving, e)
72 }
73 r.dated(append(append([]*entry{}, wait...), moving...))
74 waiting = r.snap.stack(wait, measured)
75 for _, e := range moving {
76 if measured {
77 r.snap.measure(e)
78 }
79 }
80 return waiting, moving
81}
82
83// operatorQueue is the queue across every readable repository in repos,
84// as valley pending and valley review's walk (walk.go) both read it: the
85// rows that wait on an approval and the moving rows, each in landing
86// order. A change's checks are read before it is classed, so a change
87// whose check failed is moving rather than waiting. With --drafts and
88// --checks, the drafts' checks are read with them, in the same batch.
89// each, when it is not nil, is given every repository's view as it is
90// read.
91func operatorQueue(repos []listed, h handleMap, l listing, c *costs, stderr io.Writer, each func(listed, jsonRepo)) (waiting, moving []*entry) {
92 for _, r := range repos {
93 if r.err != nil {
94 continue
95 }
96 end := phase("changes")
97 view := r.pendingView(h)
98 r.addCarried(&view, stderr)
99 end()
100 end = phase("checks")
101 r.addChecks(&view, l.drafts && l.checks, c, stderr)
102 end()
103 wa, mo := r.queue(view, h, l, l.verbose)
104 waiting, moving = append(waiting, wa...), append(moving, mo...)
105 if each != nil {
106 each(r, view)
107 }
108 }
109 byLanding(waiting)
110 byLanding(moving)
111 return waiting, moving
112}
113
114// byLanding orders rows as they would land: by when the top's active
115// version became ready, oldest first, then by project and change ID.
116func byLanding(rows []*entry) {
117 sort.SliceStable(rows, func(i, j int) bool {
118 a, b := rows[i], rows[j]
119 if a.since != b.since {
120 return a.since < b.since
121 }
122 if a.repo != b.repo {
123 return a.repo < b.repo
124 }
125 return a.id < b.id
126 })
127}
128
129// checkCount is a version's checks as a row says them: passed out of
130// required when every nix check passed, and otherwise how many are
131// pending or failed. A check of another runner is not counted, since the
132// host worker does not run it. It is "" when the checks were not read.
133func checkCount(c *jsonVersionChecks) string {
134 if c == nil {
135 return ""
136 }
137 count := map[string]int{}
138 for _, q := range c.Required {
139 count[q.State]++
140 }
141 built := len(c.Required) - count[checkOther]
142 switch {
143 case built == 0:
144 return "no checks required"
145 case count[checkFailed] > 0:
146 return counted(count[checkFailed], "check") + " failed"
147 case count[checkPending] > 0:
148 return counted(count[checkPending], "check") + " pending"
149 }
150 return fmt.Sprintf("checks %d/%d", count[checkPassed], built)
151}
152
153// failedNames names the checks that failed, up to three.
154func failedNames(c *jsonVersionChecks) string {
155 var names []string
156 for _, q := range c.Required {
157 if q.State == checkFailed {
158 names = append(names, q.Name)
159 }
160 }
161 return firstFew(names)
162}
163
164// states is the state of every change a repository records, by its
165// handle key.
166func (r listed) states() map[handleKey]string {
167 states := map[handleKey]string{}
168 for _, n := range r.snap.all {
169 states[handleKey{r.name, n.record.Target, n.id}] = n.record.State
170 }
171 return states
172}
173
174// changeEntry is a recorded change as an entry of section.
175func (r listed) changeEntry(section string, c jsonChange, h handleMap, l listing, states map[handleKey]string) *entry {
176 v := c.Versions[c.Active-1]
177 e := &entry{section: section, repo: r.name, id: c.ChangeID, version: v.N, record: c.Record,
178 name: fmt.Sprintf("%s v%d", c.Handle, v.N), ref: c.Handle, head: v.Head, title: readable(v.Title)}
179 if len(c.Knots) > 0 {
180 e.knot = readable(c.Knots[0].ID)
181 }
182 if section == "drafts" && v.Branch != "" {
183 e.name += " on " + branchName(v.Branch)
184 }
185 for _, id := range c.Includes {
186 k := handleKey{r.name, c.Target, id}
187 if st := states[k]; st != change.Ready && st != change.Draft {
188 e.includes = append(e.includes, fmt.Sprintf("includes %s (%s)", h[k], st))
189 }
190 }
191 if v.Landing == uncertain {
192 e.notes = append(e.notes, "uncertain: it may have landed without its evidence being stored")
193 }
194 if c.Restack != nil && c.Restack.Current {
195 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
196 e.notes = append(e.notes, "restack: "+readable(restackSays(c.Restack.Report, name)))
197 }
198 if l.checks {
199 e.requires = r.snap.checks(v.Head)
200 }
201 if v.Checks != nil {
202 e.state, e.checks = v.Checks, checksLine(v.Checks)
203 }
204 return e
205}
206
207// dated gives each entry its head's commit time, age and author, and a
208// change's entry the commit time of its record.
209func (r listed) dated(all []*entry) {
210 for _, e := range all {
211 out, _ := r.g.output(true, "log", "-1", "--format=%ct%x00%cr%x00%an", e.head)
212 if fields := strings.SplitN(out, "\x00", 3); len(fields) == 3 {
213 e.when = parseUnix(fields[0])
214 e.age, e.author = fields[1], readable(fields[2])
215 }
216 if e.record != "" {
217 out, _ := r.g.output(true, "log", "-1", "--format=%ct", e.record)
218 e.since = parseUnix(out)
219 }
220 }
221}