forked from asticode/go-astiencoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
208 lines (180 loc) · 4.56 KB
/
server.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
package astiencoder
import (
"encoding/json"
"fmt"
"net/http"
"github.com/asticode/go-astikit"
)
type Server struct {
l astikit.SeverityLogger
n ServerNotifierFunc
w *Workflow
}
type ServerNotifierFunc func(eventName EventName, payload interface{})
type ServerOptions struct {
Logger astikit.StdLogger
NotifierFunc ServerNotifierFunc
}
func NewServer(o ServerOptions) *Server {
return &Server{
l: astikit.AdaptStdLogger(o.Logger),
n: o.NotifierFunc,
}
}
func (s *Server) SetWorkflow(w *Workflow) {
s.w = w
}
func serverEventHandlerAdapter(eh *EventHandler, fn func(name EventName, payload interface{})) {
// Register catch all handler
eh.AddForAll(func(e Event) bool {
// Get payload
var p interface{}
switch e.Name {
case EventNameError:
p = astikit.ErrorCause(e.Payload.(error))
case EventNameNodeChildAdded, EventNameNodeChildRemoved:
if e.Payload.(Node).Status() == StatusCreated || e.Target.(Node).Status() == StatusCreated {
return false
}
p = ServerChildUpdate{
Child: e.Payload.(Node).Metadata().Name,
Parent: e.Target.(Node).Metadata().Name,
}
case EventNameNodeClosed, EventNameNodeContinued, EventNameNodePaused, EventNameNodeStopped:
p = e.Target.(Node).Metadata().Name
case EventNameNodeStarted:
p = newServerNode(e.Target.(Node))
case EventNameStats:
p = newServerStats(e)
default:
return false
}
// Custom
fn(e.Name, p)
return false
})
}
func (s *Server) EventHandlerAdapter(eh *EventHandler) {
if s.n == nil {
return
}
serverEventHandlerAdapter(eh, s.n)
}
type ServerWorkflow struct {
Name string `json:"name"`
Nodes []ServerNode `json:"nodes"`
Status string `json:"status"`
}
func newServerWorkflow(w *Workflow) (sw *ServerWorkflow) {
// Create server workflow
sw = &ServerWorkflow{
Name: w.Name(),
Nodes: []ServerNode{},
Status: w.Status(),
}
// Discover nodes
ns := make(map[string]ServerNode)
for _, n := range w.Children() {
discoverServerNode(n, ns)
}
// Add nodes
for _, n := range ns {
sw.Nodes = append(sw.Nodes, n)
}
return
}
func discoverServerNode(n Node, ns map[string]ServerNode) {
// Node has already been discovered
if _, ok := ns[n.Metadata().Name]; ok {
return
}
// Create server node
ns[n.Metadata().Name] = newServerNode(n)
// Discover children
for _, n := range n.Children() {
discoverServerNode(n, ns)
}
}
type ServerNode struct {
Children []string `json:"children"`
Closed bool `json:"closed"`
Description string `json:"description"`
Label string `json:"label"`
Name string `json:"name"`
Parents []string `json:"parents"`
Status string `json:"status"`
Tags []string `json:"tags"`
}
func newServerNode(n Node) (s ServerNode) {
// Create node
s = ServerNode{
Children: []string{},
Closed: n.IsClosed(),
Description: n.Metadata().Description,
Label: n.Metadata().Label,
Name: n.Metadata().Name,
Parents: []string{},
Status: n.Status(),
Tags: n.Metadata().Tags,
}
// Add children
for _, n := range n.Children() {
s.Children = append(s.Children, n.Metadata().Name)
}
// Add parents
for _, n := range n.Parents() {
s.Parents = append(s.Parents, n.Metadata().Name)
}
return
}
type ServerChildUpdate struct {
Child string `json:"child"`
Parent string `json:"parent"`
}
func newServerStats(e Event) (ss []ServerStat) {
for _, es := range e.Payload.([]EventStat) {
ss = append(ss, newServerStat(es))
}
return
}
type ServerStat struct {
Description string `json:"description"`
Label string `json:"label"`
Name string `json:"name"`
Target string `json:"target"`
Unit string `json:"unit"`
Value interface{} `json:"value"`
}
func newServerStat(e EventStat) (s ServerStat) {
s = ServerStat{
Description: e.Description,
Label: e.Label,
Name: e.Name,
Unit: e.Unit,
Value: e.Value,
}
if n, ok := e.Target.(Node); ok {
s.Target = n.Metadata().Name
} else if w, ok := e.Target.(*Workflow); ok {
s.Target = w.Name()
}
return
}
type ServerWelcome struct {
Workflow *ServerWorkflow `json:"workflow,omitempty"`
}
func (s *Server) ServeWelcome() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// Create body
var b ServerWelcome
if s.w != nil {
b.Workflow = newServerWorkflow(s.w)
}
// Write
if err := json.NewEncoder(rw).Encode(b); err != nil {
s.l.Error(fmt.Errorf("astiencoder: writing failed: %w", err))
rw.WriteHeader(http.StatusInternalServerError)
return
}
})
}