gunk-dev/the-valley

derive.go

sourced6ff7d88653bmain's published tip history
9.0 KiB
1package knot
2
3import (
4 "fmt"
5 "slices"
6 "sort"
7 "strings"
8 "time"
9)
10
11// Graph is a whole graph, read: every knot, and every comment on each.
12type Graph struct {
13 Index *Index
14 Knots map[string]*Knot
15 Comments map[string][]Noted // by the ID of the knot they are on, oldest first
16
17 // children and blocks are the edges read backwards, built on the
18 // first view.
19 children, blocks map[string][]string
20}
21
22// Noted is one comment as the graph holds it: what it says, where it is,
23// when it was written, and the operation that wrote it.
24type Noted struct {
25 Comment
26 Path string
27 At time.Time
28 Op string
29}
30
31// Load reads and validates a whole graph.
32func Load(idx *Index, read Reader) (*Graph, error) {
33 if err := Validate(idx, read); err != nil {
34 return nil, err
35 }
36 g := &Graph{Index: idx, Knots: map[string]*Knot{}, Comments: map[string][]Noted{}}
37 for id, f := range idx.Knots {
38 data, err := read(f.OID, MaxKnotSize)
39 if err != nil {
40 return nil, err
41 }
42 if g.Knots[id], err = ParseKnot(f.Path, f.Slug, data); err != nil {
43 return nil, err
44 }
45 }
46 for path, f := range idx.Comments {
47 data, err := read(f.OID, MaxCommentSize)
48 if err != nil {
49 return nil, err
50 }
51 c, err := ParseComment(path, data)
52 if err != nil {
53 return nil, err
54 }
55 at, err := time.Parse(CommentStamp, f.Stamp)
56 if err != nil {
57 return nil, fmt.Errorf("%s: its stamp %s is no time", path, f.Stamp)
58 }
59 g.Comments[f.ID] = append(g.Comments[f.ID], Noted{Comment: *c, Path: path, At: at, Op: f.Op})
60 }
61 for _, list := range g.Comments {
62 sort.Slice(list, func(i, j int) bool { return list[i].Path < list[j].Path })
63 }
64 return g, nil
65}
66
67// Empty is the graph a project holds before its first knot.
68func Empty() *Graph {
69 idx, _ := Layout(nil)
70 return &Graph{Index: idx, Knots: map[string]*Knot{}, Comments: map[string][]Noted{}}
71}
72
73// IDs are every knot's ID, sorted.
74func (g *Graph) IDs() []string {
75 ids := make([]string, 0, len(g.Knots))
76 for id := range g.Knots {
77 ids = append(ids, id)
78 }
79 sort.Strings(ids)
80 return ids
81}
82
83// Foreign resolves a reference to another project's knot. found is false
84// when that project's graph holds no such knot; err is for a graph that
85// could not be read.
86type Foreign func(ref string) (k *Knot, found bool, err error)
87
88// View is what the graph says of one knot beyond its own fields: whether
89// it is ready to be worked on, what holds it up, and what it holds up.
90// None of it is stored; it is derived from the graph each time.
91type View struct {
92 // Ready is an outcome or a bug that is active, unclaimed, and every
93 // blocker of which is terminal. An idea is never ready: it is thought
94 // about, not picked up as work. Nor is a question: it is answered.
95 Ready bool `json:"ready"`
96 // WaitingOn are the blockers that are not terminal, or that could not
97 // be resolved.
98 WaitingOn []string `json:"waiting_on"`
99 // ClearedByEnding are blockers that cleared by being abandoned,
100 // discarded, superseded or dropped rather than finished: they no
101 // longer block, as the archive's rule says, but a reader should know.
102 ClearedByEnding []string `json:"cleared_by_ending"`
103 // Dangling are references to another project's knots that its graph
104 // does not hold; Unresolved are those whose graph could not be read.
105 Dangling []string `json:"dangling"`
106 Unresolved []string `json:"unresolved"`
107 // Children are the knots whose parent this is, and Blocks the knots
108 // whose blocked_by names it.
109 Children []string `json:"children"`
110 Blocks []string `json:"blocks"`
111 // Progress is set for a knot with children.
112 Progress *Progress `json:"progress,omitempty"`
113 // Claim is set for a claimed knot.
114 Claim *Claim `json:"claim,omitempty"`
115}
116
117// Progress is a parent's children that are finished, out of its children:
118// terminal children that are not abandoned, out of the children that are
119// not abandoned.
120type Progress struct {
121 Done int `json:"done"`
122 Of int `json:"of"`
123}
124
125// Claim is a knot's claim, with its age.
126type Claim struct {
127 By string `json:"by"`
128 At string `json:"at"`
129 AgeSeconds int64 `json:"age_seconds"`
130 // Stale is a claim older than StaleClaim: the run that made it may
131 // have ended without releasing it.
132 Stale bool `json:"stale"`
133}
134
135// endings are the terminal statuses that end a knot without finishing it.
136var endings = []string{"abandoned", "discarded", "superseded", "dropped"}
137
138// View derives the view of the knot with id at the time now. foreign
139// resolves references to other projects' knots; nil leaves them all
140// unresolved.
141func (g *Graph) View(id string, now time.Time, foreign Foreign) View {
142 k := g.Knots[id]
143 v := View{WaitingOn: []string{}, ClearedByEnding: []string{}, Dangling: []string{}, Unresolved: []string{},
144 Children: []string{}, Blocks: []string{}}
145 for _, ref := range k.BlockedBy {
146 var b *Knot
147 if IsLocalID(ref) {
148 b = g.Knots[ref]
149 } else if foreign == nil {
150 v.Unresolved = append(v.Unresolved, ref)
151 } else if found, ok, err := foreign(ref); err != nil {
152 v.Unresolved = append(v.Unresolved, ref)
153 } else if !ok {
154 v.Dangling = append(v.Dangling, ref)
155 } else {
156 b = found
157 }
158 switch {
159 case b == nil:
160 v.WaitingOn = append(v.WaitingOn, ref)
161 case b.Active():
162 v.WaitingOn = append(v.WaitingOn, ref)
163 case slices.Contains(endings, b.Status):
164 v.ClearedByEnding = append(v.ClearedByEnding, ref)
165 }
166 }
167 if g.children == nil {
168 g.children, g.blocks = map[string][]string{}, map[string][]string{}
169 for _, other := range g.IDs() {
170 o := g.Knots[other]
171 if o.Parent != "" {
172 g.children[o.Parent] = append(g.children[o.Parent], other)
173 }
174 for _, ref := range o.BlockedBy {
175 g.blocks[ref] = append(g.blocks[ref], other)
176 }
177 }
178 }
179 var progress Progress
180 for _, child := range g.children[id] {
181 v.Children = append(v.Children, child)
182 if o := g.Knots[child]; o.Status != "abandoned" {
183 progress.Of++
184 if !o.Active() {
185 progress.Done++
186 }
187 }
188 }
189 v.Blocks = append(v.Blocks, g.blocks[id]...)
190 if len(v.Children) > 0 {
191 v.Progress = &progress
192 }
193 // A claim on a terminal knot is a leftover, and is ignored.
194 claimed := k.ClaimedBy != "" && k.Active()
195 if claimed {
196 c := &Claim{By: k.ClaimedBy, At: k.ClaimedAt}
197 if at, err := time.Parse(time.RFC3339, k.ClaimedAt); err == nil {
198 age := now.Sub(at)
199 c.AgeSeconds = int64(age / time.Second)
200 c.Stale = age > StaleClaim
201 }
202 v.Claim = c
203 }
204 v.Ready = (k.Type == "outcome" || k.Type == "bug") && k.Active() && !claimed && len(v.WaitingOn) == 0
205 return v
206}
207
208// Schema is the model as `valley knot schema` prints it: the types and
209// their statuses, the fields, the edges and the limits, so that a reader
210// of the graph never has to read the CUE schema.
211type Schema struct {
212 Schema string `json:"schema"`
213 Ref string `json:"ref"`
214 Types []Type `json:"types"`
215 Fields []Field `json:"fields"`
216 Edges []Edge `json:"edges"`
217 Limits Limits `json:"limits"`
218}
219
220// Edge is one field that names other knots.
221type Edge struct {
222 Field string `json:"field"`
223 From string `json:"from"`
224 To string `json:"to"`
225 Meaning string `json:"meaning"`
226}
227
228// Limits are the limits a graph is held to.
229type Limits struct {
230 Knots int `json:"knots"`
231 Comments int `json:"comments"`
232 KnotBytes int `json:"knot_bytes"`
233 CommentBytes int `json:"comment_bytes"`
234 ChangedPaths int `json:"changed_paths_per_commit"`
235 TitleBytes int `json:"title_bytes"`
236 ListItems int `json:"list_items"`
237}
238
239// TheSchema is the model.
240func TheSchema() Schema {
241 return Schema{
242 Schema: "valley-knot-schema/v1",
243 Ref: Ref,
244 Types: Types,
245 Fields: Fields,
246 Edges: []Edge{
247 {Field: "parent", From: "any knot", To: "an outcome in this graph",
248 Meaning: "the knot belongs to the outcome; parent edges form no cycle"},
249 {Field: "blocked_by", From: "outcome, bug", To: "any knot in this graph, or <project>:<id> in another",
250 Meaning: "the knot waits on each until it is terminal; blocked_by edges form no cycle"},
251 {Field: "graduated_into", From: "a graduated idea", To: "an outcome or a question in this graph",
252 Meaning: "where the idea moved: work taken on, or a question to settle"},
253 },
254 Limits: Limits{Knots: MaxKnots, Comments: MaxComments, KnotBytes: MaxKnotSize,
255 CommentBytes: MaxCommentSize, ChangedPaths: MaxChangedPaths, TitleBytes: MaxTitle, ListItems: MaxListItems},
256 }
257}
258
259// Describe is the schema as text, for a terminal.
260func Describe() string {
261 var b strings.Builder
262 fmt.Fprintf(&b, "knots live at %s, one file each; comments at comments/<id>/.\n\ntypes:\n", Ref)
263 for _, t := range Types {
264 fmt.Fprintf(&b, " %-9s %s-<7 hex> in %s/\n", t.Name, t.Prefix, t.Directory)
265 fmt.Fprintf(&b, " active: %s terminal: %s\n", strings.Join(t.Active, ", "), strings.Join(t.Terminal, ", "))
266 }
267 b.WriteString("\nfields:\n")
268 for _, f := range Fields {
269 fmt.Fprintf(&b, " %-15s on %s; %s\n", f.Name, strings.Join(f.On, ", "), f.Meaning)
270 }
271 b.WriteString("\nedges:\n")
272 for _, e := range TheSchema().Edges {
273 fmt.Fprintf(&b, " %-15s %s → %s: %s\n", e.Field, e.From, e.To, e.Meaning)
274 }
275 fmt.Fprintf(&b, "\nlimits: %d knots, %d comments, %d bytes a knot, %d a comment, %d files a commit\n",
276 MaxKnots, MaxComments, MaxKnotSize, MaxCommentSize, MaxChangedPaths)
277 return b.String()
278}