gunk-dev/the-valley

plan.go

source026c37612e61main's published tip history
11.0 KiB
1package server
2
3import (
4 "net/http"
5 "net/url"
6 "sort"
7 "strconv"
8 "strings"
9
10 "the-valley/knot"
11 "the-valley/webui/export"
12)
13
14// index is an export's projects and knots by name, built once per
15// generation.
16type index struct {
17 projects map[string]*export.Project
18 knots map[string]map[string]*export.Knot
19 // public are the public projects' names, in the export's order.
20 public []string
21}
22
23func newIndex(e *export.Export) *index {
24 idx := &index{projects: map[string]*export.Project{}, knots: map[string]map[string]*export.Knot{}}
25 for i := range e.Projects {
26 p := &e.Projects[i]
27 idx.projects[p.Name] = p
28 if p.Visibility == export.Public {
29 idx.public = append(idx.public, p.Name)
30 }
31 byID := map[string]*export.Knot{}
32 for j := range p.Knots {
33 byID[p.Knots[j].ID] = &p.Knots[j]
34 }
35 idx.knots[p.Name] = byID
36 }
37 return idx
38}
39
40func knotURL(project, id string) string {
41 return "/knots/" + url.PathEscape(project) + "/" + url.PathEscape(id)
42}
43
44// knotLink is a knot as a page links to it.
45type knotLink struct {
46 Project string
47 ID string
48 Title string
49 State string
50 URL string
51}
52
53func linkOf(project string, k *export.Knot) knotLink {
54 return knotLink{Project: project, ID: k.ID, Title: k.Title, State: k.State, URL: knotURL(project, k.ID)}
55}
56
57// The plan page, in its reading order: what waits on the reader, what is
58// in flight, the paths, and the milestones.
59type planPage struct {
60 Projects string
61 Tally []tally
62 InFlight []flight
63 Changes []readyChange
64 Paths []path
65 Milestones []milestone
66}
67
68// tally is how many knots are in one state.
69type tally struct {
70 N int
71 Label string
72}
73
74// flight is a knot in flight, with who claims it and since when.
75type flight struct {
76 Knot knotLink
77 Note string
78}
79
80// readyChange is a change ready for approval.
81type readyChange struct {
82 Handle string
83 Title string
84 Version string
85 Note string
86 Knots []knotLink
87}
88
89// path is one parent's children in the order their blocked_by edges set:
90// each stage holds the children whose blockers among their siblings are
91// all in earlier stages.
92type path struct {
93 Parent knotLink
94 Stages [][]knotLink
95}
96
97// milestone is an outcome with no parent and the tree of knots under it,
98// or a project's knots that have no parent and are no milestone.
99type milestone struct {
100 Knot *knotLink
101 Title string
102 Project string
103 Count string
104 Cord []string
105 Tree []node
106}
107
108// node is one knot of a milestone's tree, with the knots under it.
109type node struct {
110 Row row
111 Children []node
112 Open bool
113}
114
115// row is how a knot reads in a tree: its glyph, ID and title, and chips
116// saying what more there is to know.
117type row struct {
118 knotLink
119 Filter string
120 Chips []chip
121}
122
123type chip struct {
124 Class string
125 Text string
126}
127
128// stateRank orders a tree's children: work in progress first, finished
129// work last.
130var stateRank = map[string]int{"in-progress": 0, "ready": 1, "blocked": 2, "active": 3, "done": 4, "abandoned": 5}
131
132func rank(state string) int {
133 if r, ok := stateRank[state]; ok {
134 return r
135 }
136 return len(stateRank)
137}
138
139func (q *request) plan() {
140 var p planPage
141 p.Projects = strings.Join(q.idx.public, " · ")
142 counts := map[string]int{}
143 for _, name := range q.idx.public {
144 proj := q.idx.projects[name]
145 roots := set(proj.Roots)
146 for i := range proj.Knots {
147 k := &proj.Knots[i]
148 if (k.Type == "outcome" || k.Type == "bug") && !roots[k.ID] {
149 counts[k.State]++
150 }
151 }
152 p.InFlight = append(p.InFlight, q.inFlight(proj)...)
153 p.Changes = append(p.Changes, q.readyChanges(proj)...)
154 p.Paths = append(p.Paths, q.paths(proj)...)
155 p.Milestones = append(p.Milestones, q.milestones(proj)...)
156 }
157 for _, s := range []string{"done", "in-progress", "ready", "blocked"} {
158 p.Tally = append(p.Tally, tally{counts[s], stateLabel(s)})
159 }
160 q.page(http.StatusOK, "plan", "Plan", p)
161}
162
163// inFlight is the project's knots in flight, in the export's order: the
164// claimed ones, oldest claim first, then those in progress that nobody
165// claims. A claim's age is read from when it was made.
166func (q *request) inFlight(p *export.Project) []flight {
167 var out []flight
168 for _, id := range p.InFlight {
169 k, ok := q.idx.knots[p.Name][id]
170 if !ok {
171 continue
172 }
173 f := flight{Knot: linkOf(p.Name, k), Note: "in progress, unclaimed"}
174 if c := k.Claim; c != nil {
175 f.Note = "claimed by " + c.By
176 if at, err := parseTime(c.At); err == nil {
177 age := q.now.Sub(at)
178 f.Note += " " + ago(age)
179 if age > knot.StaleClaim {
180 f.Note += "; the claim is stale"
181 }
182 }
183 }
184 out = append(out, f)
185 }
186 return out
187}
188
189// readyChanges are the project's changes ready for approval, each with
190// its active version, whether an integration request approves it, and the
191// integrator's last decision.
192func (q *request) readyChanges(p *export.Project) []readyChange {
193 var out []readyChange
194 for _, c := range p.Changes {
195 v := c.Version
196 notes := []string{"not approved"}
197 if v.Request != nil {
198 notes = []string{"approved"}
199 }
200 if v.Outcome != nil {
201 notes = append(notes, "integrator: "+v.Outcome.Decision)
202 }
203 if v.Landing == "uncertain" {
204 notes = append(notes, "landing uncertain")
205 }
206 rc := readyChange{Handle: c.Handle, Title: v.Title, Version: p.Name + " v" + strconv.Itoa(v.N),
207 Note: strings.Join(notes, " · ")}
208 for _, ref := range c.Knots {
209 if k, ok := q.idx.knots[p.Name][ref.ID]; ok {
210 rc.Knots = append(rc.Knots, linkOf(p.Name, k))
211 }
212 }
213 out = append(out, rc)
214 }
215 return out
216}
217
218// paths are the project's parents whose children wait on one another,
219// each with its children in stages. A child's stage is one past the
220// latest stage of the siblings in its blocked_by, done or not, so
221// children with no order between them share a stage and can run in
222// parallel. Abandoned children are left out.
223func (q *request) paths(p *export.Project) []path {
224 knots := q.idx.knots[p.Name]
225 var out []path
226 for i := range p.Knots {
227 parent := &p.Knots[i]
228 var kids []*export.Knot
229 siblings := map[string]bool{}
230 for _, id := range parent.Children {
231 if k, ok := knots[id]; ok && k.State != "abandoned" {
232 kids = append(kids, k)
233 siblings[id] = true
234 }
235 }
236 ordered := false
237 for _, k := range kids {
238 for _, b := range k.BlockedBy {
239 ordered = ordered || siblings[b]
240 }
241 }
242 if !ordered {
243 continue
244 }
245 stage := map[string]int{}
246 // Each pass places the children whose sibling blockers are all
247 // placed. The graph holds blocked_by acyclic; a cycle it did not
248 // would end the passes, and its knots share a last stage.
249 for placed := 0; placed < len(kids); {
250 progress := false
251 for _, k := range kids {
252 if _, done := stage[k.ID]; done {
253 continue
254 }
255 at, ready := 0, true
256 for _, b := range k.BlockedBy {
257 if !siblings[b] {
258 continue
259 }
260 s, ok := stage[b]
261 if !ok {
262 ready = false
263 break
264 }
265 at = max(at, s+1)
266 }
267 if ready {
268 stage[k.ID] = at
269 placed++
270 progress = true
271 }
272 }
273 if !progress {
274 last := 0
275 for _, s := range stage {
276 last = max(last, s+1)
277 }
278 for _, k := range kids {
279 if _, done := stage[k.ID]; !done {
280 stage[k.ID] = last
281 placed++
282 }
283 }
284 }
285 }
286 pa := path{Parent: linkOf(p.Name, parent)}
287 for _, k := range kids {
288 s := stage[k.ID]
289 for len(pa.Stages) <= s {
290 pa.Stages = append(pa.Stages, nil)
291 }
292 pa.Stages[s] = append(pa.Stages[s], linkOf(p.Name, k))
293 }
294 out = append(out, pa)
295 }
296 return out
297}
298
299// milestones are the project's roots, each with its tree, and then its
300// knots that have no parent and are no root, as one more box.
301func (q *request) milestones(p *export.Project) []milestone {
302 knots := q.idx.knots[p.Name]
303 var out []milestone
304 for _, id := range p.Roots {
305 k, ok := knots[id]
306 if !ok {
307 continue
308 }
309 var desc []*export.Knot
310 q.descendants(p.Name, k, &desc, map[string]bool{k.ID: true})
311 done, of := 0, 0
312 var cord []string
313 for _, d := range desc {
314 if d.State == "abandoned" {
315 continue
316 }
317 of++
318 if d.State == "done" {
319 done++
320 }
321 cord = append(cord, d.State)
322 }
323 // The cord reads finished work first, then the rest by state.
324 sort.SliceStable(cord, func(i, j int) bool {
325 di, dj := cord[i] == "done", cord[j] == "done"
326 if di != dj {
327 return di
328 }
329 return rank(cord[i]) < rank(cord[j])
330 })
331 link := linkOf(p.Name, k)
332 out = append(out, milestone{Knot: &link, Title: k.Title, Project: p.Name,
333 Count: strconv.Itoa(done) + " of " + strconv.Itoa(of) + " done", Cord: cord,
334 Tree: q.tree(p.Name, k.Children, map[string]bool{k.ID: true})})
335 }
336 roots := set(p.Roots)
337 var loose []string
338 for _, k := range p.Knots {
339 if k.Parent == "" && !roots[k.ID] {
340 loose = append(loose, k.ID)
341 }
342 }
343 if len(loose) > 0 {
344 out = append(out, milestone{Title: "Unfiled", Project: p.Name, Count: count(len(loose), "knot"),
345 Tree: q.tree(p.Name, loose, map[string]bool{})})
346 }
347 return out
348}
349
350// descendants appends every knot under k, depth first. seen guards
351// against a parent cycle the graph would not hold.
352func (q *request) descendants(project string, k *export.Knot, into *[]*export.Knot, seen map[string]bool) {
353 for _, id := range k.Children {
354 c, ok := q.idx.knots[project][id]
355 if !ok || seen[id] {
356 continue
357 }
358 seen[id] = true
359 *into = append(*into, c)
360 q.descendants(project, c, into, seen)
361 }
362}
363
364// tree is the knots called ids and everything under them, each level
365// ordered by state.
366func (q *request) tree(project string, ids []string, seen map[string]bool) []node {
367 var kids []*export.Knot
368 for _, id := range ids {
369 if k, ok := q.idx.knots[project][id]; ok && !seen[id] {
370 kids = append(kids, k)
371 }
372 }
373 sort.SliceStable(kids, func(i, j int) bool { return rank(kids[i].State) < rank(kids[j].State) })
374 var out []node
375 for _, k := range kids {
376 inner := map[string]bool{k.ID: true}
377 for id := range seen {
378 inner[id] = true
379 }
380 n := node{Row: q.row(project, k), Open: k.State != "done" && k.State != "abandoned"}
381 n.Children = q.tree(project, k.Children, inner)
382 out = append(out, n)
383 }
384 return out
385}
386
387// row is a knot's line in a tree. Filter is the state the page's filters
388// read: abandoned work is finished work, as done is.
389func (q *request) row(project string, k *export.Knot) row {
390 r := row{knotLink: linkOf(project, k), Filter: k.State}
391 if k.State == "abandoned" {
392 r.Filter = "done"
393 }
394 if k.Target != "" && k.Target != project {
395 r.Chips = append(r.Chips, chip{"quiet", "lands in " + k.Target})
396 }
397 if k.Claim != nil {
398 r.Chips = append(r.Chips, chip{"", "claimed by " + k.Claim.By})
399 }
400 if k.State == "blocked" {
401 if len(k.WaitingOn) > 0 {
402 r.Chips = append(r.Chips, chip{"blocked", "waits on " + strings.Join(k.WaitingOn, ", ")})
403 } else {
404 r.Chips = append(r.Chips, chip{"blocked", "waits on an unpublished knot"})
405 }
406 }
407 if k.Progress != nil && k.Progress.Of > 0 {
408 r.Chips = append(r.Chips, chip{"quiet", strconv.Itoa(k.Progress.Done) + "/" + strconv.Itoa(k.Progress.Of)})
409 }
410 if k.CommentCount > 0 {
411 r.Chips = append(r.Chips, chip{"quiet", count(k.CommentCount, "comment")})
412 }
413 return r
414}
415
416func set(list []string) map[string]bool {
417 m := map[string]bool{}
418 for _, s := range list {
419 m[s] = true
420 }
421 return m
422}