1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-04 22:17:59 +08:00
Dušan Borovčanin dfa766e013 MF-407 - Values of zero are being omitted (#434)
* 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>
2018-11-05 19:18:51 +01:00

160 lines
3.5 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package influxdb
import (
"errors"
"strconv"
"sync"
"time"
"github.com/mainflux/mainflux/writers"
influxdata "github.com/influxdata/influxdb/client/v2"
"github.com/mainflux/mainflux"
)
const pointName = "messages"
var _ writers.MessageRepository = (*influxRepo)(nil)
var (
errZeroValueSize = errors.New("zero value batch size")
errZeroValueTimeout = errors.New("zero value batch timeout")
errNilBatch = errors.New("nil batch")
)
type influxRepo struct {
client influxdata.Client
batch influxdata.BatchPoints
batchSize int
mu sync.Mutex
tick <-chan time.Time
cfg influxdata.BatchPointsConfig
}
type fields map[string]interface{}
type tags map[string]string
// New returns new InfluxDB writer.
func New(client influxdata.Client, database string, batchSize int, batchTimeout time.Duration) (writers.MessageRepository, error) {
if batchSize <= 0 {
return &influxRepo{}, errZeroValueSize
}
if batchTimeout <= 0 {
return &influxRepo{}, errZeroValueTimeout
}
repo := &influxRepo{
client: client,
cfg: influxdata.BatchPointsConfig{
Database: database,
},
batchSize: batchSize,
}
var err error
repo.batch, err = influxdata.NewBatchPoints(repo.cfg)
if err != nil {
return &influxRepo{}, err
}
repo.tick = time.NewTicker(batchTimeout).C
go func() {
for {
<-repo.tick
// Nil point indicates that savePoint method is triggered by the ticker.
repo.savePoint(nil)
}
}()
return repo, nil
}
func (repo *influxRepo) savePoint(point *influxdata.Point) error {
repo.mu.Lock()
defer repo.mu.Unlock()
if repo.batch == nil {
return errNilBatch
}
// Ignore ticker if there is nothing to save.
if len(repo.batch.Points()) == 0 && point == nil {
return nil
}
if point != nil {
repo.batch.AddPoint(point)
}
if len(repo.batch.Points())%repo.batchSize == 0 || point == nil {
if err := repo.client.Write(repo.batch); err != nil {
return err
}
// It would be nice to reset ticker at this point, which
// implies creating a new ticker and goroutine. It would
// introduce unnecessary complexity with no justified benefits.
var err error
repo.batch, err = influxdata.NewBatchPoints(repo.cfg)
if err != nil {
return err
}
}
return nil
}
func (repo *influxRepo) Save(msg mainflux.Message) error {
tags, fields := repo.tagsOf(&msg), repo.fieldsOf(&msg)
pt, err := influxdata.NewPoint(pointName, tags, fields, time.Now())
if err != nil {
return err
}
return repo.savePoint(pt)
}
func (repo *influxRepo) tagsOf(msg *mainflux.Message) tags {
time := strconv.FormatFloat(msg.Time, 'f', -1, 64)
update := strconv.FormatFloat(msg.UpdateTime, 'f', -1, 64)
channel := strconv.FormatUint(msg.Channel, 10)
publisher := strconv.FormatUint(msg.Publisher, 10)
return tags{
"Channel": channel,
"Publisher": publisher,
"Protocol": msg.Protocol,
"Name": msg.Name,
"Unit": msg.Unit,
"Link": msg.Link,
"Time": time,
"UpdateTime": update,
}
}
func (repo *influxRepo) fieldsOf(msg *mainflux.Message) fields {
ret := fields{}
switch msg.Value.(type) {
case *mainflux.Message_FloatValue:
ret["Value"] = msg.GetFloatValue()
case *mainflux.Message_StringValue:
ret["StringValue"] = msg.GetStringValue()
case *mainflux.Message_DataValue:
ret["DataValue"] = msg.GetDataValue()
case *mainflux.Message_BoolValue:
ret["BoolValue"] = msg.GetBoolValue()
}
if msg.ValueSum != nil {
ret["ValueSum"] = msg.GetValueSum().GetValue()
}
return ret
}