/
datastore_incident_manager.go
249 lines (188 loc) · 6.45 KB
/
datastore_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"strconv"
"cloud.google.com/go/datastore"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
type DataStoreIncidentManager struct {
Context *context.Context
Connection *datastore.Client
LastKnownId int64
}
type DataStoreIncident struct {
Type string
Id int64
Description string
Reporter string
State string
Attributes []DataStoreIncidentAttribute
}
type DataStoreIncidentAttribute struct {
Name string
Value string
}
func CreateDataStoreClient(projectName string, authFile string) (*context.Context, *datastore.Client) {
context := context.Background()
client, err := datastore.NewClient(context, projectName, option.WithServiceAccountFile(authFile))
if err != nil {
panic(err)
}
return &context, client
}
func (manager DataStoreIncidentManager) AddIncident(incident *Incident) bool {
id := manager.getNextId()
incident.Id = id
dsInc := convertFromIncident(incident)
taskKey := datastore.NameKey("incidents", strconv.FormatInt(id, 10), nil)
logManager.LogPrintln("Attempting to put incident into database")
if _, err := manager.Connection.Put(*manager.Context, taskKey, &dsInc); err != nil {
logManager.LogPrintf("Unable to put incident %v\n", err)
return false
}
manager.LastKnownId = id
logManager.LogPrintln("Incident created in database")
return true
}
func convertFromIncident(incident *Incident) DataStoreIncident {
attributes := make([]DataStoreIncidentAttribute, 0)
for k, v := range incident.Attributes {
attributes = append(attributes, DataStoreIncidentAttribute{Name: k, Value: v})
}
return DataStoreIncident{incident.Type, incident.Id, incident.Description, incident.Reporter, incident.State, attributes}
}
func convertToIncident(incident *DataStoreIncident) Incident {
retVal := Incident{incident.Type, incident.Id, incident.Description, incident.Reporter, incident.State, make(map[string]string, 0)}
for _, v := range incident.Attributes {
retVal.Attributes[v.Name] = v.Value
}
return retVal
}
func (manager DataStoreIncidentManager) getNextId() int64 {
logManager.LogPrintf("Getting next id last known id %v\n", manager.LastKnownId)
q := datastore.NewQuery("incidents").Filter("Id >", manager.LastKnownId)
iter := manager.Connection.Run(*manager.Context, q)
max := manager.LastKnownId
for {
var incident Incident
_, err := iter.Next(&incident)
if err == iterator.Done {
break
}
if err != nil {
logManager.LogPrintf("Got error when attempting to get ids %v\n", err)
break
}
if incident.Id > max {
max = incident.Id
}
}
logManager.LogPrintf("Got new max id %v\n", max+1)
return max + 1
}
func (manager DataStoreIncidentManager) GetIncident(incidentId int) (Incident, bool) {
q := datastore.NewQuery("incidents").Filter("Id =", incidentId)
iter := manager.Connection.Run(*manager.Context, q)
var incident DataStoreIncident
for {
_, err := iter.Next(&incident)
if err == iterator.Done {
break
}
if err != nil {
logManager.LogPrintf("Got error when attempting to get incident %v\n", err)
break
}
}
return convertToIncident(&incident), true
}
func (manager DataStoreIncidentManager) GetIncidents(filter *FilterRequest) ([]Incident, bool) {
retVal := make([]Incident, 0)
q := datastore.NewQuery("incidents")
iter := manager.Connection.Run(*manager.Context, q)
for {
var incident DataStoreIncident
_, err := iter.Next(&incident)
if err == iterator.Done {
break
}
if err != nil {
logManager.LogPrintf("Got error when attempting to get incident %v\n", err)
break
}
inc := convertToIncident(&incident)
// TODO: This is not ideal really we should be having datastore filter our values.
// However looking at the documentation it does not appear that there is a way
// to accomplish these complex filters. So for now we will pull everything and filter
// the values out later
if incidentInFilterRequest(inc, filter) {
retVal = append(retVal, inc)
}
}
return retVal, true
}
func (manager DataStoreIncidentManager) UpdateIncident(id int, incident IncidentUpdate) bool {
logManager.LogPrintf("Got incident update request for %v\n", id)
inc, found := manager.GetIncident(id)
if !found {
return false
}
if !updateIncident(&inc, incident) {
return true
}
dsInc := convertFromIncident(&inc)
taskKey := datastore.NameKey("incidents", strconv.FormatInt(inc.Id, 10), nil)
logManager.LogPrintf("Attempting to update incident %v in database. %v\n", inc.Id, dsInc)
if _, err := manager.Connection.Put(*manager.Context, taskKey, &dsInc); err != nil {
logManager.LogPrintf("Unable to update incident %v\n", err)
return false
}
logManager.LogPrintf("Updated incident %v in database\n", inc.Id)
return true
}
func (manager DataStoreIncidentManager) AddAttachment(incidentId int, attachment Attachment) bool {
parentKey := datastore.NameKey("incidents", strconv.Itoa(incidentId), nil)
taskKey := datastore.NameKey("incidentattachments", attachment.FileName, parentKey)
logManager.LogPrintln("Attempting to put incident attachment into database")
if _, err := manager.Connection.Put(*manager.Context, taskKey, &attachment); err != nil {
logManager.LogPrintf("Unable to put incident attachment %v\n", err)
return false
}
logManager.LogPrintln("Incident attachment created in database")
return true
}
func (manager DataStoreIncidentManager) GetAttachments(incidentId int) ([]Attachment, bool) {
retVal := make([]Attachment, 0)
q := datastore.NewQuery("incidentattachments").Ancestor(datastore.NameKey("incidents", strconv.Itoa(incidentId), nil))
iter := manager.Connection.Run(*manager.Context, q)
var att Attachment
for {
_, err := iter.Next(&att)
if err == iterator.Done {
break
}
if err != nil {
logManager.LogPrintf("Got error when attempting to get incident %v\n", err)
break
}
retVal = append(retVal, att)
}
return retVal, true
}
func (manager DataStoreIncidentManager) RemoveAttachment(incidentId int, fileName string) bool {
parentKey := datastore.NameKey("incidents", strconv.Itoa(incidentId), nil)
taskKey := datastore.NameKey("incidentattachments", fileName, parentKey)
logManager.LogPrintln("Attempting to remove incident attachment from database")
err := manager.Connection.Delete(*manager.Context, taskKey)
if err != nil {
logManager.LogPrintf("Unable to delete attachment %v", err)
return false
}
return true
}
func (manager DataStoreIncidentManager) CleanUp() {
if manager.Connection != nil {
manager.Connection.Close()
}
}