2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-21 16:28:52 +02:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
2018-10-01 17:36:53 +02:00
|
|
|
"errors"
|
2018-05-21 16:28:52 +02:00
|
|
|
"strconv"
|
2018-09-23 01:53:03 +02:00
|
|
|
"sync"
|
2018-05-21 16:28:52 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/writers"
|
|
|
|
|
|
|
|
influxdata "github.com/influxdata/influxdb/client/v2"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
)
|
|
|
|
|
2018-06-01 11:20:44 +02:00
|
|
|
const pointName = "messages"
|
|
|
|
|
2018-05-21 16:28:52 +02:00
|
|
|
var _ writers.MessageRepository = (*influxRepo)(nil)
|
|
|
|
|
2018-10-01 17:36:53 +02:00
|
|
|
var (
|
|
|
|
errZeroValueSize = errors.New("zero value batch size")
|
|
|
|
errZeroValueTimeout = errors.New("zero value batch timeout")
|
2018-11-05 19:18:51 +01:00
|
|
|
errNilBatch = errors.New("nil batch")
|
2018-10-01 17:36:53 +02:00
|
|
|
)
|
|
|
|
|
2018-05-21 16:28:52 +02:00
|
|
|
type influxRepo struct {
|
2018-09-23 01:53:03 +02:00
|
|
|
client influxdata.Client
|
2018-11-05 19:18:51 +01:00
|
|
|
batch influxdata.BatchPoints
|
2018-09-23 01:53:03 +02:00
|
|
|
batchSize int
|
|
|
|
mu sync.Mutex
|
|
|
|
tick <-chan time.Time
|
|
|
|
cfg influxdata.BatchPointsConfig
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type fields map[string]interface{}
|
|
|
|
type tags map[string]string
|
|
|
|
|
|
|
|
// New returns new InfluxDB writer.
|
2018-09-23 01:53:03 +02:00
|
|
|
func New(client influxdata.Client, database string, batchSize int, batchTimeout time.Duration) (writers.MessageRepository, error) {
|
2018-11-05 19:18:51 +01:00
|
|
|
if batchSize <= 0 {
|
2018-10-01 17:36:53 +02:00
|
|
|
return &influxRepo{}, errZeroValueSize
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
if batchTimeout <= 0 {
|
2018-10-01 17:36:53 +02:00
|
|
|
return &influxRepo{}, errZeroValueTimeout
|
|
|
|
}
|
|
|
|
|
2018-09-23 01:53:03 +02:00
|
|
|
repo := &influxRepo{
|
|
|
|
client: client,
|
|
|
|
cfg: influxdata.BatchPointsConfig{
|
|
|
|
Database: database,
|
|
|
|
},
|
|
|
|
batchSize: batchSize,
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
repo.batch, err = influxdata.NewBatchPoints(repo.cfg)
|
|
|
|
if err != nil {
|
|
|
|
return &influxRepo{}, err
|
2018-09-23 01:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
repo.tick = time.NewTicker(batchTimeout).C
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-repo.tick
|
2018-11-05 19:18:51 +01:00
|
|
|
// Nil point indicates that savePoint method is triggered by the ticker.
|
|
|
|
repo.savePoint(nil)
|
2018-09-23 01:53:03 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return repo, nil
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
func (repo *influxRepo) savePoint(point *influxdata.Point) error {
|
2018-09-23 01:53:03 +02:00
|
|
|
repo.mu.Lock()
|
|
|
|
defer repo.mu.Unlock()
|
2018-11-05 19:18:51 +01:00
|
|
|
if repo.batch == nil {
|
|
|
|
return errNilBatch
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
// Ignore ticker if there is nothing to save.
|
|
|
|
if len(repo.batch.Points()) == 0 && point == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-23 01:53:03 +02:00
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
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
|
|
|
|
}
|
2018-09-23 01:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *influxRepo) Save(msg mainflux.Message) error {
|
2018-05-21 16:28:52 +02:00
|
|
|
tags, fields := repo.tagsOf(&msg), repo.fieldsOf(&msg)
|
2018-06-01 11:20:44 +02:00
|
|
|
pt, err := influxdata.NewPoint(pointName, tags, fields, time.Now())
|
2018-05-21 16:28:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
return repo.savePoint(pt)
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-09-23 01:53:03 +02:00
|
|
|
|
2018-05-21 16:28:52 +02:00
|
|
|
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 {
|
2018-11-05 19:18:51 +01:00
|
|
|
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()
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|
2018-11-05 19:18:51 +01:00
|
|
|
|
|
|
|
return ret
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|