/
codec-mongo.go
82 lines (73 loc) · 1.86 KB
/
codec-mongo.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
package log
import (
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/pydio/cells/v4/common/dao/mongodb"
"github.com/pydio/cells/v4/common/utils/configx"
)
const mongoCollection = "syslog"
type MongoCodec struct {
baseCodec
}
func (m *MongoCodec) Unmarshal(indexed interface{}) (interface{}, error) {
cursor, ok := indexed.(*mongo.Cursor)
if !ok {
return nil, fmt.Errorf("not a cursor")
}
ilog := &IndexableLog{}
if er := cursor.Decode(ilog); er != nil {
return nil, er
}
return ilog.LogMessage, nil
}
func (m *MongoCodec) BuildQuery(query interface{}, offset, limit int32, sortFields string, sortDesc bool) (interface{}, interface{}, error) {
qString, ok := query.(string)
if !ok {
return nil, nil, fmt.Errorf("BuildQuery expects a string")
}
ff, e := mongodb.BleveQueryToMongoFilters(qString, true, func(s string) string {
return strings.ToLower(s)
})
if e != nil {
return nil, nil, e
}
return ff, nil, nil
}
func (m *MongoCodec) BuildQueryOptions(query interface{}, offset, limit int32, sortFields string, sortDesc bool) (interface{}, error) {
opts := &options.FindOptions{Sort: bson.D{{"ts", -1}, {"nano", -1}}}
if limit > 0 {
l64 := int64(limit)
opts.Limit = &l64
}
if offset > 0 {
o64 := int64(offset)
opts.Skip = &o64
}
return opts, nil
}
func (m *MongoCodec) GetModel(sc configx.Values) (interface{}, bool) {
model := mongodb.Model{
Collections: []mongodb.Collection{
{
Name: mongoCollection,
DefaultCollation: mongodb.Collation{
Strength: 2,
Locale: "en_US",
},
Indexes: []map[string]int{
{"ts": -1, "nano": -1},
{"msg": 1},
{"service": 1},
{"level": 1},
{"logger": 1},
{"username": 1},
{"operationuuid": 1},
},
TruncateSorterDesc: "ts",
},
}}
return model, true
}