5
5
"fmt"
6
6
"math/rand"
7
7
"regexp"
8
+ "strconv"
8
9
"strings"
9
10
"sync"
10
11
@@ -45,7 +46,6 @@ func ByNumberAndComponent(tasks []*Task) [][]*TaskNode {
45
46
}
46
47
47
48
var buckets [][]* TaskNode
48
- var lastNode * TaskNode
49
49
for i := 0 ; i < count ; {
50
50
matchBase := matches [i ]
51
51
j := i + 1
@@ -56,26 +56,45 @@ func ByNumberAndComponent(tasks []*Task) [][]*TaskNode {
56
56
break
57
57
}
58
58
if matchBase [groupComponent ] != matchNext [groupComponent ] {
59
- groups = append (groups , & TaskNode {Tasks : tasks [i :j ]})
59
+ node := & TaskNode {
60
+ Tasks : tasks [i :j ],
61
+ component : matchBase [groupComponent ],
62
+ }
63
+ var err error
64
+ if node .runlevel , err = strconv .Atoi (matchBase [groupNumber ]); err != nil {
65
+ panic (err )
66
+ }
67
+ groups = append (groups , node )
60
68
i = j
61
69
}
62
70
matchBase = matchNext
63
71
}
64
72
if len (groups ) > 0 {
65
- groups = append (groups , & TaskNode {Tasks : tasks [i :j ]})
73
+ node := & TaskNode {
74
+ Tasks : tasks [i :j ],
75
+ component : matchBase [groupComponent ],
76
+ }
77
+ var err error
78
+ if node .runlevel , err = strconv .Atoi (matchBase [groupNumber ]); err != nil {
79
+ panic (err )
80
+ }
81
+ groups = append (groups , node )
66
82
i = j
67
83
buckets = append (buckets , groups )
68
- lastNode = nil
69
84
continue
70
85
}
71
- if lastNode == nil {
72
- lastNode = & TaskNode {Tasks : append ([]* Task (nil ), tasks [i :j ]... )}
73
- i = j
74
- buckets = append (buckets , []* TaskNode {lastNode })
75
- continue
86
+ node := & TaskNode {
87
+ Tasks : append ([]* Task (nil ), tasks [i :j ]... ),
88
+ }
89
+ if matchBase != nil {
90
+ node .component = matchBase [groupComponent ]
91
+ var err error
92
+ if node .runlevel , err = strconv .Atoi (matchBase [groupNumber ]); err != nil {
93
+ panic (err )
94
+ }
76
95
}
77
- lastNode .Tasks = append (lastNode .Tasks , tasks [i :j ]... )
78
96
i = j
97
+ buckets = append (buckets , []* TaskNode {node })
79
98
}
80
99
return buckets
81
100
}
@@ -105,19 +124,39 @@ func FlattenByNumberAndComponent(tasks []*Task) [][]*TaskNode {
105
124
break
106
125
}
107
126
if matchBase [groupComponent ] != matchNext [groupComponent ] {
108
- groups = append (groups , & TaskNode {Tasks : tasks [i :j ]})
127
+ node := & TaskNode {
128
+ Tasks : tasks [i :j ],
129
+ component : matchBase [groupComponent ],
130
+ }
131
+ var err error
132
+ if node .runlevel , err = strconv .Atoi (matchBase [groupNumber ]); err != nil {
133
+ panic (err )
134
+ }
135
+ groups = append (groups , node )
109
136
i = j
110
137
}
111
138
matchBase = matchNext
112
139
}
113
140
if len (groups ) > 0 {
114
- groups = append (groups , & TaskNode {Tasks : tasks [i :j ]})
141
+ node := & TaskNode {
142
+ Tasks : tasks [i :j ],
143
+ component : matchBase [groupComponent ],
144
+ }
145
+ var err error
146
+ if node .runlevel , err = strconv .Atoi (matchBase [groupNumber ]); err != nil {
147
+ panic (err )
148
+ }
149
+ groups = append (groups , node )
115
150
i = j
116
151
lastNode = nil
117
152
continue
118
153
}
119
154
if lastNode == nil {
120
- lastNode = & TaskNode {Tasks : append ([]* Task (nil ), tasks [i :j ]... )}
155
+ lastNode = & TaskNode {
156
+ Tasks : append ([]* Task (nil ), tasks [i :j ]... ),
157
+ runlevel : - 1 ,
158
+ component : "FIXME" ,
159
+ }
121
160
i = j
122
161
groups = append (groups , lastNode )
123
162
continue
@@ -137,6 +176,12 @@ type TaskNode struct {
137
176
Tasks []* Task
138
177
// Out is a list of node indices to which there is an edge from this node (=dependents).
139
178
Out []int
179
+
180
+ // runlevel is task's run level, extracted from the manifest filename.
181
+ runlevel int
182
+
183
+ // component is task's component name, extracted from the manifest filename.
184
+ component string
140
185
}
141
186
142
187
func (n * TaskNode ) String () string {
@@ -148,7 +193,7 @@ func (n *TaskNode) String() string {
148
193
}
149
194
arr = append (arr , t .Manifest .GVK .String ())
150
195
}
151
- return "{ Tasks: " + strings .Join (arr , ", " ) + "}"
196
+ return fmt . Sprintf ( "{RunLevel: %d, Component: %q, Tasks: %s}" , n . runlevel , n . component , strings .Join (arr , ", " ))
152
197
}
153
198
154
199
func (n * TaskNode ) replaceIn (index , with int ) {
@@ -370,21 +415,34 @@ func (g *TaskGraph) Roots() []int {
370
415
}
371
416
372
417
// Tree renders the task graph in Graphviz DOT.
373
- func (g * TaskGraph ) Tree () string {
418
+ func (g * TaskGraph ) Tree (granularity string ) string {
374
419
out := []string {
375
420
"digraph tasks {" ,
376
421
" labelloc=t;" ,
377
422
" rankdir=TB;" ,
378
423
}
379
424
for i , node := range g .Nodes {
380
- label := make ([]string , 0 , len (node .Tasks ))
381
- for _ , task := range node .Tasks {
382
- label = append (label , strings .Replace (task .String (), "\" " , "" , - 1 ))
425
+ var label string
426
+ if node .runlevel != 0 || node .component != "" {
427
+ label = fmt .Sprintf ("%02d-%s, %d manifests" , node .runlevel , node .component , len (node .Tasks ))
428
+ } else {
429
+ label = fmt .Sprintf ("%d manifests" , len (node .Tasks ))
383
430
}
384
- if len (label ) == 0 {
385
- label = append (label , "no manifests" )
431
+ switch granularity {
432
+ case "manifest" :
433
+ labels := make ([]string , 0 , len (node .Tasks ))
434
+ for _ , task := range node .Tasks {
435
+ labels = append (labels , strings .Replace (task .String (), "\" " , "" , - 1 ))
436
+ }
437
+ if len (labels ) > 0 {
438
+ label = fmt .Sprintf ("%s\\ n%s" , label , strings .Join (labels , "\\ n" ))
439
+ }
440
+ case "task-node" :
441
+ // nothing to append
442
+ default :
443
+ panic (fmt .Sprintf ("unrecognized granularity %q" , granularity ))
386
444
}
387
- out = append (out , fmt .Sprintf (" %d [ label=\" %s\" shape=\" box\" ];" , i , strings . Join ( label , " \\ n" ) ))
445
+ out = append (out , fmt .Sprintf (" %d [ label=\" %s\" shape=\" box\" ];" , i , label ))
388
446
}
389
447
for i , node := range g .Nodes {
390
448
for _ , j := range node .Out {
0 commit comments