-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinspect.go
181 lines (140 loc) · 3.36 KB
/
inspect.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
package running
import (
"strings"
)
type Inspector struct {
target *Engine
}
func Inspect(e *Engine) Inspector {
return Inspector{target: e}
}
func (i Inspector) GetNodeBuildersName() []string {
var names []string
if i.target != nil {
i.target.buildersLocker.RLock()
defer i.target.buildersLocker.RUnlock()
for name := range i.target.builders {
names = append(names, name)
}
}
return names
}
func (i Inspector) GetNodeBuildersInfo() map[string]NodeBuilderInfo {
infos := make(map[string]NodeBuilderInfo)
if i.target != nil {
i.target.buildersLocker.RLock()
defer i.target.buildersLocker.RUnlock()
for name, info := range i.target.buildersInfo {
infos[name] = info
}
}
return infos
}
func (i Inspector) GetPlansName() []string {
var names []string
if i.target != nil {
i.target.plansLocker.RLock()
defer i.target.plansLocker.RUnlock()
for name := range i.target.plans {
names = append(names, name)
}
}
return names
}
type PlanInfo struct {
Version string
Vertexes []VertexInfo
Edges []Edge
GlobalProps map[string]interface{}
LabelMap map[string]bool
}
type VertexInfo struct {
VertexName string
NodeInfo NodeInfo
}
type NodeInfo struct {
NodeType string
NodeName string
Props map[string]interface{}
Wrappers []string
ReUse bool
Virtual bool
LabelMap map[string]struct{}
SubNodes []NodeInfo
}
type Edge struct {
From string
To string
}
func (i Inspector) DescribePlan(name string) PlanInfo {
var info PlanInfo
if i.target != nil {
i.target.plansLocker.RLock()
plan := i.target.plans[name]
i.target.plansLocker.RUnlock()
if plan != nil {
plan.locker.RLock()
defer plan.locker.RUnlock()
info.Version = plan.version
info.Vertexes = make([]VertexInfo, 0, len(plan.graph.Vertexes))
for vName, vertex := range plan.graph.Vertexes {
info.Vertexes = append(info.Vertexes, VertexInfo{
VertexName: vName,
NodeInfo: describeNode(plan, vertex.RefRoot.NodeName),
})
for _, vNext := range vertex.Next {
if vNext != nil && vNext.RefRoot != nil {
info.Edges = append(info.Edges, Edge{
From: vName,
To: vNext.RefRoot.NodeName,
})
}
}
}
info.GlobalProps = map[string]interface{}{}
if exportable, ok := plan.props.(ExportableProps); ok {
raw := exportable.Raw()
for k, v := range raw {
if !strings.Contains(k, ".") {
info.GlobalProps[k] = v
}
}
}
}
}
info.LabelMap = make(map[string]bool)
for _, v := range info.Vertexes {
for label := range v.NodeInfo.LabelMap {
info.LabelMap[label] = true
}
}
return info
}
func describeNode(plan *Plan, node string, path ...string) NodeInfo {
ref := plan.graph.NodeRefs[node]
info := NodeInfo{
NodeType: ref.NodeType,
NodeName: ref.NodeName,
Wrappers: ref.Wrappers,
ReUse: ref.ReUse,
Virtual: ref.Virtual,
LabelMap: ref.Labels,
Props: map[string]interface{}{},
SubNodes: make([]NodeInfo, 0, len(ref.SubRefs)),
}
path = append(path, node)
prefix := strings.Join(path, ".")
if exportable, ok := plan.props.(ExportableProps); ok {
raw := exportable.Raw()
for k, v := range raw {
p := strings.LastIndex(k, ".")
if p > 0 && p+1 < len(k) && k[:p] == prefix {
info.Props[k[p+1:]] = v
}
}
}
for _, subRef := range ref.SubRefs {
info.SubNodes = append(info.SubNodes, describeNode(plan, subRef.NodeName, path...))
}
return info
}