2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-08-08 13:38:34 +02:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
const collection = "mainflux"
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*mongoRepository)(nil)
|
|
|
|
|
|
|
|
type mongoRepository struct {
|
|
|
|
db *mongo.Database
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
// Message struct is used as a MongoDB representation of Mainflux message.
|
|
|
|
type message struct {
|
2018-12-05 13:09:25 +01:00
|
|
|
Channel string `bson:"channel,omitempty"`
|
2019-03-15 18:38:07 +01:00
|
|
|
Subtopic string `bson:"subtopic,omitempty"`
|
2018-12-05 13:09:25 +01:00
|
|
|
Publisher string `bson:"publisher,omitempty"`
|
2018-11-05 19:18:51 +01:00
|
|
|
Protocol string `bson:"protocol,omitempty"`
|
|
|
|
Name string `bson:"name,omitempty"`
|
|
|
|
Unit string `bson:"unit,omitempty"`
|
|
|
|
FloatValue *float64 `bson:"value,omitempty"`
|
|
|
|
StringValue *string `bson:"stringValue,omitempty"`
|
|
|
|
BoolValue *bool `bson:"boolValue,omitempty"`
|
|
|
|
DataValue *string `bson:"dataValue,omitempty"`
|
|
|
|
ValueSum *float64 `bson:"valueSum,omitempty"`
|
|
|
|
Time float64 `bson:"time,omitempty"`
|
|
|
|
UpdateTime float64 `bson:"updateTime,omitempty"`
|
|
|
|
Link string `bson:"link,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:38:34 +02:00
|
|
|
// New returns new MongoDB reader.
|
|
|
|
func New(db *mongo.Database) readers.MessageRepository {
|
|
|
|
return mongoRepository{db: db}
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
func (repo mongoRepository) ReadAll(chanID string, offset, limit uint64, query map[string]string) []mainflux.Message {
|
2018-08-08 13:38:34 +02:00
|
|
|
col := repo.db.Collection(collection)
|
2018-12-18 15:17:55 +01:00
|
|
|
sortMap := map[string]interface{}{
|
|
|
|
"time": -1,
|
|
|
|
}
|
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 {
|
|
|
|
return []mainflux.Message{}
|
|
|
|
}
|
|
|
|
defer cursor.Close(context.Background())
|
|
|
|
|
|
|
|
messages := []mainflux.Message{}
|
|
|
|
for cursor.Next(context.Background()) {
|
2018-11-05 19:18:51 +01:00
|
|
|
var m message
|
|
|
|
if err := cursor.Decode(&m); err != nil {
|
2018-08-08 13:38:34 +02:00
|
|
|
return []mainflux.Message{}
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
msg := mainflux.Message{
|
|
|
|
Channel: m.Channel,
|
2019-03-15 18:38:07 +01:00
|
|
|
Subtopic: m.Subtopic,
|
2018-11-05 19:18:51 +01:00
|
|
|
Publisher: m.Publisher,
|
|
|
|
Protocol: m.Protocol,
|
|
|
|
Name: m.Name,
|
|
|
|
Unit: m.Unit,
|
|
|
|
Time: m.Time,
|
|
|
|
UpdateTime: m.UpdateTime,
|
|
|
|
Link: m.Link,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case m.FloatValue != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_FloatValue{FloatValue: *m.FloatValue}
|
2018-11-05 19:18:51 +01:00
|
|
|
case m.StringValue != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_StringValue{StringValue: *m.StringValue}
|
2018-11-05 19:18:51 +01:00
|
|
|
case m.DataValue != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_DataValue{DataValue: *m.DataValue}
|
2018-11-05 19:18:51 +01:00
|
|
|
case m.BoolValue != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_BoolValue{BoolValue: *m.BoolValue}
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.ValueSum != nil {
|
|
|
|
msg.ValueSum = &mainflux.SumValue{Value: *m.ValueSum}
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:38:34 +02:00
|
|
|
messages = append(messages, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages
|
|
|
|
}
|
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
|
|
|
}
|