This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
statistics_mongodb_repo.go
131 lines (106 loc) · 2.52 KB
/
statistics_mongodb_repo.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
package db
import (
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
"github.com/globalsign/mgo/bson"
"github.com/keptn/keptn/statistics-service/operations"
"go.mongodb.org/mongo-driver/mongo"
)
const keptnStatsCollection = "keptn-stats"
// StatisticsMongoDBRepo godoc
type StatisticsMongoDBRepo struct {
DbConnection MongoDBConnection
statsCollection *mongo.Collection
}
type HexID struct {
ID primitive.ObjectID `bson:"_id"`
}
// GetStatistics godoc
func (s *StatisticsMongoDBRepo) GetStatistics(from, to time.Time) ([]operations.Statistics, error) {
err := s.getCollection()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
searchOptions := bson.M{}
searchOptions["from"] = bson.M{
"$gt": from,
}
searchOptions["to"] = bson.M{
"$lt": to,
}
cur, err := s.statsCollection.Find(ctx, searchOptions)
defer func() {
if cur == nil {
return
}
cur.Close(ctx)
}()
if err != nil {
return nil, err
}
result := []operations.Statistics{}
if cur.RemainingBatchLength() == 0 {
return nil, ErrNoStatisticsFound
}
for cur.Next(ctx) {
stats := &operations.Statistics{}
err := cur.Decode(stats)
if err != nil {
return nil, err
}
result = append(result, *stats)
}
decodedResult, err := decodeKeys(result)
if err != nil {
return nil, err
}
return decodedResult, nil
}
// StoreStatistics godoc
func (s *StatisticsMongoDBRepo) StoreStatistics(statistics operations.Statistics) error {
encodedStats, err := encodeKeys(&statistics)
if err != nil {
return err
}
err = s.getCollection()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
_, err = s.statsCollection.InsertOne(ctx, encodedStats)
if err != nil {
return err
}
return nil
}
func (s *StatisticsMongoDBRepo) DeleteStatistics(from, to time.Time) error {
err := s.getCollection()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
searchOptions := bson.M{}
searchOptions["from"] = bson.M{
"$gt": from.String(),
}
searchOptions["to"] = bson.M{
"$lt": to.String(),
}
_, err = s.statsCollection.DeleteMany(ctx, searchOptions)
return err
}
func (s *StatisticsMongoDBRepo) getCollection() error {
err := s.DbConnection.EnsureDBConnection()
if err != nil {
return err
}
if s.statsCollection == nil {
s.statsCollection = s.DbConnection.Client.Database(databaseName).Collection(keptnStatsCollection)
}
return nil
}