This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
mongodb_log_repo.go
180 lines (152 loc) · 4.91 KB
/
mongodb_log_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
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
package db
import (
"context"
"fmt"
"github.com/benbjohnson/clock"
"github.com/keptn/go-utils/pkg/common/timeutils"
"github.com/keptn/keptn/shipyard-controller/models"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
const logCollectionName = "keptnErrorLogs"
type MongoDBLogRepo struct {
DbConnection *MongoDBConnection
TheClock clock.Clock
}
func NewMongoDBLogRepo(dbConnection *MongoDBConnection) *MongoDBLogRepo {
return &MongoDBLogRepo{DbConnection: dbConnection, TheClock: clock.New()}
}
func (mdbrepo *MongoDBLogRepo) SetupTTLIndex(duration time.Duration) error {
collection, ctx, cancel, err := mdbrepo.getCollectionAndContext()
if err != nil {
return fmt.Errorf("could not get collection: %s", err.Error())
}
defer cancel()
return SetupTTLIndex(ctx, "time", duration, collection)
}
func (mdbrepo *MongoDBLogRepo) CreateLogEntries(entries []models.LogEntry) error {
collection, ctx, cancel, err := mdbrepo.getCollectionAndContext()
if err != nil {
return fmt.Errorf("could not get collection: %s", err.Error())
}
defer cancel()
var inserts = []interface{}{}
for index := range entries {
if entries[index].Time.IsZero() {
entries[index].Time = mdbrepo.TheClock.Now().UTC()
}
inserts = append(inserts, entries[index])
}
_, err = collection.InsertMany(ctx, inserts)
if err != nil {
return err
}
return nil
}
func (mdbrepo *MongoDBLogRepo) GetLogEntries(params models.GetLogParams) (*models.GetLogsResponse, error) {
collection, ctx, cancel, err := mdbrepo.getCollectionAndContext()
if err != nil {
return nil, err
}
defer cancel()
searchOptions, err := mdbrepo.getSearchOptions(params.LogFilter)
if err != nil {
return nil, err
}
totalCount, err := collection.CountDocuments(ctx, searchOptions)
if err != nil {
return nil, fmt.Errorf("error counting elements in events collection: %v", err)
}
sortOptions := options.Find().SetSort(bson.D{{Key: "time", Value: -1}}).SetSkip(params.NextPageKey)
if params.PageSize > 0 {
sortOptions = sortOptions.SetLimit(params.PageSize)
}
cur, err := collection.Find(ctx, searchOptions, sortOptions)
defer closeCursor(ctx, cur)
if err != nil && err != mongo.ErrNoDocuments {
return nil, err
}
result := &models.GetLogsResponse{
Logs: []models.LogEntry{},
NextPageKey: 0,
PageSize: 0,
TotalCount: totalCount,
}
logs := []models.LogEntry{}
if params.PageSize > 0 && params.PageSize+params.NextPageKey < totalCount {
result.NextPageKey = params.PageSize + params.NextPageKey
}
for cur.Next(ctx) {
logEntry := &models.LogEntry{}
if err := cur.Decode(logEntry); err != nil {
log.Errorf("could not decode log entry: %s", err.Error())
}
logs = append(logs, *logEntry)
}
result.Logs = logs
return result, nil
}
func (mdbrepo *MongoDBLogRepo) DeleteLogEntries(params models.DeleteLogParams) error {
collection, ctx, cancel, err := mdbrepo.getCollectionAndContext()
if err != nil {
return err
}
defer cancel()
searchOptions, err := mdbrepo.getSearchOptions(params.LogFilter)
if err != nil {
return err
}
_, err = collection.DeleteMany(ctx, searchOptions)
if err != nil {
return fmt.Errorf("could not delete log entries: %s", err)
}
return nil
}
func (mdbrepo *MongoDBLogRepo) getSearchOptions(filter models.LogFilter) (bson.M, error) {
searchOptions := bson.M{}
if filter.IntegrationID != "" {
searchOptions["integrationid"] = filter.IntegrationID
}
if filter.FromTime != "" {
fromTime, err := time.Parse(timeutils.KeptnTimeFormatISO8601, filter.FromTime)
if err != nil {
return nil, fmt.Errorf("could not parse provided fromTime %s: %s", filter.FromTime, err.Error())
}
if filter.BeforeTime == "" {
searchOptions["time"] = bson.M{
"$gte": fromTime,
}
} else {
beforeTime, err := time.Parse(timeutils.KeptnTimeFormatISO8601, filter.BeforeTime)
if err != nil {
return nil, fmt.Errorf("could not parse provided beforeTime %s: %s", filter.BeforeTime, err.Error())
}
searchOptions["$and"] = []bson.M{
{"time": bson.M{"$gte": fromTime}},
{"time": bson.M{"$lte": beforeTime}},
}
}
}
if filter.FromTime == "" && filter.BeforeTime != "" {
beforeTime, err := time.Parse(timeutils.KeptnTimeFormatISO8601, filter.BeforeTime)
if err != nil {
return nil, fmt.Errorf("could not parse provided beforeTime %s: %s", filter.BeforeTime, err.Error())
}
searchOptions["time"] = bson.M{
"$lte": beforeTime,
}
}
return searchOptions, nil
}
func (mdbrepo *MongoDBLogRepo) getCollectionAndContext() (*mongo.Collection, context.Context, context.CancelFunc, error) {
err := mdbrepo.DbConnection.EnsureDBConnection()
if err != nil {
return nil, nil, nil, err
}
collection := mdbrepo.DbConnection.Client.Database(getDatabaseName()).Collection(logCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
return collection, ctx, cancel, nil
}