-
Notifications
You must be signed in to change notification settings - Fork 481
/
notifications.go
482 lines (384 loc) · 14.1 KB
/
notifications.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*******************************************************************************
* Copyright 2018 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*******************************************************************************/
package mongo
import (
"github.com/edgexfoundry/edgex-go/internal/pkg/db"
"github.com/edgexfoundry/edgex-go/internal/pkg/db/mongo/models"
contract "github.com/edgexfoundry/go-mod-core-contracts/models"
"github.com/globalsign/mgo/bson"
)
const (
NOTIFICATION_COLLECTION = "notification"
SUBSCRIPTION_COLLECTION = "subscription"
TRANSMISSION_COLLECTION = "transmission"
)
var currentMaxResultCount int // configuration read max limit
var cleanupDefaultAge int
/* ----------------------- Notifications ------------------------*/
func (mc MongoClient) GetNotifications() ([]contract.Notification, error) {
return mc.getNotifications(bson.M{})
}
func (mc MongoClient) GetNotificationById(id string) (contract.Notification, error) {
query, err := idToBsonM(id)
if err != nil {
return contract.Notification{}, err
}
return mc.getNotification(query)
}
func (mc MongoClient) GetNotificationBySlug(slug string) (contract.Notification, error) {
return mc.getNotification(bson.M{"slug": slug})
}
func (mc MongoClient) GetNotificationBySender(sender string, limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"sender": sender}, limit)
}
func (mc MongoClient) GetNotificationsByLabels(labels []string, limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"labels": bson.M{"$in": labels}}, limit)
}
func (mc MongoClient) GetNotificationsByStartEnd(start int64, end int64, limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"created": bson.M{"$gt": start, "$lt": end}}, limit)
}
func (mc MongoClient) GetNotificationsByStart(start int64, limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"created": bson.M{"$gt": start}}, limit)
}
func (mc MongoClient) GetNotificationsByEnd(end int64, limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"created": bson.M{"$lt": end}}, limit)
}
func (mc MongoClient) GetNewNotifications(limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"status": "NEW"}, limit)
}
func (mc MongoClient) GetNewNormalNotifications(limit int) ([]contract.Notification, error) {
return mc.getNotificationsLimit(bson.M{"status": "NEW", "severity": "NORMAL"}, limit)
}
func (mc MongoClient) AddNotification(n contract.Notification) (string, error) {
s := mc.getSessionCopy()
defer s.Close()
var mapped models.Notification
id, err := mapped.FromContract(n)
if err != nil {
return "", err
}
if err = mc.checkNotificationSlugIntegrity(mapped.Slug); err != nil {
return "", err
}
mapped.TimestampForAdd()
if err = s.DB(mc.database.Name).C(NOTIFICATION_COLLECTION).Insert(mapped); err != nil {
return "", errorMap(err)
}
return id, err
}
func (mc MongoClient) UpdateNotification(n contract.Notification) error {
return mc.updateNotification(n)
}
func (mc MongoClient) MarkNotificationProcessed(n contract.Notification) error {
n.Status = contract.NotificationsStatus(contract.Processed)
return mc.updateNotification(n)
}
func (mc MongoClient) DeleteNotificationById(id string) error {
mn, err := mc.GetNotificationById(id)
if err == db.ErrNotFound {
return err
}
return mc.deleteNotificationAndAssociatedTransmissions(mn)
}
func (mc MongoClient) DeleteNotificationBySlug(slug string) error {
mn, err := mc.GetNotificationBySlug(slug)
if err == db.ErrNotFound {
return err
}
return mc.deleteNotificationAndAssociatedTransmissions(mn)
}
func (mc MongoClient) DeleteNotificationsOld(age int) error {
currentTime := db.MakeTimestamp()
end := int(currentTime) - age
query := bson.M{"modified": bson.M{
"$lt": end}, "status": "PROCESSED"}
mns, err := mc.getNotifications(query)
if err == db.ErrNotFound {
return err
}
for _, mn := range mns {
if err := mc.deleteNotificationAndAssociatedTransmissions(mn); err != nil {
return err
}
}
return nil
}
func (mc MongoClient) deleteNotificationAndAssociatedTransmissions(n contract.Notification) error {
if err := mc.deleteAll(bson.M{"notification.slug": n.Slug}, TRANSMISSION_COLLECTION); err != nil {
return err
}
return mc.deleteByObjectID(n.ID, NOTIFICATION_COLLECTION)
}
func (mc MongoClient) checkNotificationSlugIntegrity(slug string) error {
if slug == "" {
return db.ErrSlugEmpty
}
if _, err := mc.getNotification(bson.M{"slug": slug}); err == nil {
return db.ErrNotUnique
}
return nil
}
func (mc MongoClient) updateNotification(n contract.Notification) error {
var mapped models.Notification
id, err := mapped.FromContract(n)
if err != nil {
return err
}
mapped.TimestampForUpdate()
return mc.updateId(NOTIFICATION_COLLECTION, id, mapped)
}
func (mc MongoClient) getNotification(q bson.M) (contract.Notification, error) {
s := mc.getSessionCopy()
defer s.Close()
var model models.Notification
if err := errorMap(s.DB(mc.database.Name).C(NOTIFICATION_COLLECTION).Find(q).One(&model)); err == db.ErrNotFound {
return contract.Notification{}, err
}
return model.ToContract(), nil
}
func (mc MongoClient) getNotifications(q bson.M) ([]contract.Notification, error) {
return mc.getNotificationsLimit(q, currentMaxResultCount)
}
func (mc MongoClient) getNotificationsLimit(q bson.M, limit int) ([]contract.Notification, error) {
s := mc.getSessionCopy()
defer s.Close()
// Check if limit is 0
if limit == 0 {
return []contract.Notification{}, nil
}
var notifications []models.Notification
if err := s.DB(mc.database.Name).C(NOTIFICATION_COLLECTION).Find(q).Limit(limit).All(¬ifications); err != nil {
return []contract.Notification{}, errorMap(err)
}
contracts := []contract.Notification{}
for _, model := range notifications {
contracts = append(contracts, model.ToContract())
}
return contracts, nil
}
/* ----------------------- Subscriptions ------------------------*/
func (mc MongoClient) GetSubscriptionBySlug(slug string) (contract.Subscription, error) {
return mc.getSubscription(bson.M{"slug": slug})
}
func (mc MongoClient) GetSubscriptionByCategories(categories []string) ([]contract.Subscription, error) {
return mc.getSubscriptions(bson.M{"subscribedCategories": bson.M{"$in": categories}})
}
func (mc MongoClient) GetSubscriptionByLabels(labels []string) ([]contract.Subscription, error) {
return mc.getSubscriptions(bson.M{"subscribedLabels": bson.M{"$in": labels}})
}
func (mc MongoClient) GetSubscriptionByCategoriesLabels(categories []string, labels []string) ([]contract.Subscription, error) {
return mc.getSubscriptions(bson.M{"subscribedCategories": bson.M{"$in": categories}, "subscribedLabels": bson.M{"$in": labels}})
}
func (mc MongoClient) GetSubscriptionByReceiver(receiver string) ([]contract.Subscription, error) {
return mc.getSubscriptions(bson.M{"receiver": receiver})
}
func (mc MongoClient) GetSubscriptionById(id string) (contract.Subscription, error) {
query, err := idToBsonM(id)
if err != nil {
return contract.Subscription{}, err
}
return mc.getSubscription(query)
}
func (mc MongoClient) DeleteSubscriptionById(id string) error {
return mc.deleteByObjectID(id, SUBSCRIPTION_COLLECTION)
}
func (mc MongoClient) AddSubscription(sub contract.Subscription) (string, error) {
s := mc.getSessionCopy()
defer s.Close()
var mapped models.Subscription
id, err := mapped.FromContract(sub)
if err != nil {
return "", err
}
if err = mc.checkSubscriptionSlugIntegrity(mapped.Slug); err != nil {
return "", err
}
mapped.TimestampForAdd()
if err = s.DB(mc.database.Name).C(SUBSCRIPTION_COLLECTION).Insert(mapped); err != nil {
return "", errorMap(err)
}
return id, nil
}
func (mc MongoClient) UpdateSubscription(sub contract.Subscription) error {
var mapped models.Subscription
id, err := mapped.FromContract(sub)
if err != nil {
return err
}
mapped.TimestampForUpdate()
return mc.updateId(SUBSCRIPTION_COLLECTION, id, mapped)
}
func (mc MongoClient) DeleteSubscriptionBySlug(slug string) error {
ms, err := mc.GetSubscriptionBySlug(slug)
if err == db.ErrNotFound {
return err
}
return mc.deleteByObjectID(ms.ID, SUBSCRIPTION_COLLECTION)
}
// Return all the subscriptions
// UnexpectedError - failed to retrieve subscriptions from the database
func (mc MongoClient) GetSubscriptions() ([]contract.Subscription, error) {
return mc.getSubscriptions(bson.M{})
}
func (mc MongoClient) checkSubscriptionSlugIntegrity(slug string) error {
if slug == "" {
return db.ErrSlugEmpty
}
_, err := mc.getSubscription(bson.M{"slug": slug})
if err == nil {
return db.ErrNotUnique
}
return nil
}
func (mc MongoClient) getSubscription(q bson.M) (c contract.Subscription, err error) {
s := mc.getSessionCopy()
defer s.Close()
var sub models.Subscription
if err = s.DB(mc.database.Name).C(SUBSCRIPTION_COLLECTION).Find(q).One(&sub); err != nil {
return c, errorMap(err)
}
c = sub.ToContract()
return
}
func (mc MongoClient) getSubscriptions(q bson.M) (c []contract.Subscription, err error) {
s := mc.getSessionCopy()
defer s.Close()
var subs []models.Subscription
if err = errorMap(s.DB(mc.database.Name).C(SUBSCRIPTION_COLLECTION).Find(q).All(&subs)); err != nil {
return
}
c = []contract.Subscription{}
for _, sub := range subs {
c = append(c, sub.ToContract())
}
return
}
/* ----------------------- Transmissions ------------------------*/
// limits for transmissions here refer to resend counts
func (mc MongoClient) AddTransmission(t contract.Transmission) (string, error) {
s := mc.getSessionCopy()
defer s.Close()
var mapped models.Transmission
id, err := mapped.FromContract(t)
if err != nil {
return "", err
}
mapped.TimestampForAdd()
if err = s.DB(mc.database.Name).C(TRANSMISSION_COLLECTION).Insert(mapped); err != nil {
return "", errorMap(err)
}
return id, nil
}
func (mc MongoClient) UpdateTransmission(t contract.Transmission) error {
var mapped models.Transmission
id, err := mapped.FromContract(t)
if err != nil {
return err
}
mapped.TimestampForUpdate()
return mc.updateId(TRANSMISSION_COLLECTION, id, mapped)
}
func (mc MongoClient) DeleteTransmission(age int64, status contract.TransmissionStatus) error {
currentTime := db.MakeTimestamp()
end := currentTime - age
return mc.deleteAll(bson.M{"modified": bson.M{"$lt": end}, "status": status}, TRANSMISSION_COLLECTION)
}
func (mc MongoClient) GetTransmissionById(id string) (contract.Transmission, error) {
query, err := idToBsonM(id)
if err != nil {
return contract.Transmission{}, err
}
return mc.getTransmission(query)
}
func (mc MongoClient) GetTransmissionsByNotificationSlug(slug string, limit int) ([]contract.Transmission, error) {
return mc.getTransmissionsLimit(bson.M{"notification.slug": slug}, limit)
}
func (mc MongoClient) GetTransmissionsByNotificationSlugAndStartEnd(slug string, start int64, end int64, limit int) ([]contract.Transmission, error) {
return mc.getTransmissionsLimit(bson.M{"notification.slug": slug, "created": bson.M{"$gt": start, "$lt": end}}, limit)
}
func (mc MongoClient) GetTransmissionsByStartEnd(start int64, end int64, limit int) ([]contract.Transmission, error) {
return mc.getTransmissionsLimit(bson.M{"created": bson.M{"$gt": start, "$lt": end}}, limit)
}
func (mc MongoClient) GetTransmissionsByStart(start int64, limit int) ([]contract.Transmission, error) {
return mc.getTransmissionsLimit(bson.M{"created": bson.M{"$gt": start}}, limit)
}
func (mc MongoClient) GetTransmissionsByEnd(end int64, limit int) ([]contract.Transmission, error) {
return mc.getTransmissionsLimit(bson.M{"created": bson.M{"$lt": end}}, limit)
}
func (mc MongoClient) GetTransmissionsByStatus(limit int, status contract.TransmissionStatus) ([]contract.Transmission, error) {
return mc.getTransmissionsLimit(bson.M{"status": status}, limit)
}
func (mc MongoClient) getTransmission(q bson.M) (c contract.Transmission, err error) {
s := mc.getSessionCopy()
defer s.Close()
var t models.Transmission
if err = errorMap(s.DB(mc.database.Name).C(TRANSMISSION_COLLECTION).Find(q).One(&t)); err != nil {
return
}
c = t.ToContract()
return
}
func (mc MongoClient) getTransmissionsLimit(q bson.M, limit int) (c []contract.Transmission, err error) {
s := mc.getSessionCopy()
defer s.Close()
if limit == 0 {
return []contract.Transmission{}, nil
}
var transmissions []models.Transmission
if err = errorMap(s.DB(mc.database.Name).C(TRANSMISSION_COLLECTION).Find(q).Limit(limit).All(&transmissions)); err != nil {
return
}
c = []contract.Transmission{}
for _, transmission := range transmissions {
c = append(c, transmission.ToContract())
}
return
}
/* ----------------------- General Deletion ------------------------*/
func (mc MongoClient) deleteByObjectID(id string, col string) (err error) {
s := mc.getSessionCopy()
defer s.Close()
query, err := idToBsonM(id)
if err != nil {
return
}
return errorMap(s.DB(mc.database.Name).C(col).Remove(query))
}
func (mc MongoClient) deleteAll(q bson.M, col string) error {
s := mc.getSessionCopy()
defer s.Close()
_, err := s.DB(mc.database.Name).C(col).RemoveAll(q)
return errorMap(err)
}
/* ----------------------- General Cleanup ------------------------*/
func (mc MongoClient) Cleanup() error {
return mc.CleanupOld(cleanupDefaultAge)
}
func (mc MongoClient) CleanupOld(age int) error {
currentTime := db.MakeTimestamp()
end := int(currentTime) - age
query := bson.M{"modified": bson.M{"$lt": end}}
mns, err := mc.getNotifications(query)
if err == db.ErrNotFound {
return err
}
for _, mn := range mns {
err = mc.deleteNotificationAndAssociatedTransmissions(mn)
if err != nil {
return err
}
}
return nil
}