2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-08 13:38:34 +02:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2020-12-30 15:43:04 +01:00
|
|
|
jsont "github.com/mainflux/mainflux/pkg/transformers/json"
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/senml"
|
2018-08-08 13:38:34 +02:00
|
|
|
"github.com/mainflux/mainflux/readers"
|
2019-04-16 14:58:56 +02:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2018-08-08 13:38:34 +02:00
|
|
|
)
|
|
|
|
|
2020-12-30 15:43:04 +01:00
|
|
|
const (
|
|
|
|
format = "format"
|
|
|
|
// Collection for SenML messages
|
|
|
|
defCollection = "messages"
|
|
|
|
)
|
2018-08-08 13:38:34 +02:00
|
|
|
|
2020-06-03 17:34:01 +02:00
|
|
|
var errReadMessages = errors.New("failed to read messages from mongodb database")
|
2020-04-13 12:57:53 +02:00
|
|
|
|
2018-08-08 13:38:34 +02:00
|
|
|
var _ readers.MessageRepository = (*mongoRepository)(nil)
|
|
|
|
|
|
|
|
type mongoRepository struct {
|
|
|
|
db *mongo.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new MongoDB reader.
|
|
|
|
func New(db *mongo.Database) readers.MessageRepository {
|
2019-05-07 15:59:18 +02:00
|
|
|
return mongoRepository{
|
|
|
|
db: db,
|
|
|
|
}
|
2018-08-08 13:38:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:59:18 +02:00
|
|
|
func (repo mongoRepository) ReadAll(chanID string, offset, limit uint64, query map[string]string) (readers.MessagesPage, error) {
|
2020-12-30 15:43:04 +01:00
|
|
|
collection, ok := query[format]
|
|
|
|
if !ok {
|
|
|
|
collection = defCollection
|
|
|
|
}
|
2018-08-08 13:38:34 +02:00
|
|
|
col := repo.db.Collection(collection)
|
2020-12-30 15:43:04 +01:00
|
|
|
delete(query, format)
|
2018-12-18 15:17:55 +01:00
|
|
|
sortMap := map[string]interface{}{
|
|
|
|
"time": -1,
|
|
|
|
}
|
2020-12-30 15:43:04 +01:00
|
|
|
// Remove format filter and format the rest properly.
|
2019-03-15 18:38:07 +01:00
|
|
|
filter := fmtCondition(chanID, query)
|
2019-04-16 14:58:56 +02:00
|
|
|
cursor, err := col.Find(context.Background(), filter, options.Find().SetSort(sortMap).SetLimit(int64(limit)).SetSkip(int64(offset)))
|
2018-08-08 13:38:34 +02:00
|
|
|
if err != nil {
|
2020-04-13 12:57:53 +02:00
|
|
|
return readers.MessagesPage{}, errors.Wrap(errReadMessages, err)
|
2018-08-08 13:38:34 +02:00
|
|
|
}
|
|
|
|
defer cursor.Close(context.Background())
|
|
|
|
|
2020-12-30 15:43:04 +01:00
|
|
|
var messages []readers.Message
|
|
|
|
switch collection {
|
|
|
|
case defCollection:
|
|
|
|
for cursor.Next(context.Background()) {
|
|
|
|
var m senml.Message
|
|
|
|
if err := cursor.Decode(&m); err != nil {
|
|
|
|
return readers.MessagesPage{}, errors.Wrap(errReadMessages, err)
|
|
|
|
}
|
2018-08-08 13:38:34 +02:00
|
|
|
|
2020-12-30 15:43:04 +01:00
|
|
|
messages = append(messages, m)
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
2020-12-30 15:43:04 +01:00
|
|
|
default:
|
|
|
|
for cursor.Next(context.Background()) {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := cursor.Decode(&m); err != nil {
|
|
|
|
return readers.MessagesPage{}, errors.Wrap(errReadMessages, err)
|
|
|
|
}
|
|
|
|
m["payload"] = jsont.ParseFlat(m["payload"])
|
|
|
|
|
|
|
|
messages = append(messages, m)
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
2018-08-08 13:38:34 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
total, err := col.CountDocuments(context.Background(), filter)
|
|
|
|
if err != nil {
|
2020-04-13 12:57:53 +02:00
|
|
|
return readers.MessagesPage{}, errors.Wrap(errReadMessages, err)
|
2019-04-25 00:18:43 +02:00
|
|
|
}
|
|
|
|
if total < 0 {
|
2019-05-07 15:59:18 +02:00
|
|
|
return readers.MessagesPage{}, nil
|
2019-04-25 00:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return readers.MessagesPage{
|
|
|
|
Total: uint64(total),
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Messages: messages,
|
2019-05-07 15:59:18 +02:00
|
|
|
}, nil
|
2018-08-08 13:38:34 +02:00
|
|
|
}
|
2019-03-15 18:38:07 +01:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
func fmtCondition(chanID string, query map[string]string) *bson.D {
|
|
|
|
filter := bson.D{
|
|
|
|
bson.E{
|
|
|
|
Key: "channel",
|
|
|
|
Value: chanID,
|
|
|
|
},
|
|
|
|
}
|
2019-03-15 18:38:07 +01:00
|
|
|
for name, value := range query {
|
|
|
|
switch name {
|
|
|
|
case
|
|
|
|
"channel",
|
|
|
|
"subtopic",
|
|
|
|
"publisher",
|
|
|
|
"name",
|
|
|
|
"protocol":
|
2019-04-16 14:58:56 +02:00
|
|
|
filter = append(filter, bson.E{Key: name, Value: value})
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 14:58:56 +02:00
|
|
|
|
|
|
|
return &filter
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|