gunk-dev/the-valley

plan.go

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