-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmongo.go
More file actions
188 lines (163 loc) · 4.21 KB
/
mongo.go
File metadata and controls
188 lines (163 loc) · 4.21 KB
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
package mongo
import (
"context"
"fmt"
"strings"
"time"
"github.com/linuxboot/contest/cmds/admin_server/storage"
"github.com/linuxboot/contest/pkg/xcontext"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
/*
DefaultDB and DefaultCollection refere to the database and collection initiated by docker/mongo/initdb.js file
*/
DefaultDB = "admin-server-db"
DefaultCollection = "logs"
)
type MongoStorage struct {
dbClient *mongo.Client
collection *mongo.Collection
}
func NewMongoStorage(ctx xcontext.Context, uri string) (*MongoStorage, error) {
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
return nil, err
}
err = client.Connect(ctx)
if err != nil {
return nil, err
}
// check that the server is alive
err = client.Ping(context.Background(), nil)
if err != nil {
return nil, fmt.Errorf("Err while pinging mongo server: %w", err)
}
collection := client.Database(DefaultDB).Collection(DefaultCollection)
return &MongoStorage{
dbClient: client,
collection: collection,
}, nil
}
func toMongoQuery(query storage.Query) bson.D {
q := bson.D{}
if query.JobID != nil {
q = append(q, bson.E{
Key: "job_id",
Value: *query.JobID,
})
}
if query.LogData != nil {
q = append(q, bson.E{
Key: "log_data",
Value: bson.M{
"$regex": primitive.Regex{Pattern: *query.LogData, Options: "ig"},
},
})
}
if query.StartDate != nil && query.EndDate != nil {
q = append(q, bson.E{
Key: "date",
Value: bson.M{
"$gte": primitive.NewDateTimeFromTime(*query.StartDate),
"$lte": primitive.NewDateTimeFromTime(*query.EndDate),
},
})
} else if query.StartDate != nil {
q = append(q, bson.E{
Key: "date",
Value: bson.M{"$gte": primitive.NewDateTimeFromTime(*query.StartDate)},
})
} else if query.EndDate != nil {
q = append(q, bson.E{
Key: "date",
Value: bson.M{"$lte": primitive.NewDateTimeFromTime(*query.EndDate)},
})
}
if query.LogLevel != nil && *query.LogLevel != "" {
levels := strings.Split(*query.LogLevel, ",")
q = append(q,
bson.E{
Key: "log_level",
Value: bson.M{
"$in": levels,
},
},
)
}
return q
}
func (s *MongoStorage) StoreLogs(ctx xcontext.Context, logs []storage.Log) error {
var mongoLogs []interface{}
for _, log := range logs {
mongoLogs = append(mongoLogs, toMongoLog(&log))
}
_, err := s.collection.InsertMany(ctx, mongoLogs)
if err != nil {
ctx.Errorf("Error while inserting a batch of logs: %v", err)
return storage.ErrInsert
}
return nil
}
func (s *MongoStorage) GetLogs(ctx xcontext.Context, query storage.Query) (*storage.Result, error) {
q := toMongoQuery(query)
//get the count of the logs
count, err := s.collection.CountDocuments(ctx, q)
if err != nil {
ctx.Errorf("Error while performing count query: %v", err)
return nil, storage.ErrQuery
}
opts := options.Find()
opts.SetSkip(int64(int(query.PageSize) * int(query.Page)))
opts.SetLimit(int64(query.PageSize))
cur, err := s.collection.Find(ctx, q, opts)
if err != nil {
ctx.Errorf("Error while querying logs from db: %v", err)
return nil, storage.ErrQuery
}
var logs []Log
err = cur.All(ctx, &logs)
if err != nil {
ctx.Errorf("Error while reading query result from db: %v", err)
return nil, storage.ErrQuery
}
// convert to storage logs
storageLogs := make([]storage.Log, 0, len(logs))
for _, log := range logs {
storageLogs = append(storageLogs, log.toStorageLog())
}
return &storage.Result{
Logs: storageLogs,
Count: uint64(count),
Page: query.Page,
PageSize: query.PageSize,
}, nil
}
func (s *MongoStorage) Close(ctx xcontext.Context) error {
return s.dbClient.Disconnect(ctx)
}
type Log struct {
JobID uint64 `bson:"job_id"`
LogData string `bson:"log_data"`
Date time.Time `bson:"date"`
LogLevel string `bson:"log_level"`
}
func (l *Log) toStorageLog() storage.Log {
return storage.Log{
JobID: l.JobID,
LogData: l.LogData,
Date: l.Date,
LogLevel: l.LogLevel,
}
}
func toMongoLog(l *storage.Log) Log {
return Log{
JobID: l.JobID,
LogData: l.LogData,
Date: l.Date,
LogLevel: l.LogLevel,
}
}