-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.go
192 lines (164 loc) · 3.75 KB
/
service.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
package main
import (
"fmt"
"github.com/rs/xid"
"github.com/s8sg/goflow"
"github.com/s8sg/goflow-dashboard/lib"
redis "gopkg.in/redis.v5"
"os"
"strings"
)
var rdb *redis.Client
// listGoFLows get list of go-flows
func listGoFLows() ([]*Flow, error) {
rdb = getRDB()
command := rdb.Keys("goflow-flow:*")
rdb.Process(command)
flowKeys, err := command.Result()
if err != nil {
return nil, nil
}
flows := make([]*Flow, 0)
for _, key := range flowKeys {
flowName := strings.Split(key, ":")[1]
if flowName == "" {
continue
}
flow := &Flow{
Name: flowName,
}
flows = append(flows, flow)
}
return flows, nil
}
// getDot request to dot-generator for the dag dot graph
func getDot(flowName string) (string, error) {
rdb = getRDB()
command := rdb.Get("goflow-flow:" + flowName)
rdb.Process(command)
definition, err := command.Result()
if err != nil {
return "", nil
}
dot, err := lib.MakeDotFromDefinitionString(definition)
return dot, err
}
// listFlowRequests get list of request for a goflow
func listFlowRequests(flow string) (map[string]string, error) {
return lib.ListRequests(flow)
}
// buildFlowDesc get a flow details
func buildFlowDesc(functions []*Flow, flowName string) (*FlowDesc, error) {
var functionObj *Flow
for _, functionObj = range functions {
if functionObj.Name == flowName {
break
}
}
dot, dErr := getDot(flowName)
if dErr != nil {
return nil, fmt.Errorf("failed to get dot, %v", dErr)
}
flowDesc := &FlowDesc{
Name: functionObj.Name,
Dot: dot,
}
return flowDesc, nil
}
// listRequestTraces get list of traces for a request traceID
func listRequestTraces(requestId string, requestTraceId string) (*RequestTrace, error) {
requestTraceResponse, err := lib.ListTraces(requestTraceId)
if err != nil {
return nil, err
}
requestTrace := &RequestTrace{
RequestID: requestId,
TraceId: requestTraceId,
StartTime: requestTraceResponse.StartTime,
NodeTraces: make(map[string]*NodeTrace, 0),
Duration: requestTraceResponse.Duration,
}
for id, nodeTrace := range requestTraceResponse.NodeTraces {
nodeTraceObj := &NodeTrace{
StartTime: nodeTrace.StartTime,
Duration: nodeTrace.Duration,
}
requestTrace.NodeTraces[id] = nodeTraceObj
}
return requestTrace, nil
}
// getRequestState request the flow for the request status
func getRequestState(flow, requestId string) (string, error) {
rdb = getRDB()
return "", nil
}
// executeFlow execute a flow
func executeFlow(flow string, data []byte) (string, error) {
fs := &goflow.FlowService{
RedisURL: getRedisAddr(),
}
requestId := getNewId()
request := &goflow.Request{
Body: data,
RequestId: requestId,
}
err := fs.Execute(flow, request)
if err != nil {
return "", err
}
return requestId, nil
}
// pauseRequest pause a request
func pauseRequest(flow string, requestID string) error {
fs := &goflow.FlowService{
RedisURL: getRedisAddr(),
}
err := fs.Pause(flow, requestID)
if err != nil {
return err
}
return nil
}
// resumeRequest resumes a request
func resumeRequest(flow string, requestID string) error {
fs := &goflow.FlowService{
RedisURL: getRedisAddr(),
}
err := fs.Resume(flow, requestID)
if err != nil {
return err
}
return nil
}
// stopRequest stops a request
func stopRequest(flow string, requestID string) error {
fs := &goflow.FlowService{
RedisURL: getRedisAddr(),
}
err := fs.Stop(flow, requestID)
if err != nil {
return err
}
return nil
}
func getRDB() *redis.Client {
addr := getRedisAddr()
if rdb == nil {
rdb = redis.NewClient(&redis.Options{
Addr: addr,
DB: 0,
})
}
return rdb
}
func getRedisAddr() string {
addr := os.Getenv("REDIS_URL")
if addr == "" {
addr = "localhost:6379"
}
return addr
}
func getNewId() string {
guid := xid.New()
return guid.String()
}