/
runtime_incident_manager.go
191 lines (151 loc) · 4.58 KB
/
runtime_incident_manager.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
package main
import (
"sort"
"strings"
)
// RuntimeIncidentManager manages incidents in the applications runtime.
// These incidents will no longer be available after the application shuts down.
type RuntimeIncidentManager struct {
Incidents map[int64]*Incident // The incidents created.
Attachments map[int][]Attachment // The attachments and the incident association.
}
// AddIncident adds an incident to the runtimes incident collection.
func (manager RuntimeIncidentManager) AddIncident(incident *Incident) bool {
var id = int64(len(manager.Incidents))
incident.Id = id
if incident.Attributes == nil {
incident.Attributes = make(map[string]string, 0)
}
manager.Incidents[id] = incident
return true
}
// GetIncident attempts to get an incident out of the runtimes incident collection.
// If an incident is not found a false will be returned.
func (manager RuntimeIncidentManager) GetIncident(incidentId int) (Incident, bool) {
if val, ok := manager.Incidents[int64(incidentId)]; ok {
return *val, true
}
return Incident{}, false
}
// GetIncidents will get all incidents out of the runtimes incident collection.
func (manager RuntimeIncidentManager) GetIncidents(filter *FilterRequest) ([]Incident, bool) {
retVal := make([]Incident, 0)
for _, v := range manager.Incidents {
if incidentInFilterRequest(*v, filter) {
retVal = append(retVal, *v)
}
}
sort.Slice(retVal, func(i, j int) bool {
return retVal[i].Id < retVal[j].Id
})
return retVal, true
}
func incidentInFilterRequest(incident Incident, filter *FilterRequest) bool {
if filter == nil {
return true
}
if isOrRequest(filter) {
for _, k := range filter.Filters {
if incidentInComplexFilter(incident, k) {
return true
}
}
return false
}
for _, k := range filter.Filters {
if !incidentInComplexFilter(incident, k) {
return false
}
}
return true
}
func incidentInComplexFilter(incident Incident, filter ComplexFilter) bool {
logManager.LogPrintf("Processing complex filter %v\n", filter)
if filter.Children != nil {
logManager.LogPrintf("Processing complex filter with children %v\n", filter.Children)
if isOrFilter(filter) {
for _, v := range filter.Children {
if incidentInComplexFilter(incident, *v) {
return true
}
}
return false
}
for _, v := range filter.Children {
if !incidentInComplexFilter(incident, *v) {
return false
}
}
return true
}
if isOrFilter(filter) {
for _, v := range filter.Filter {
if incidentInFilter(incident, v) {
return true
}
}
return false
}
for _, v := range filter.Filter {
if !incidentInFilter(incident, v) {
return false
}
}
return true
}
func incidentInFilter(incident Incident, filter Filter) bool {
val := getIncidentPropertyValue(filter.Property, incident)
if isEqualsComparision(filter) {
return strings.EqualFold(filter.Value, val)
}
if isContainsComparision(filter) {
return strings.Contains(strings.ToLower(val), strings.ToLower(filter.Value))
}
if isNotEqualsComparision(filter) {
return !strings.EqualFold(filter.Value, val)
}
return false
}
// UpdateIncident will update a given incident in the runtime.
func (manager RuntimeIncidentManager) UpdateIncident(id int, update IncidentUpdate) bool {
if val, ok := manager.Incidents[int64(id)]; ok {
updateIncident(val, update)
return true
}
return false
}
// AddAttachment will create an association between an attachment and an incident in the runtime.
func (manager RuntimeIncidentManager) AddAttachment(incidentId int, attachment Attachment) bool {
if _, ok := manager.Incidents[int64(incidentId)]; ok {
manager.Attachments[incidentId] = append(manager.Attachments[incidentId], attachment)
return true
}
return false
}
// GetAttachments will find all attachments associated with an incident in the runtime.
func (manager RuntimeIncidentManager) GetAttachments(incidentId int) ([]Attachment, bool) {
if val, ok := manager.Attachments[incidentId]; ok {
return val, true
}
attachments := make([]Attachment, 0)
manager.Attachments[incidentId] = attachments
return attachments, true
}
// RemoveAttachment will find and remove an attachment associated with an incident.
func (manager RuntimeIncidentManager) RemoveAttachment(incidentId int, fileName string) bool {
val, ok := manager.Attachments[incidentId]
if !ok {
return false
}
for i, v := range val {
if v.FileName == fileName {
manager.Attachments[incidentId] = append(val[:i], val[i+1:]...)
return true
}
}
return false
}
// CleanUp will do any required cleanup actions on the incident manager.
func (manager RuntimeIncidentManager) CleanUp() {
// No op
}