-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_traverse.go
125 lines (104 loc) · 3.16 KB
/
node_traverse.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
package sapcontrol
import "strings"
// GetNodeByID returns an AlertNode with specific ID.
func (n AlertNodes) GetNodeByID(id int) (res AlertNode) {
for _, node := range n {
if node.ID == id {
res = node
break
}
}
return res
}
// GetChildNodeByName returns hierarchically below parentID the next node with given name.
func (n AlertNodes) GetChildNodeByName(name string, parentID int) (res AlertNode) {
for _, node := range n {
if strings.ToLower(node.Name) == strings.ToLower(name) && node.Parent == parentID {
res = node
break
}
}
return res
}
// GetNodesByName returns all AlertNodes with given name.
func (n AlertNodes) GetNodesByName(name string) (res AlertNodes) {
for _, node := range n {
if strings.ToLower(node.Name) == strings.ToLower(name) {
res = append(res, node)
}
}
return res
}
// GetNodesByParentID returns all AlertNodes which belong to given parent.
func (n AlertNodes) GetNodesByParentID(parentID int) (res AlertNodes) {
for _, node := range n {
if node.Parent == parentID {
res = append(res, node)
}
}
return res
}
// GetNodesByNameRecursive returns all AlertNodes and theit childs which belong to parent node with given name.
func (n AlertNodes) GetNodesByNameRecursive(name string) (res AlertNodes) {
nodes := n.GetNodesByName(name)
for _, node := range nodes {
subNodes := n.GetNodesByParentIDRecursive(node.ID)
if subNodes != nil {
res = append(res, subNodes...)
}
}
return res
}
// GetNodesByParentIDRecursive returns all AlertNodes and their childs which blong to parent with given ID.
func (n AlertNodes) GetNodesByParentIDRecursive(parentID int) (res AlertNodes) {
nodes := n.GetNodesByParentID(parentID)
res = append(res, nodes...)
// traverse through nodes and find more childs
for _, node := range nodes {
subNodes := n.GetNodesByParentIDRecursive(node.ID)
if subNodes != nil {
res = append(res, subNodes...)
}
}
return res
}
// GetLastNodesByName returns hierarchically last AlertNodes which blong to parent node with given name.
func (n AlertNodes) GetLastNodesByName(name string) (res AlertNodes) {
nodes := n.GetNodesByName(name)
// traverse through nodes and find last childs
for _, node := range nodes {
// check if we have childs
switch n.GetNodesByParentID(node.ID) {
case nil:
// we have no childs, so we are the last node
res = append(res, node)
default:
// we have childs, get their last nodes
resChilds := n.GetLastNodesByParentID(node.ID)
if resChilds != nil {
res = append(res, resChilds...)
}
}
}
return res
}
// GetLastNodesByParentID returns hierarchically last AlertNodes which blong to parent with given ID.
func (n AlertNodes) GetLastNodesByParentID(parentID int) (res AlertNodes) {
nodes := n.GetNodesByParentID(parentID)
// traverse through nodes and find last childs
for _, node := range nodes {
// check if we have childs
switch n.GetNodesByParentID(node.ID) {
case nil:
// we have no childs, so we are the last node
res = append(res, node)
default:
// we have childs, get their last nodes
resChilds := n.GetLastNodesByParentID(node.ID)
if resChilds != nil {
res = append(res, resChilds...)
}
}
}
return res
}