-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
148 lines (134 loc) · 2.71 KB
/
map.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
package main
type ServiceMap struct {
Nodes []Node
Edges []Edge
Paths map[string]Path
}
func NewServiceMap() ServiceMap {
return ServiceMap{
Nodes: make([]Node, 0),
Edges: make([]Edge, 0),
Paths: make(map[string]Path, 0),
}
}
type Node struct {
Name string
}
func (s *ServiceMap) AddNode(name string) {
for _, v := range s.Nodes {
if v.Name == name {
return
}
}
s.Nodes = appendNodeUnique(s.Nodes, Node{Name: name})
}
type Edge struct {
From string
To string
Attributes map[string][]string
}
func (s *ServiceMap) AddPathEdge(path, from, to string, attr map[string][]string) {
if from == "" || to == "" || path == "" {
return
}
for k, v := range s.Edges {
if v.From == from && v.To == to {
s.Edges[k].Attributes = mergeAttributes(v.Attributes, attr)
return
}
}
e := Edge{From: from, To: to, Attributes: attr}
s.Edges = append(s.Edges, e)
p, ok := s.Paths[path]
if !ok {
s.Paths[path] = Path{
Edges: make([]Edge, 0),
}
}
p.Edges = appendEdgeUnique(p.Edges, e)
p.Attributes = mergeAttributes(p.Attributes, e.Attributes)
s.Paths[path] = p
}
type Path struct {
Edges []Edge
RequestRate int
Attributes map[string][]string
}
func (s *ServiceMap) Join(maps ...ServiceMap) {
for _, m := range maps {
s.Nodes = appendNodeUnique(s.Nodes, m.Nodes...)
s.Edges = appendEdgeUnique(s.Edges, m.Edges...)
for k, path := range m.Paths {
v, ok := s.Paths[k]
if !ok {
s.Paths[k] = path
continue
}
v.Attributes = mergeAttributes(path.Attributes)
v.Edges = appendEdgeUnique(v.Edges, path.Edges...)
s.Paths[k] = v
}
}
}
func mergeAttributes(arg ...map[string][]string) map[string][]string {
ret := make(map[string][]string)
for _, attrMap := range arg {
for k, attr := range attrMap {
v, ok := ret[k]
if !ok {
ret[k] = attr
}
ret[k] = appendStringUnique(v, attr...)
}
}
return ret
}
func appendStringUnique(into []string, add ...string) []string {
ret := into
for _, a := range add {
found := false
for _, v := range into {
if a == v {
found = true
break
}
}
if !found {
ret = append(ret, a)
}
}
return ret
}
func appendNodeUnique(into []Node, add ...Node) []Node {
ret := into
for _, a := range add {
found := false
for _, v := range into {
if a.Name == v.Name {
found = true
break
}
}
if !found {
ret = append(ret, a)
}
}
return ret
}
func appendEdgeUnique(into []Edge, add ...Edge) []Edge {
ret := into
for i, a := range add {
found := false
for _, v := range into {
if a.From == v.From && a.To == v.To {
ret[i].Attributes = mergeAttributes(a.Attributes, v.Attributes)
found = true
break
}
}
if !found {
ret = append(ret, a)
}
}
return ret
}