2018-08-25 12:48:03 +02:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2018-11-05 19:18:51 +01:00
|
|
|
"strings"
|
2018-11-18 16:42:39 +01:00
|
|
|
"time"
|
2018-08-25 12:48:03 +02:00
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
|
|
|
|
influxdata "github.com/influxdata/influxdb/client/v2"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxLimit = 100
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*influxRepository)(nil)
|
|
|
|
|
|
|
|
type influxRepository struct {
|
|
|
|
database string
|
|
|
|
client influxdata.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new InfluxDB reader.
|
|
|
|
func New(client influxdata.Client, database string) (readers.MessageRepository, error) {
|
|
|
|
return &influxRepository{database, client}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
func (repo *influxRepository) ReadAll(chanID string, offset, limit uint64, query map[string]string) []mainflux.Message {
|
2018-08-25 12:48:03 +02:00
|
|
|
if limit > maxLimit {
|
|
|
|
limit = maxLimit
|
|
|
|
}
|
2018-11-05 19:18:51 +01:00
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
condition := fmtCondition(chanID, query)
|
|
|
|
cmd := fmt.Sprintf(`SELECT * from messages WHERE %s ORDER BY time DESC LIMIT %d OFFSET %d`, condition, limit, offset)
|
2018-08-25 12:48:03 +02:00
|
|
|
q := influxdata.Query{
|
|
|
|
Command: cmd,
|
|
|
|
Database: repo.database,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := []mainflux.Message{}
|
|
|
|
|
|
|
|
resp, err := repo.client.Query(q)
|
|
|
|
if err != nil || resp.Error() != nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Results) < 1 || len(resp.Results[0].Series) < 1 {
|
|
|
|
return ret
|
|
|
|
}
|
2018-11-18 16:42:39 +01:00
|
|
|
|
2018-08-25 12:48:03 +02:00
|
|
|
result := resp.Results[0].Series[0]
|
|
|
|
for _, v := range result.Values {
|
2018-11-05 19:18:51 +01:00
|
|
|
ret = append(ret, parseMessage(result.Columns, v))
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
func fmtCondition(chanID string, query map[string]string) string {
|
|
|
|
condition := fmt.Sprintf(`channel='%s'`, chanID)
|
|
|
|
for name, value := range query {
|
|
|
|
switch name {
|
|
|
|
case
|
|
|
|
"channel",
|
|
|
|
"subtopic",
|
|
|
|
"publisher":
|
|
|
|
condition = fmt.Sprintf(`%s AND %s='%s'`, condition, name,
|
|
|
|
strings.Replace(value, "'", "\\'", -1))
|
|
|
|
case
|
|
|
|
"name",
|
|
|
|
"protocol":
|
|
|
|
condition = fmt.Sprintf(`%s AND "%s"='%s'`, condition, name,
|
|
|
|
strings.Replace(value, "\"", "\\\"", -1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return condition
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
// ParseMessage and parseValues are util methods. Since InfluxDB client returns
|
|
|
|
// results in form of rows and columns, this obscure message conversion is needed
|
2018-08-25 12:48:03 +02:00
|
|
|
// to return actual []mainflux.Message from the query result.
|
2018-11-05 19:18:51 +01:00
|
|
|
func parseValues(value interface{}, name string, msg *mainflux.Message) {
|
2018-11-18 16:42:39 +01:00
|
|
|
if name == "valueSum" && value != nil {
|
2018-11-05 19:18:51 +01:00
|
|
|
if sum, ok := value.(json.Number); ok {
|
|
|
|
valSum, err := sum.Float64()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-11-18 16:42:39 +01:00
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
msg.ValueSum = &mainflux.SumValue{Value: valSum}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-11-18 16:42:39 +01:00
|
|
|
|
|
|
|
if strings.HasSuffix(strings.ToLower(name), "value") {
|
2018-11-05 19:18:51 +01:00
|
|
|
switch value.(type) {
|
|
|
|
case bool:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_BoolValue{BoolValue: value.(bool)}
|
2018-11-05 19:18:51 +01:00
|
|
|
case json.Number:
|
|
|
|
num, err := value.(json.Number).Float64()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_FloatValue{FloatValue: num}
|
2018-11-05 19:18:51 +01:00
|
|
|
case string:
|
2018-11-18 16:42:39 +01:00
|
|
|
if strings.HasPrefix(name, "string") {
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_StringValue{StringValue: value.(string)}
|
2018-11-05 19:18:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-18 16:42:39 +01:00
|
|
|
if strings.HasPrefix(name, "data") {
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_DataValue{DataValue: value.(string)}
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
func parseMessage(names []string, fields []interface{}) mainflux.Message {
|
2018-08-25 12:48:03 +02:00
|
|
|
m := mainflux.Message{}
|
|
|
|
v := reflect.ValueOf(&m).Elem()
|
|
|
|
for i, name := range names {
|
2018-11-05 19:18:51 +01:00
|
|
|
parseValues(fields[i], name, &m)
|
2018-11-18 16:42:39 +01:00
|
|
|
msgField := v.FieldByName(strings.Title(name))
|
2018-08-25 12:48:03 +02:00
|
|
|
if !msgField.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-05 19:18:51 +01:00
|
|
|
|
2018-08-25 12:48:03 +02:00
|
|
|
f := msgField.Interface()
|
|
|
|
switch f.(type) {
|
|
|
|
case string:
|
|
|
|
if s, ok := fields[i].(string); ok {
|
|
|
|
msgField.SetString(s)
|
|
|
|
}
|
|
|
|
case float64:
|
2018-11-18 16:42:39 +01:00
|
|
|
if name == "time" {
|
|
|
|
t, err := time.Parse(time.RFC3339, fields[i].(string))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v := float64(t.Unix())
|
|
|
|
msgField.SetFloat(v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
val, _ := strconv.ParseFloat(fields[i].(string), 64)
|
|
|
|
msgField.SetFloat(val)
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|