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