2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2019-05-08 01:09:28 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
2019-10-31 21:04:47 +08:00
|
|
|
"context"
|
2020-12-30 22:43:04 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-05-08 01:09:28 +08:00
|
|
|
|
2019-05-16 19:35:13 +08:00
|
|
|
"github.com/gofrs/uuid"
|
2022-12-13 22:34:01 +08:00
|
|
|
"github.com/jackc/pgerrcode"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
"github.com/jmoiron/sqlx" // required for DB access
|
2021-01-12 06:55:34 +08:00
|
|
|
"github.com/mainflux/mainflux/consumers"
|
2020-06-03 21:16:19 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2020-12-30 22:43:04 +08:00
|
|
|
mfjson "github.com/mainflux/mainflux/pkg/transformers/json"
|
2020-06-03 21:16:19 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/senml"
|
2019-05-08 01:09:28 +08:00
|
|
|
)
|
|
|
|
|
2020-04-13 18:57:53 +08:00
|
|
|
var (
|
2020-12-30 22:43:04 +08:00
|
|
|
errInvalidMessage = errors.New("invalid message representation")
|
2020-05-06 21:15:19 +08:00
|
|
|
errSaveMessage = errors.New("failed to save message to postgres database")
|
|
|
|
errTransRollback = errors.New("failed to rollback transaction")
|
2020-12-30 22:43:04 +08:00
|
|
|
errNoTable = errors.New("relation does not exist")
|
2020-04-13 18:57:53 +08:00
|
|
|
)
|
2019-05-08 01:09:28 +08:00
|
|
|
|
2023-04-21 00:20:53 +08:00
|
|
|
var _ consumers.BlockingConsumer = (*postgresRepo)(nil)
|
2019-05-08 01:09:28 +08:00
|
|
|
|
|
|
|
type postgresRepo struct {
|
|
|
|
db *sqlx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new PostgreSQL writer.
|
2023-04-21 00:20:53 +08:00
|
|
|
func New(db *sqlx.DB) consumers.BlockingConsumer {
|
2019-05-08 01:09:28 +08:00
|
|
|
return &postgresRepo{db: db}
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:38:16 +08:00
|
|
|
func (pr postgresRepo) ConsumeBlocking(ctx context.Context, message interface{}) (err error) {
|
2020-12-30 22:43:04 +08:00
|
|
|
switch m := message.(type) {
|
|
|
|
case mfjson.Messages:
|
2023-10-16 17:43:33 +08:00
|
|
|
return pr.saveJSON(ctx, m)
|
2020-12-30 22:43:04 +08:00
|
|
|
default:
|
2023-07-22 20:38:16 +08:00
|
|
|
return pr.saveSenml(ctx, m)
|
2020-12-30 22:43:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:38:16 +08:00
|
|
|
func (pr postgresRepo) saveSenml(ctx context.Context, messages interface{}) (err error) {
|
2020-12-30 22:43:04 +08:00
|
|
|
msgs, ok := messages.([]senml.Message)
|
|
|
|
if !ok {
|
|
|
|
return errSaveMessage
|
|
|
|
}
|
2019-05-08 01:09:28 +08:00
|
|
|
q := `INSERT INTO messages (id, channel, subtopic, publisher, protocol,
|
2020-12-30 22:43:04 +08:00
|
|
|
name, unit, value, string_value, bool_value, data_value, sum,
|
|
|
|
time, update_time)
|
|
|
|
VALUES (:id, :channel, :subtopic, :publisher, :protocol, :name, :unit,
|
|
|
|
:value, :string_value, :bool_value, :data_value, :sum,
|
|
|
|
:time, :update_time);`
|
2019-05-08 01:09:28 +08:00
|
|
|
|
2023-07-22 20:38:16 +08:00
|
|
|
tx, err := pr.db.BeginTxx(ctx, nil)
|
2019-05-16 19:35:13 +08:00
|
|
|
if err != nil {
|
2020-04-13 18:57:53 +08:00
|
|
|
return errors.Wrap(errSaveMessage, err)
|
2019-05-16 19:35:13 +08:00
|
|
|
}
|
2020-05-06 21:15:19 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if txErr := tx.Rollback(); txErr != nil {
|
|
|
|
err = errors.Wrap(err, errors.Wrap(errTransRollback, txErr))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
err = errors.Wrap(errSaveMessage, err)
|
|
|
|
}
|
|
|
|
}()
|
2019-05-08 01:09:28 +08:00
|
|
|
|
2020-12-30 22:43:04 +08:00
|
|
|
for _, msg := range msgs {
|
|
|
|
id, err := uuid.NewV4()
|
2019-10-31 21:04:47 +08:00
|
|
|
if err != nil {
|
2020-12-30 22:43:04 +08:00
|
|
|
return err
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|
2020-12-30 22:43:04 +08:00
|
|
|
m := senmlMessage{Message: msg, ID: id.String()}
|
|
|
|
if _, err := tx.NamedExec(q, m); err != nil {
|
2022-12-13 22:34:01 +08:00
|
|
|
pgErr, ok := err.(*pgconn.PgError)
|
2019-10-31 21:04:47 +08:00
|
|
|
if ok {
|
2023-07-28 09:01:27 +08:00
|
|
|
if pgErr.Code == pgerrcode.InvalidTextRepresentation {
|
2020-12-30 22:43:04 +08:00
|
|
|
return errors.Wrap(errSaveMessage, errInvalidMessage)
|
2019-10-31 21:04:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:57:53 +08:00
|
|
|
return errors.Wrap(errSaveMessage, err)
|
2019-10-31 21:04:47 +08:00
|
|
|
}
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|
2020-05-06 21:15:19 +08:00
|
|
|
return err
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|
|
|
|
|
2023-10-16 17:43:33 +08:00
|
|
|
func (pr postgresRepo) saveJSON(ctx context.Context, msgs mfjson.Messages) error {
|
|
|
|
if err := pr.insertJSON(ctx, msgs); err != nil {
|
2020-12-30 22:43:04 +08:00
|
|
|
if err == errNoTable {
|
|
|
|
if err := pr.createTable(msgs.Format); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-16 17:43:33 +08:00
|
|
|
return pr.insertJSON(ctx, msgs)
|
2020-12-30 22:43:04 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|
|
|
|
|
2023-10-16 17:43:33 +08:00
|
|
|
func (pr postgresRepo) insertJSON(ctx context.Context, msgs mfjson.Messages) error {
|
|
|
|
tx, err := pr.db.BeginTxx(ctx, nil)
|
2020-12-30 22:43:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(errSaveMessage, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if txErr := tx.Rollback(); txErr != nil {
|
|
|
|
err = errors.Wrap(err, errors.Wrap(errTransRollback, txErr))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
err = errors.Wrap(errSaveMessage, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
q := `INSERT INTO %s (id, channel, created, subtopic, publisher, protocol, payload)
|
|
|
|
VALUES (:id, :channel, :created, :subtopic, :publisher, :protocol, :payload);`
|
|
|
|
q = fmt.Sprintf(q, msgs.Format)
|
|
|
|
|
|
|
|
for _, m := range msgs.Data {
|
|
|
|
var dbmsg jsonMessage
|
|
|
|
dbmsg, err = toJSONMessage(m)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(errSaveMessage, err)
|
|
|
|
}
|
2022-12-13 22:34:01 +08:00
|
|
|
|
2020-12-30 22:43:04 +08:00
|
|
|
if _, err = tx.NamedExec(q, dbmsg); err != nil {
|
2022-12-13 22:34:01 +08:00
|
|
|
pgErr, ok := err.(*pgconn.PgError)
|
2020-12-30 22:43:04 +08:00
|
|
|
if ok {
|
2022-12-13 22:34:01 +08:00
|
|
|
switch pgErr.Code {
|
|
|
|
case pgerrcode.InvalidTextRepresentation:
|
2020-12-30 22:43:04 +08:00
|
|
|
return errors.Wrap(errSaveMessage, errInvalidMessage)
|
2022-12-13 22:34:01 +08:00
|
|
|
case pgerrcode.UndefinedTable:
|
2020-12-30 22:43:04 +08:00
|
|
|
return errNoTable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr postgresRepo) createTable(name string) error {
|
|
|
|
q := `CREATE TABLE IF NOT EXISTS %s (
|
2022-01-18 19:08:54 +08:00
|
|
|
id UUID,
|
|
|
|
created BIGINT,
|
|
|
|
channel VARCHAR(254),
|
|
|
|
subtopic VARCHAR(254),
|
|
|
|
publisher VARCHAR(254),
|
|
|
|
protocol TEXT,
|
|
|
|
payload JSONB,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
)`
|
2020-12-30 22:43:04 +08:00
|
|
|
q = fmt.Sprintf(q, name)
|
|
|
|
|
|
|
|
_, err := pr.db.Exec(q)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type senmlMessage struct {
|
|
|
|
senml.Message
|
|
|
|
ID string `db:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonMessage struct {
|
|
|
|
ID string `db:"id"`
|
|
|
|
Channel string `db:"channel"`
|
|
|
|
Created int64 `db:"created"`
|
|
|
|
Subtopic string `db:"subtopic"`
|
|
|
|
Publisher string `db:"publisher"`
|
|
|
|
Protocol string `db:"protocol"`
|
|
|
|
Payload []byte `db:"payload"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func toJSONMessage(msg mfjson.Message) (jsonMessage, error) {
|
2019-11-05 18:57:16 +08:00
|
|
|
id, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
2020-12-30 22:43:04 +08:00
|
|
|
return jsonMessage{}, err
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|
|
|
|
|
2020-12-30 22:43:04 +08:00
|
|
|
data := []byte("{}")
|
|
|
|
if msg.Payload != nil {
|
|
|
|
b, err := json.Marshal(msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return jsonMessage{}, errors.Wrap(errSaveMessage, err)
|
|
|
|
}
|
|
|
|
data = b
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|
|
|
|
|
2020-12-30 22:43:04 +08:00
|
|
|
m := jsonMessage{
|
|
|
|
ID: id.String(),
|
|
|
|
Channel: msg.Channel,
|
|
|
|
Created: msg.Created,
|
|
|
|
Subtopic: msg.Subtopic,
|
|
|
|
Publisher: msg.Publisher,
|
|
|
|
Protocol: msg.Protocol,
|
|
|
|
Payload: data,
|
2019-05-16 19:35:13 +08:00
|
|
|
}
|
|
|
|
|
2019-11-05 18:57:16 +08:00
|
|
|
return m, nil
|
2019-05-08 01:09:28 +08:00
|
|
|
}
|