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

* Implement errors package in senml transformer, readers and writers Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove unused const Return wrapped error in postgres writer Signed-off-by: Ivan Milošević <iva@blokovi.com> * fix default db host in postgres writer Signed-off-by: Ivan Milošević <iva@blokovi.com> * fix capital letters in errors messages Signed-off-by: Ivan Milošević <iva@blokovi.com> * use svcName instead of postgres for Promethius initialization Signed-off-by: Ivan Milošević <iva@blokovi.com>
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cassandra
|
|
|
|
import (
|
|
"github.com/gocql/gocql"
|
|
"github.com/mainflux/mainflux/errors"
|
|
"github.com/mainflux/mainflux/transformers/senml"
|
|
"github.com/mainflux/mainflux/writers"
|
|
)
|
|
|
|
var errSaveMessage = errors.New("faled to save message to cassandra database")
|
|
|
|
var _ writers.MessageRepository = (*cassandraRepository)(nil)
|
|
|
|
type cassandraRepository struct {
|
|
session *gocql.Session
|
|
}
|
|
|
|
// New instantiates Cassandra message repository.
|
|
func New(session *gocql.Session) writers.MessageRepository {
|
|
return &cassandraRepository{session}
|
|
}
|
|
|
|
func (cr *cassandraRepository) Save(messages ...senml.Message) error {
|
|
cql := `INSERT INTO messages (id, channel, subtopic, publisher, protocol,
|
|
name, unit, value, string_value, bool_value, data_value, sum,
|
|
time, update_time)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
id := gocql.TimeUUID()
|
|
|
|
for _, msg := range messages {
|
|
err := cr.session.Query(cql, id, msg.Channel, msg.Subtopic, msg.Publisher,
|
|
msg.Protocol, msg.Name, msg.Unit, msg.Value, msg.StringValue,
|
|
msg.BoolValue, msg.DataValue, msg.Sum, msg.Time, msg.UpdateTime).Exec()
|
|
if err != nil {
|
|
return errors.Wrap(errSaveMessage, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|