gunk-dev/the-valley

plan.go

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