-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexposer.go
240 lines (205 loc) · 4.96 KB
/
exposer.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
package astiencoder
type exposer struct {
e *Encoder
}
func newExposer(e *Encoder) *exposer {
return &exposer{e: e}
}
func (e *exposer) stopEncoder() {
e.e.Stop()
}
// ExposedEncoder represents an exposed encoder.
type ExposedEncoder struct {
Workflows []ExposedWorkflowBase `json:"workflows"`
}
// ExposedWorkflow represents an exposed workflow
type ExposedWorkflow struct {
ExposedWorkflowBase
Edges []ExposedWorkflowEdge `json:"edges"`
Job Job `json:"job"`
Nodes []ExposedWorkflowNode `json:"nodes"`
}
func newExposedWorkflow(w *Workflow) (o ExposedWorkflow) {
// Init
o = ExposedWorkflow{
ExposedWorkflowBase: newExposedWorkflowBase(w),
Edges: []ExposedWorkflowEdge{},
Job: w.j,
Nodes: []ExposedWorkflowNode{},
}
// Loop through children
for _, n := range w.bn.Children() {
o.parseNode(n)
}
return
}
func (w *ExposedWorkflow) parseNode(p Node) {
// Append node
w.Nodes = append(w.Nodes, newExposedWorkflowNode(p))
// Loop through children
for _, c := range p.Children() {
// Append edge
w.Edges = append(w.Edges, newExposedWorkflowEdge(p, c))
// Parse node
w.parseNode(c)
}
}
// ExposedWorkflowBase represents a base exposed encoder workflow
type ExposedWorkflowBase struct {
Name string `json:"name"`
Status string `json:"status"`
}
func newExposedWorkflowBase(w *Workflow) ExposedWorkflowBase {
return ExposedWorkflowBase{
Name: w.name,
Status: w.Status(),
}
}
// ExposedWorkflowEdge represents an exposed workflow edge
type ExposedWorkflowEdge struct {
From string `json:"from"`
To string `json:"to"`
}
func newExposedWorkflowEdge(parent, child Node) ExposedWorkflowEdge {
return ExposedWorkflowEdge{
From: parent.Metadata().Name,
To: child.Metadata().Name,
}
}
// ExposedWorkflowNode represents an exposed workflow node
type ExposedWorkflowNode struct {
Description string `json:"description"`
Label string `json:"label"`
Name string `json:"name"`
Stats []ExposedStatMetadata `json:"stats"`
Status string `json:"status"`
}
// ExposedStatMetadata represents exposed stat metadata
type ExposedStatMetadata struct {
Description string `json:"description"`
Label string `json:"label"`
Unit string `json:"unit"`
}
func newExposedWorkflowNode(n Node) (w ExposedWorkflowNode) {
w = ExposedWorkflowNode{
Description: n.Metadata().Description,
Label: n.Metadata().Label,
Name: n.Metadata().Name,
Stats: []ExposedStatMetadata{},
Status: n.Status(),
}
if s := n.Stater(); s != nil {
for _, v := range s.StatsMetadata() {
w.Stats = append(w.Stats, ExposedStatMetadata{
Description: v.Description,
Label: v.Label,
Unit: v.Unit,
})
}
}
return
}
func (e *exposer) encoder() (o ExposedEncoder) {
e.e.m.Lock()
defer e.e.m.Unlock()
o = ExposedEncoder{
Workflows: []ExposedWorkflowBase{},
}
for _, w := range e.e.ws {
o.Workflows = append(o.Workflows, newExposedWorkflowBase(w))
}
return
}
func (e *exposer) addWorkflow(name string, j Job) (err error) {
_, err = e.e.NewWorkflow(name, j)
return
}
func (e *exposer) workflow(name string) (ew ExposedWorkflow, err error) {
var w *Workflow
if w, err = e.e.Workflow(name); err != nil {
return
}
ew = newExposedWorkflow(w)
return
}
func (e *exposer) continueWorkflow(name string) (err error) {
// Retrieve workflow
var w *Workflow
if w, err = e.e.Workflow(name); err != nil {
return
}
// Continue workflow
w.Continue()
return
}
func (e *exposer) pauseWorkflow(name string) (err error) {
// Retrieve workflow
var w *Workflow
if w, err = e.e.Workflow(name); err != nil {
return
}
// Pause workflow
w.Pause()
return
}
func (e *exposer) startWorkflow(name string) (err error) {
// Retrieve workflow
var w *Workflow
if w, err = e.e.Workflow(name); err != nil {
return
}
// Start workflow
w.Start()
return
}
func (e *exposer) continueNode(workflow, node string) (err error) {
// Retrieve workflow
var w *Workflow
if w, err = e.e.Workflow(workflow); err != nil {
return
}
// Retrieve node
var n Node
if n, err = w.Node(node); err != nil {
return
}
// Continue node
n.Continue()
return
}
func (e *exposer) pauseNode(workflow, node string) (err error) {
// Retrieve workflow
var w *Workflow
if w, err = e.e.Workflow(workflow); err != nil {
return
}
// Retrieve node
var n Node
if n, err = w.Node(node); err != nil {
return
}
// Pause node
n.Pause()
return
}
func (e *exposer) startNode(workflow, node string) (err error) {
// Retrieve workflow
var w *Workflow
if w, err = e.e.Workflow(workflow); err != nil {
return
}
// Retrieve node
var n Node
if n, err = w.Node(node); err != nil {
return
}
// Check workflow status
if w.Status() == StatusRunning {
// Workflow is running, we only start the desired node
w.startNode(n)
} else {
// Workflow is stopped, we start it as well as the desired node
w.start(n)
}
return
}