mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* Fix empty protobuf values Update Normalizer service and .proto files. Reader and Writer services needs to be updated due to message format change. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update HTTP adapter to use gogo protobuf Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Reader services API Update API or Reader services to match Message changes due to switching to gogo/proto. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update InfluxDB services Update InfluxDB Reader and Writer services to match new Message format. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update MongoDB services Update MongoDB Reader and Writer services to match new message format. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Cassandra services Update Cassandra Reader and Writer service to match new Message format. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix InfluxDB Reader test Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Makefile and docs accordingly Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix possible data race in InfluxDB writer Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update InfluxDB Writer tests Raise test coverage. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix InfluxDB reader Fix wrong ValueSum readings. Upadete tests and raise coverage. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update MongoDB services tests Raise test coverage for MongoDB Reader and Writer services. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Readers API tests Raise test coverage. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Raise test coverage Update Cassandra Reader and Writer services tests. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Use gogo/protobuf in CoAP adapter Add gogo/protobuf to Gopkg.toml and update dependencies. Update Dockerfile to run `make proto`. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Cassandra Reader tests Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Refactor code Improve code style and comments to improve readability. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Rename Sum to SumValue Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Rename Values to Value Since message contains only single value (or possibly no value at all), `Values` name could be misleading. Rename simple double value from `Value` to `FloatValue` accordingly. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix InfluxDB Reader logging Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Replace exclusive if statements with switch-case Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Cassandra services tests Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package mongodb
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/readers"
|
|
"github.com/mongodb/mongo-go-driver/bson"
|
|
"github.com/mongodb/mongo-go-driver/mongo"
|
|
"github.com/mongodb/mongo-go-driver/mongo/findopt"
|
|
)
|
|
|
|
const collection = "mainflux"
|
|
|
|
var _ readers.MessageRepository = (*mongoRepository)(nil)
|
|
|
|
type mongoRepository struct {
|
|
db *mongo.Database
|
|
}
|
|
|
|
// Message struct is used as a MongoDB representation of Mainflux message.
|
|
type message struct {
|
|
Channel uint64 `bson:"channel,omitempty"`
|
|
Publisher uint64 `bson:"publisher,omitempty"`
|
|
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"`
|
|
}
|
|
|
|
// New returns new MongoDB reader.
|
|
func New(db *mongo.Database) readers.MessageRepository {
|
|
return mongoRepository{db: db}
|
|
}
|
|
|
|
func (repo mongoRepository) ReadAll(chanID, offset, limit uint64) []mainflux.Message {
|
|
col := repo.db.Collection(collection)
|
|
cursor, err := col.Find(context.Background(), bson.NewDocument(bson.EC.Int64("channel", int64(chanID))), findopt.Limit(int64(limit)), findopt.Skip(int64(offset)))
|
|
if err != nil {
|
|
return []mainflux.Message{}
|
|
}
|
|
defer cursor.Close(context.Background())
|
|
|
|
messages := []mainflux.Message{}
|
|
for cursor.Next(context.Background()) {
|
|
var m message
|
|
if err := cursor.Decode(&m); err != nil {
|
|
return []mainflux.Message{}
|
|
}
|
|
|
|
msg := mainflux.Message{
|
|
Channel: m.Channel,
|
|
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:
|
|
msg.Value = &mainflux.Message_FloatValue{*m.FloatValue}
|
|
case m.StringValue != nil:
|
|
msg.Value = &mainflux.Message_StringValue{*m.StringValue}
|
|
case m.DataValue != nil:
|
|
msg.Value = &mainflux.Message_DataValue{*m.DataValue}
|
|
case m.BoolValue != nil:
|
|
msg.Value = &mainflux.Message_BoolValue{*m.BoolValue}
|
|
}
|
|
|
|
if m.ValueSum != nil {
|
|
msg.ValueSum = &mainflux.SumValue{Value: *m.ValueSum}
|
|
}
|
|
|
|
messages = append(messages, msg)
|
|
}
|
|
|
|
return messages
|
|
}
|