-
Notifications
You must be signed in to change notification settings - Fork 7
/
tape.go
1453 lines (1229 loc) · 33.2 KB
/
tape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package progrock
import (
"bytes"
"fmt"
"io"
"sort"
"sync"
"time"
"github.com/muesli/termenv"
"github.com/vito/midterm"
"github.com/vito/progrock/ui"
)
type Frontend interface {
Writer
Render(*Tape, io.Writer, *UI) error
}
// Tape is a Writer that collects all progress output for displaying in a
// terminal UI.
//
// The Tape has a configurable Frontend which is called with a lock held such
// that it is safe to access all exposed fields of the Tape. Otherwise it is
// not generally safe to access its exported fields. This is a conscious
// trade-off to avoid too much lock contention in the fast path.
type Tape struct {
// a configurable frontend
frontend Frontend
// ChronologicalVertexIDs lists the vertex IDs in the order that they were
// most recently active, with the most recently active last. This is an order
// suitable for displaying to the user in the terminal, where they will
// naturally be watching the bottom of the screen.
ChronologicalVertexIDs []string
// zoomed vertices
zoomed map[string]*zoomState
lastZoom *zoomState
zoomHook func(*zoomState)
// raw vertex/group state from the event stream
Vertices map[string]*Vertex
Groups map[string]*Group
// vertex <-> group mappings
GroupVertices map[string]map[string]struct{}
VertexGroups map[string]map[string]struct{}
// group children
GroupChildren map[string]map[string]struct{}
// vertex state
VertexTasks map[string][]*VertexTask
vertexLogs map[string]*ui.Vterm
// whether and when the tape has been IsClosed
IsClosed bool
ClosedAt time.Time
// the time that tape was created and closed
CreatedAt time.Time
// color profile loaded from the env when the tape was constructed
ColorProfile termenv.Profile
// screen Width and Height
Width, Height int
// visible region for active vterms
ReasonableTermHeight int
// UI config
IsFocused bool // only show 'focused' vertex output, condensing the rest
ShowInternalVertices bool // show internal vertexes
RevealErroredVertices bool // show errored vertexes no matter what
ShowCompletedLogs bool // show output even for completed vertexes
ShowEdgesBetweenVerticesInSameGroup bool // show edges between vertexes in the same group
// output from messages and internal debugging
globalLogs *ui.Vterm
globalLogsW *termenv.Output
// minimum message level to display to the user
messageLevel MessageLevel
l sync.Mutex
}
type zoomState struct {
Input io.Writer
Output *midterm.Terminal
}
// NewTape returns a new Tape.
func NewTape() *Tape {
colorProfile := ui.ColorProfile()
logs := ui.NewVterm()
return &Tape{
CreatedAt: time.Now(),
// default to unbounded screen size so we don't arbitrarily wrap log output
// when no size is given
Width: -1,
Height: -1,
// sane default before window size is known
ReasonableTermHeight: 10,
ColorProfile: colorProfile,
Vertices: make(map[string]*Vertex),
Groups: make(map[string]*Group),
GroupVertices: make(map[string]map[string]struct{}),
VertexGroups: make(map[string]map[string]struct{}),
GroupChildren: make(map[string]map[string]struct{}),
VertexTasks: make(map[string][]*VertexTask),
vertexLogs: make(map[string]*ui.Vterm),
zoomed: make(map[string]*zoomState),
globalLogs: logs,
globalLogsW: ui.NewOutput(logs, termenv.WithProfile(colorProfile)),
messageLevel: MessageLevel_WARNING,
}
}
var _ Writer = &Tape{}
// WriteStatus implements Writer by collecting vertex and task updates and
// writing vertex logs to internal virtual terminals.
func (tape *Tape) WriteStatus(status *StatusUpdate) error {
tape.l.Lock()
defer tape.l.Unlock()
for _, g := range status.Groups {
tape.Groups[g.Id] = g
if g.Parent != nil {
parents, found := tape.GroupChildren[g.GetParent()]
if !found {
parents = make(map[string]struct{})
tape.GroupChildren[g.GetParent()] = parents
}
parents[g.Id] = struct{}{}
}
}
for _, v := range status.Vertexes {
existing, found := tape.Vertices[v.Id]
if !found {
tape.insert(v.Id, v)
} else if existing.Completed != nil && v.Cached {
// don't clobber the "real" vertex with a cache
// TODO: count cache hits?
} else {
tape.Vertices[v.Id] = v
}
_, isZoomed := tape.zoomed[v.Id]
if v.Zoomed && !isZoomed {
tape.initZoom(v)
} else if isZoomed {
tape.releaseZoom(v)
}
}
for _, t := range status.Tasks {
tape.recordTask(t)
}
for _, l := range status.Logs {
var w io.Writer
if t, found := tape.zoomed[l.Vertex]; found {
w = t.Output
} else {
w = tape.VertexLogs(l.Vertex)
}
_, err := w.Write(l.Data)
if err != nil {
return fmt.Errorf("write logs: %w", err)
}
}
for _, ms := range status.Memberships {
members, found := tape.GroupVertices[ms.Group]
if !found {
members = make(map[string]struct{})
tape.GroupVertices[ms.Group] = members
}
for _, vtxId := range ms.Vertexes {
groups, found := tape.VertexGroups[vtxId]
if !found {
groups = make(map[string]struct{})
tape.VertexGroups[vtxId] = groups
}
if len(groups) == 0 {
// only place vertexes in their first group
members[vtxId] = struct{}{}
groups[ms.Group] = struct{}{}
}
}
}
for _, msg := range status.Messages {
tape.log(msg)
}
if tape.frontend != nil {
if err := tape.frontend.WriteStatus(status); err != nil {
return err
}
}
return nil
}
func (tape *Tape) initZoom(v *Vertex) {
var vt *midterm.Terminal
if tape.Height == -1 || tape.Width == -1 {
vt = midterm.NewAutoResizingTerminal()
} else {
vt = midterm.NewTerminal(tape.Height, tape.Width)
}
w := setupTerm(v.Id, vt)
tape.zoomed[v.Id] = &zoomState{
Output: vt,
Input: w,
}
}
func (tape *Tape) releaseZoom(vtx *Vertex) {
delete(tape.zoomed, vtx.Id)
}
func (tape *Tape) recordTask(t *VertexTask) {
tasks := tape.VertexTasks[t.Vertex]
var updated bool
for i, task := range tasks {
if task.Name == t.Name {
tasks[i] = t
updated = true
}
}
if !updated {
tasks = append(tasks, t)
tape.VertexTasks[t.Vertex] = tasks
}
}
func (tape *Tape) log(msg *Message) {
if msg.Level < tape.messageLevel {
return
}
WriteMessage(tape.globalLogsW, msg)
}
func WriteMessage(out *termenv.Output, msg *Message) error {
var prefix termenv.Style
switch msg.Level {
case MessageLevel_DEBUG:
prefix = out.String("DEBUG:").Foreground(termenv.ANSIBlue).Bold()
case MessageLevel_WARNING:
prefix = out.String("WARNING:").Foreground(termenv.ANSIYellow).Bold()
case MessageLevel_ERROR:
prefix = out.String("ERROR:").Foreground(termenv.ANSIRed).Bold()
}
str := msg.Message
for _, l := range msg.Labels {
str += " "
str += out.String(fmt.Sprintf("%s=%q", l.Name, l.Value)).
Foreground(termenv.ANSIBrightBlack).
String()
}
_, err := fmt.Fprintln(out, prefix, str)
return err
}
func (tape *Tape) AllVertices() []*Vertex {
tape.l.Lock()
defer tape.l.Unlock()
var vertices []*Vertex
for _, vid := range tape.ChronologicalVertexIDs {
vtx, found := tape.Vertices[vid]
if !found {
// should be impossible
continue
}
vertices = append(vertices, vtx)
}
return vertices
}
func (tape *Tape) RunningVertex() *Vertex {
tape.l.Lock()
defer tape.l.Unlock()
for i := len(tape.ChronologicalVertexIDs) - 1; i >= 0; i-- {
vid := tape.ChronologicalVertexIDs[i]
vtx := tape.Vertices[vid]
if vtx.Started != nil && vtx.Completed == nil {
return vtx
}
}
return nil
}
type VertexActivity struct {
LastLine string
TasksCompleted int64
TasksTotal int64
TaskBarsCurrent int64
TaskBarsTotal int64
}
func (tape *Tape) Activity(vtx *Vertex) VertexActivity {
tape.l.Lock()
defer tape.l.Unlock()
var activity VertexActivity
term := tape.vertexLogs[vtx.Id]
if term != nil {
activity.LastLine = term.LastLine()
}
tasks := tape.VertexTasks[vtx.Id]
for _, task := range tasks {
activity.TasksTotal++
if task.Completed != nil {
activity.TasksCompleted++
} else if task.Started != nil && activity.LastLine == "" {
activity.LastLine = task.GetName()
}
activity.TaskBarsTotal += task.GetTotal()
activity.TaskBarsCurrent += task.GetCurrent()
}
return activity
}
// CompletedCount returns the number of completed vertexes.
// TODO: cache this
func (tape *Tape) CompletedCount() int {
tape.l.Lock()
defer tape.l.Unlock()
var completed int
for _, v := range tape.Vertices {
if v.Completed != nil {
completed++
}
}
return completed
}
// UncachedCount returns the number of completed uncached vertexes.
// TODO: cache this
func (tape *Tape) RunningCount() int {
tape.l.Lock()
defer tape.l.Unlock()
var running int
for _, v := range tape.Vertices {
if v.Started != nil && v.Completed == nil {
running++
}
}
return running
}
// UncachedCount returns the number of completed uncached vertexes.
// TODO: cache this
func (tape *Tape) UncachedCount() int {
tape.l.Lock()
defer tape.l.Unlock()
var uncached int
for _, v := range tape.Vertices {
if v.Completed != nil && !v.Cached {
uncached++
}
}
return uncached
}
// CachedCount returns the number of cached vertexes.
// TODO: cache this
func (tape *Tape) CachedCount() int {
tape.l.Lock()
defer tape.l.Unlock()
var cached int
for _, v := range tape.Vertices {
if v.Cached {
cached++
}
}
return cached
}
// ErroredCount returns the number of errored vertexes.
// TODO: cache this
func (tape *Tape) ErroredCount() int {
tape.l.Lock()
defer tape.l.Unlock()
var errored int
for _, v := range tape.Vertices {
if v.Error != nil {
errored++
}
}
return errored
}
// TotalCount returns the total number of vertexes.
func (tape *Tape) TotalCount() int {
tape.l.Lock()
defer tape.l.Unlock()
return len(tape.Vertices)
}
// Duration returns the duration that the Tape has been accepting updates until
// Close has been called.
func (tape *Tape) Duration() time.Duration {
tape.l.Lock()
defer tape.l.Unlock()
if tape.IsClosed {
return tape.ClosedAt.Sub(tape.CreatedAt)
}
return time.Since(tape.CreatedAt)
}
// Close marks the Tape as done, which tells it to display all vertex output
// for the final render.
func (tape *Tape) Close() error {
tape.l.Lock()
defer tape.l.Unlock()
tape.IsClosed = true
tape.ClosedAt = time.Now()
if tape.frontend != nil {
return tape.frontend.Close()
}
return nil
}
// Closed returns whether the Tape has been closed.
func (tape *Tape) Closed() bool {
tape.l.Lock()
defer tape.l.Unlock()
return tape.IsClosed
}
// SetFrontend configures a custom frontend for the Tape to display.
func (tape *Tape) SetFrontend(f Frontend) {
tape.l.Lock()
defer tape.l.Unlock()
tape.frontend = f
}
// VerboseEdges sets whether to display edges between vertexes in the same
// group.
func (tape *Tape) VerboseEdges(verbose bool) {
tape.l.Lock()
defer tape.l.Unlock()
tape.ShowEdgesBetweenVerticesInSameGroup = verbose
}
// ShowInternal sets whether to show internal vertexes in the output.
func (tape *Tape) ShowInternal(show bool) {
tape.l.Lock()
defer tape.l.Unlock()
tape.ShowInternalVertices = show
}
// RevealErrored sets whether to show errored vertexes even when they would
// otherwise not be shown (i.e. internal, or focusing). You may want to set
// this when debugging, or for features that might break when bootstrapping
// before a higher level error can be shown.
func (tape *Tape) RevealErrored(reveal bool) {
tape.l.Lock()
defer tape.l.Unlock()
tape.RevealErroredVertices = reveal
}
// ShowAllOutput sets whether to show output even for successful vertexes.
func (tape *Tape) ShowAllOutput(show bool) {
tape.l.Lock()
defer tape.l.Unlock()
tape.ShowCompletedLogs = show
}
// Focus sets whether to hide output of non-focused vertexes.
func (tape *Tape) Focus(focused bool) {
tape.l.Lock()
defer tape.l.Unlock()
tape.IsFocused = focused
}
// MessageLevel sets the minimum level for messages to display.
func (tape *Tape) MessageLevel(level MessageLevel) {
tape.l.Lock()
defer tape.l.Unlock()
tape.messageLevel = level
}
// SetWindowSize sets the size of the terminal UI, which influences the
// dimensions for vertex logs, progress bars, etc.
func (tape *Tape) SetWindowSize(w, h int) {
tape.l.Lock()
tape.Width = w
tape.Height = h
tape.ReasonableTermHeight = h / 4
for _, l := range tape.vertexLogs {
l.SetWidth(w)
}
for _, t := range tape.zoomed {
t.Output.Resize(h, w)
}
tape.globalLogs.SetWidth(w)
tape.l.Unlock()
}
var pulse = ui.FadeFrames
func init() {
framesPerBeat := len(ui.FadeFrames.Frames)
frames := make([]string, framesPerBeat)
copy(frames, ui.FadeFrames.Frames)
lastFrame := (framesPerBeat / 4) * 3
for i := lastFrame; i < len(frames); i++ {
frames[i] = ui.FadeFrames.Frames[lastFrame]
}
pulse.Frames = frames
}
func (tape *Tape) Render(w io.Writer, u *UI) error {
tape.l.Lock()
defer tape.l.Unlock()
out := ui.NewOutput(w, termenv.WithProfile(tape.ColorProfile))
zoomSt := tape.currentZoom()
if zoomSt != tape.lastZoom {
tape.zoomHook(zoomSt)
tape.lastZoom = zoomSt
}
var err error
if zoomSt != nil {
err = tape.renderZoomed(w, u, zoomSt)
} else if tape.frontend != nil {
err = tape.frontend.Render(tape, w, u)
} else if tape.IsFocused {
err = RenderTree(tape, w, u)
} else {
err = tape.renderDAG(out, u)
}
if err != nil {
return err
}
tape.globalLogs.SetHeight(10)
fmt.Fprint(w, tape.globalLogs.View())
return nil
}
func (tape *Tape) currentZoom() *zoomState {
var firstZoomed *Vertex
var firstState *zoomState
for vId, st := range tape.zoomed {
v, found := tape.Vertices[vId]
if !found {
// should be impossible
continue
}
if v.Started == nil {
// just being defensive, theoretically this is a valid state
continue
}
if firstZoomed == nil || v.Started.AsTime().Before(firstZoomed.Started.AsTime()) {
firstZoomed = v
firstState = st
}
}
return firstState
}
func (tape *Tape) renderZoomed(w io.Writer, u *UI, st *zoomState) error {
return st.Output.Render(w)
}
func (tape *Tape) renderDAG(out *termenv.Output, u *UI) error {
b := tape.bouncer()
var completed []*Vertex
var runningAndFailed []*Vertex
for _, dig := range tape.ChronologicalVertexIDs {
vtx := tape.Vertices[dig]
if vtx.Completed == nil || vtx.Error != nil {
runningAndFailed = append(runningAndFailed, vtx)
} else {
completed = append(completed, vtx)
}
}
order := append(completed, runningAndFailed...)
groups := progressGroups{}
for i, vtx := range order {
for _, g := range groups {
if g != nil {
g.Witness(vtx)
}
}
groups = groups.Reap(out, u, order[i:])
for _, g := range b.Groups(vtx) {
groups = groups.AddGroup(out, u, b, g, tape.log)
}
if !b.IsVisible(vtx) {
continue
}
symbol := ui.Block
if vtx.Completed == nil {
symbol, _, _ = u.Spinner.ViewFrame(pulse)
}
groups.VertexPrefix(out, u, vtx, symbol, tape.log)
if err := u.RenderVertex(out, vtx); err != nil {
return err
}
tasks := tape.VertexTasks[vtx.Id]
for _, t := range tasks {
groups.TaskPrefix(out, u, vtx)
if err := u.RenderTask(out, t); err != nil {
return err
}
}
activeExceptCurrent := order[1:]
var haveInput []*Vertex
for _, a := range activeExceptCurrent {
if a.HasInput(vtx) && (!b.IsSibling(a, vtx) || tape.ShowEdgesBetweenVerticesInSameGroup) {
// avoid forking for vertexes in the same group; often more noisy than helpful
haveInput = append(haveInput, a)
}
}
for i, g := range groups {
if g != nil {
if g.WitnessedAll() {
groups[i] = nil
}
}
}
if len(haveInput) > 0 {
groups = groups.AddVertex(out, u, tape.Groups, vtx, haveInput)
}
if tape.IsClosed || tape.ShowCompletedLogs || vtx.Completed == nil || vtx.Error != nil {
term := tape.VertexLogs(vtx.Id)
if vtx.Error != nil {
term.SetHeight(term.UsedHeight())
} else {
term.SetHeight(tape.ReasonableTermHeight)
}
if !tape.IsFocused {
buf := new(bytes.Buffer)
prefixOut := ui.NewOutput(buf, termenv.WithProfile(out.Profile))
groups.TermPrefix(prefixOut, u, vtx)
term.SetPrefix(buf.String())
}
if tape.IsClosed {
term.SetHeight(term.UsedHeight())
}
if err := u.RenderTerm(out, term); err != nil {
return err
}
}
}
groups.Reap(out, u, nil)
return nil
}
func (tape *Tape) bouncer() *bouncer {
return &bouncer{
order: tape.ChronologicalVertexIDs,
groups: tape.Groups,
group2vertexes: tape.GroupVertices,
vertices: tape.Vertices,
vertex2groups: tape.VertexGroups,
focus: tape.IsFocused,
showInternal: tape.ShowInternalVertices,
revealErrored: tape.RevealErroredVertices,
}
}
func (tape *Tape) EachVertex(f func(*Vertex, *ui.Vterm) error) error {
tape.l.Lock()
defer tape.l.Unlock()
for _, vtx := range tape.Vertices {
if err := f(vtx, tape.VertexLogs(vtx.Id)); err != nil {
return err
}
}
return nil
}
func (tape *Tape) setZoomHook(fn func(*zoomState)) {
tape.l.Lock()
defer tape.l.Unlock()
tape.zoomHook = fn
}
func (tape *Tape) insert(id string, vtx *Vertex) {
if vtx.Started == nil {
// skip pending vertices; too complicated to deal with
return
}
tape.Vertices[id] = vtx
for i, dig := range tape.ChronologicalVertexIDs {
other := tape.Vertices[dig]
if other.Started.AsTime().After(vtx.Started.AsTime()) {
inserted := make([]string, len(tape.ChronologicalVertexIDs)+1)
copy(inserted, tape.ChronologicalVertexIDs[:i])
inserted[i] = id
copy(inserted[i+1:], tape.ChronologicalVertexIDs[i:])
tape.ChronologicalVertexIDs = inserted
return
}
}
tape.ChronologicalVertexIDs = append(tape.ChronologicalVertexIDs, id)
}
func (tape *Tape) VertexLogs(vertex string) *ui.Vterm {
term, found := tape.vertexLogs[vertex]
if !found {
term = ui.NewVterm()
if tape.Width != -1 {
term.SetWidth(tape.Width)
}
tape.vertexLogs[vertex] = term
}
return term
}
type bouncer struct {
order []string
groups map[string]*Group
vertices map[string]*Vertex
group2vertexes map[string]map[string]struct{}
vertex2groups map[string]map[string]struct{}
focus bool
showInternal bool
revealErrored bool
}
func (b *bouncer) Groups(vtx *Vertex) []*Group {
groups := make([]*Group, 0, len(b.vertex2groups[vtx.Id]))
for id := range b.vertex2groups[vtx.Id] {
group, found := b.groups[id]
if found {
groups = append(groups, group)
}
}
// TODO this is in the hot path, would be better to maintain order instead of
// sorting
sort.Slice(groups, func(i, j int) bool {
return groups[i].Started.AsTime().Before(groups[j].Started.AsTime())
})
return groups
}
func (b *bouncer) IsVisible(vtx *Vertex) bool {
if vtx.Error != nil && b.revealErrored {
// show errored vertices no matter what
return true
}
if vtx.Internal && !b.showInternal {
// filter out internal vertices unless we're showing them
return false
}
if b.focus && !vtx.Focused {
// if we're focusing, ignore unfocused vertices
return false
}
return true
}
func (b *bouncer) VisibleVertices(group *Group) []*Vertex {
var visible []*Vertex
if group == nil {
for _, vid := range b.order {
vtx, found := b.vertices[vid]
if !found {
// shouldn't happen
continue
}
orphaned := len(b.vertex2groups[vid]) == 0
if orphaned && b.IsVisible(vtx) {
visible = append(visible, vtx)
}
}
return visible
}
for vid := range b.group2vertexes[group.Id] {
vtx, found := b.vertices[vid]
if !found {
// shouldn't happen
continue
}
if b.IsVisible(vtx) {
visible = append(visible, vtx)
}
}
return visible
}
func (b *bouncer) Parent(group *Group) (*Group, bool) {
if group.Parent == nil {
return nil, false
}
parent, found := b.groups[group.GetParent()]
return parent, found
}
// TODO cache the hell out of this
func (b *bouncer) HiddenAncestors(group *Group) []*Group {
var hidden []*Group
for {
parent, found := b.Parent(group)
if !found {
break
}
if len(b.VisibleVertices(parent)) == 0 {
hidden = append(hidden, parent)
} else {
break
}
group = parent
}
return hidden
}
func (b *bouncer) VisibleDepth(group *Group) int {
if group.Parent == nil {
return 0
}
var depth int
vs := b.VisibleVertices(group)
if len(vs) > 0 {
depth++
}
parent, found := b.groups[group.GetParent()]
if !found {
return depth
}
return depth + b.VisibleDepth(parent)
}
func (b *bouncer) IsInGroup(vtx *Vertex, group *Group) bool {
groups := b.vertex2groups[vtx.Id]
if len(groups) == 0 {
// vertex is not in any group; default it to the root group
return group.Name == RootGroup
}
_, found := groups[group.Id]
return found
}
func (b *bouncer) IsSubGroup(child, needle *Group) bool {
for parent, found := b.Parent(child); found; parent, found = b.Parent(parent) {
if parent.Id == needle.Id {
return true
}
}
return false
}
func (b *bouncer) IsInGroupOrChild(vtx *Vertex, group *Group) bool {
if b.IsInGroup(vtx, group) {
return true
}
for groupId := range b.vertex2groups[vtx.Id] {
g, found := b.groups[groupId]
if !found {
continue
}
if b.IsSubGroup(g, group) {
return true
}
}
return false
}
func (b *bouncer) IsSibling(x, y *Vertex) bool {
groupsA, found := b.vertex2groups[x.Id]
if !found {
return false
}
groupsB, found := b.vertex2groups[y.Id]
if !found {
return false
}
for id := range groupsA {
if _, found := groupsB[id]; found {
return true
}
}
return false
}
type progressGroups []progressGroup
type progressGroup interface {
ID() string
DirectlyContains(*Vertex) bool
IsActiveVia(*Vertex) bool
Created(*Vertex) bool
Witness(*Vertex)
WitnessedAll() bool
}
type progGroup struct {
*Group
bouncer *bouncer
}
func (gg progGroup) ID() string {
return gg.Id
}
func (gg progGroup) DirectlyContains(vtx *Vertex) bool {
return gg.bouncer.IsInGroup(vtx, gg.Group)
}