-
Notifications
You must be signed in to change notification settings - Fork 2
/
changes.go
58 lines (53 loc) · 2.07 KB
/
changes.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
package db
import (
"time"
"github.com/containerum/kube-client/pkg/model"
"github.com/globalsign/mgo/bson"
)
func (mongo *MongoStorage) GetChangesList(namespace, resource, collectionName string, limit int, startTime time.Time) ([]model.Event, error) {
mongo.logger.WithField("collection", collectionName).Debugf("getting changes")
var collection = mongo.db.C(collectionName)
result := make([]model.Event, 0)
if err := collection.Find(bson.M{
"resourcenamespace": namespace,
"resourcename": resource,
"dateadded": bson.M{
"$gte": startTime,
},
}).Sort("-eventtime").Limit(limit).All(&result); err != nil {
mongo.logger.WithError(err).Errorf("unable to get changes")
return nil, PipErr{error: err}.ToMongerr().NotFoundToNil().Extract()
}
return result, nil
}
func (mongo *MongoStorage) GetChangesInNamespacesList(collectionName string, limit int, startTime time.Time, namespaces ...string) ([]model.Event, error) {
mongo.logger.WithField("collection", collectionName).Debugf("getting changes in namespace")
var collection = mongo.db.C(collectionName)
result := make([]model.Event, 0)
if err := collection.Find(bson.M{
"resourcenamespace": bson.M{
"$in": namespaces,
},
"dateadded": bson.M{
"$gte": startTime,
},
}).Sort("-eventtime").Limit(limit).All(&result); err != nil {
mongo.logger.WithError(err).Errorf("unable to get changes in namespace")
return nil, PipErr{error: err}.ToMongerr().NotFoundToNil().Extract()
}
return result, nil
}
func (mongo *MongoStorage) GetAllChangesList(collectionName string, limit int, startTime time.Time) ([]model.Event, error) {
mongo.logger.WithField("collection", collectionName).Debugf("getting changes in all namespaces")
var collection = mongo.db.C(collectionName)
result := make([]model.Event, 0)
if err := collection.Find(bson.M{
"dateadded": bson.M{
"$gte": startTime,
},
}).Sort("-eventtime").Limit(limit).All(&result); err != nil {
mongo.logger.WithError(err).Errorf("unable to get changes in all namespaces")
return nil, PipErr{error: err}.ToMongerr().NotFoundToNil().Extract()
}
return result, nil
}