-
Notifications
You must be signed in to change notification settings - Fork 20
/
inspect.go
157 lines (143 loc) · 4.26 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
package flows
import (
"fmt"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/utils"
)
type FlowInfo struct {
Dependencies *Dependencies `json:"dependencies"`
Results []*ResultInfo `json:"results"`
WaitingExits []ExitUUID `json:"waiting_exits"`
}
type Dependencies struct {
Classifiers []*assets.ClassifierReference `json:"classifiers,omitempty"`
Channels []*assets.ChannelReference `json:"channels,omitempty"`
Contacts []*ContactReference `json:"contacts,omitempty"`
Fields []*assets.FieldReference `json:"fields,omitempty"`
Flows []*assets.FlowReference `json:"flows,omitempty"`
Groups []*assets.GroupReference `json:"groups,omitempty"`
Labels []*assets.LabelReference `json:"labels,omitempty"`
Templates []*assets.TemplateReference `json:"templates,omitempty"`
}
// NewDependencies creates a new dependency listing from the slice of references
func NewDependencies(refs []assets.Reference) *Dependencies {
d := &Dependencies{}
for _, r := range refs {
switch typed := r.(type) {
case *assets.ChannelReference:
d.Channels = append(d.Channels, typed)
case *assets.ClassifierReference:
d.Classifiers = append(d.Classifiers, typed)
case *ContactReference:
d.Contacts = append(d.Contacts, typed)
case *assets.FieldReference:
d.Fields = append(d.Fields, typed)
case *assets.FlowReference:
d.Flows = append(d.Flows, typed)
case *assets.GroupReference:
d.Groups = append(d.Groups, typed)
case *assets.LabelReference:
d.Labels = append(d.Labels, typed)
case *assets.TemplateReference:
d.Templates = append(d.Templates, typed)
default:
panic(fmt.Sprintf("unknown dependency type reference: %v", r))
}
}
return d
}
// Check checks the asset dependencies and notifies the caller of missing assets via the callback
func (d *Dependencies) Check(sa SessionAssets, missing assets.MissingCallback) error {
for _, ref := range d.Channels {
if sa.Channels().Get(ref.UUID) == nil {
missing(ref, nil)
}
}
for _, ref := range d.Classifiers {
if sa.Classifiers().Get(ref.UUID) == nil {
missing(ref, nil)
}
}
for _, ref := range d.Fields {
if sa.Fields().Get(ref.Key) == nil {
missing(ref, nil)
}
}
for _, ref := range d.Flows {
_, err := sa.Flows().Get(ref.UUID)
if err != nil {
missing(ref, err)
}
}
for _, ref := range d.Groups {
if sa.Groups().Get(ref.UUID) == nil {
missing(ref, nil)
}
}
for _, ref := range d.Labels {
if sa.Labels().Get(ref.UUID) == nil {
missing(ref, nil)
}
}
for _, ref := range d.Templates {
if sa.Templates().Get(ref.UUID) == nil {
missing(ref, nil)
}
}
return nil
}
// ResultInfo is possible result that a flow might generate
type ResultInfo struct {
Key string `json:"key"`
Name string `json:"name"`
Categories []string `json:"categories"`
NodeUUIDs []NodeUUID `json:"node_uuids"`
}
// NewResultInfo creates a new result spec
func NewResultInfo(name string, categories []string, node Node) *ResultInfo {
return &ResultInfo{
Key: utils.Snakify(name),
Name: name,
Categories: categories,
NodeUUIDs: []NodeUUID{node.UUID()},
}
}
func (r *ResultInfo) String() string {
return fmt.Sprintf("key=%s|name=%s|categories=%s", r.Key, r.Name, strings.Join(r.Categories, ","))
}
// MergeResultInfos merges result specs based on key
func MergeResultInfos(specs []*ResultInfo) []*ResultInfo {
merged := make([]*ResultInfo, 0, len(specs))
byKey := make(map[string]*ResultInfo)
for _, spec := range specs {
existing := byKey[spec.Key]
// merge if we already have a result info with this key
if existing != nil {
// merge categories
for _, category := range spec.Categories {
if !utils.StringSliceContains(existing.Categories, category, false) {
existing.Categories = append(existing.Categories, category)
}
}
// merge node UUIDs
for _, nodeUUID := range spec.NodeUUIDs {
uuidSeen := false
for _, u := range existing.NodeUUIDs {
if u == nodeUUID {
uuidSeen = true
break
}
}
if !uuidSeen {
existing.NodeUUIDs = append(existing.NodeUUIDs, nodeUUID)
}
}
} else {
// if not, add as new unique result spec
merged = append(merged, spec)
byKey[spec.Key] = spec
}
}
return merged
}