2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
package readers
|
|
|
|
|
2022-02-15 05:49:23 +08:00
|
|
|
import "errors"
|
|
|
|
|
2021-02-10 05:44:04 +08:00
|
|
|
const (
|
|
|
|
// EqualKey represents the equal comparison operator key.
|
|
|
|
EqualKey = "eq"
|
|
|
|
// LowerThanKey represents the lower-than comparison operator key.
|
|
|
|
LowerThanKey = "lt"
|
|
|
|
// LowerThanEqualKey represents the lower-than-or-equal comparison operator key.
|
|
|
|
LowerThanEqualKey = "le"
|
|
|
|
// GreaterThanKey represents the greater-than-or-equal comparison operator key.
|
|
|
|
GreaterThanKey = "gt"
|
|
|
|
// GreaterThanEqualKey represents the greater-than-or-equal comparison operator key.
|
|
|
|
GreaterThanEqualKey = "ge"
|
|
|
|
)
|
|
|
|
|
2022-02-15 05:49:23 +08:00
|
|
|
// ErrReadMessages indicates failure occurred while reading messages from database.
|
|
|
|
var ErrReadMessages = errors.New("failed to read messages from database")
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
// MessageRepository specifies message reader API.
|
|
|
|
type MessageRepository interface {
|
|
|
|
// ReadAll skips given number of messages for given channel and returns next
|
|
|
|
// limited number of messages.
|
2021-01-26 19:23:15 +08:00
|
|
|
ReadAll(chanID string, pm PageMetadata) (MessagesPage, error)
|
2019-04-25 06:18:43 +08:00
|
|
|
}
|
|
|
|
|
2020-12-30 22:43:04 +08:00
|
|
|
// Message represents any message format.
|
|
|
|
type Message interface{}
|
|
|
|
|
2019-04-25 06:18:43 +08:00
|
|
|
// MessagesPage contains page related metadata as well as list of messages that
|
|
|
|
// belong to this page.
|
|
|
|
type MessagesPage struct {
|
2021-01-26 19:23:15 +08:00
|
|
|
PageMetadata
|
2019-04-25 06:18:43 +08:00
|
|
|
Total uint64
|
2020-12-30 22:43:04 +08:00
|
|
|
Messages []Message
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
2021-01-26 19:23:15 +08:00
|
|
|
|
|
|
|
// PageMetadata represents the parameters used to create database queries
|
|
|
|
type PageMetadata struct {
|
2021-02-10 05:44:04 +08:00
|
|
|
Offset uint64 `json:"offset"`
|
|
|
|
Limit uint64 `json:"limit"`
|
2021-01-26 19:23:15 +08:00
|
|
|
Subtopic string `json:"subtopic,omitempty"`
|
|
|
|
Publisher string `json:"publisher,omitempty"`
|
|
|
|
Protocol string `json:"protocol,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Value float64 `json:"v,omitempty"`
|
2021-02-10 05:44:04 +08:00
|
|
|
Comparator string `json:"comparator,omitempty"`
|
2021-01-26 19:23:15 +08:00
|
|
|
BoolValue bool `json:"vb,omitempty"`
|
|
|
|
StringValue string `json:"vs,omitempty"`
|
|
|
|
DataValue string `json:"vd,omitempty"`
|
|
|
|
From float64 `json:"from,omitempty"`
|
|
|
|
To float64 `json:"to,omitempty"`
|
|
|
|
Format string `json:"format,omitempty"`
|
|
|
|
}
|
2021-02-10 05:44:04 +08:00
|
|
|
|
|
|
|
// ParseValueComparator convert comparison operator keys into mathematic anotation
|
|
|
|
func ParseValueComparator(query map[string]interface{}) string {
|
|
|
|
comparator := "="
|
|
|
|
val, ok := query["comparator"]
|
|
|
|
if ok {
|
|
|
|
switch val.(string) {
|
|
|
|
case EqualKey:
|
|
|
|
comparator = "="
|
|
|
|
case LowerThanKey:
|
|
|
|
comparator = "<"
|
|
|
|
case LowerThanEqualKey:
|
|
|
|
comparator = "<="
|
|
|
|
case GreaterThanKey:
|
|
|
|
comparator = ">"
|
|
|
|
case GreaterThanEqualKey:
|
|
|
|
comparator = ">="
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return comparator
|
|
|
|
}
|