gunk-dev/the-valley

queue.go

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