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