2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-11-20 19:46:33 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
|
|
|
|
2018-11-20 19:46:33 +01:00
|
|
|
"github.com/go-redis/redis"
|
2020-11-23 11:34:29 +01:00
|
|
|
"github.com/mainflux/mainflux/internal/groups"
|
2018-11-20 19:46:33 +01:00
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
streamID = "mainflux.things"
|
|
|
|
streamLen = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ things.Service = (*eventStore)(nil)
|
|
|
|
|
|
|
|
type eventStore struct {
|
|
|
|
svc things.Service
|
|
|
|
client *redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEventStoreMiddleware returns wrapper around things service that sends
|
|
|
|
// events to event store.
|
|
|
|
func NewEventStoreMiddleware(svc things.Service, client *redis.Client) things.Service {
|
|
|
|
return eventStore{
|
|
|
|
svc: svc,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (es eventStore) CreateThings(ctx context.Context, token string, ths ...things.Thing) ([]things.Thing, error) {
|
|
|
|
sths, err := es.svc.CreateThings(ctx, token, ths...)
|
2019-10-29 05:59:54 -06:00
|
|
|
if err != nil {
|
|
|
|
return sths, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, thing := range sths {
|
|
|
|
event := createThingEvent{
|
|
|
|
id: thing.ID,
|
|
|
|
owner: thing.Owner,
|
|
|
|
name: thing.Name,
|
|
|
|
metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sths, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) UpdateThing(ctx context.Context, token string, thing things.Thing) error {
|
|
|
|
if err := es.svc.UpdateThing(ctx, token, thing); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := updateThingEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: thing.ID,
|
2018-11-20 19:46:33 +01:00
|
|
|
name: thing.Name,
|
|
|
|
metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
// UpdateKey doesn't send event because key shouldn't be sent over stream.
|
|
|
|
// Maybe we can start publishing this event at some point, without key value
|
|
|
|
// in order to notify adapters to disconnect connected things after key update.
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) UpdateKey(ctx context.Context, token, id, key string) error {
|
|
|
|
return es.svc.UpdateKey(ctx, token, id, key)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) ViewThing(ctx context.Context, token, id string) (things.Thing, error) {
|
|
|
|
return es.svc.ViewThing(ctx, token, id)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:15:30 +02:00
|
|
|
func (es eventStore) ListThings(ctx context.Context, token string, offset, limit uint64, name string, metadata things.Metadata) (things.Page, error) {
|
2019-09-17 13:46:24 +00:00
|
|
|
return es.svc.ListThings(ctx, token, offset, limit, name, metadata)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 22:47:15 +02:00
|
|
|
func (es eventStore) ListThingsByChannel(ctx context.Context, token, id string, offset, limit uint64, connected bool) (things.Page, error) {
|
|
|
|
return es.svc.ListThingsByChannel(ctx, token, id, offset, limit, connected)
|
2019-04-25 14:37:51 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) RemoveThing(ctx context.Context, token, id string) error {
|
|
|
|
if err := es.svc.RemoveThing(ctx, token, id); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := removeThingEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: id,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (es eventStore) CreateChannels(ctx context.Context, token string, channels ...things.Channel) ([]things.Channel, error) {
|
|
|
|
schs, err := es.svc.CreateChannels(ctx, token, channels...)
|
2019-10-29 05:59:54 -06:00
|
|
|
if err != nil {
|
|
|
|
return schs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range schs {
|
|
|
|
event := createChannelEvent{
|
|
|
|
id: channel.ID,
|
|
|
|
owner: channel.Owner,
|
|
|
|
name: channel.Name,
|
|
|
|
metadata: channel.Metadata,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return schs, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) UpdateChannel(ctx context.Context, token string, channel things.Channel) error {
|
|
|
|
if err := es.svc.UpdateChannel(ctx, token, channel); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := updateChannelEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: channel.ID,
|
2018-11-28 15:58:48 +01:00
|
|
|
name: channel.Name,
|
|
|
|
metadata: channel.Metadata,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) ViewChannel(ctx context.Context, token, id string) (things.Channel, error) {
|
|
|
|
return es.svc.ViewChannel(ctx, token, id)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2019-10-01 14:12:52 +02:00
|
|
|
func (es eventStore) ListChannels(ctx context.Context, token string, offset, limit uint64, name string, metadata things.Metadata) (things.ChannelsPage, error) {
|
|
|
|
return es.svc.ListChannels(ctx, token, offset, limit, name, metadata)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 22:47:15 +02:00
|
|
|
func (es eventStore) ListChannelsByThing(ctx context.Context, token, id string, offset, limit uint64, connected bool) (things.ChannelsPage, error) {
|
|
|
|
return es.svc.ListChannelsByThing(ctx, token, id, offset, limit, connected)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) RemoveChannel(ctx context.Context, token, id string) error {
|
|
|
|
if err := es.svc.RemoveChannel(ctx, token, id); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := removeChannelEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: id,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
func (es eventStore) Connect(ctx context.Context, token string, chIDs, thIDs []string) error {
|
|
|
|
if err := es.svc.Connect(ctx, token, chIDs, thIDs); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
for _, chID := range chIDs {
|
|
|
|
for _, thID := range thIDs {
|
|
|
|
event := connectThingEvent{
|
|
|
|
chanID: chID,
|
|
|
|
thingID: thID,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
2019-11-11 17:24:29 -07:00
|
|
|
}
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) Disconnect(ctx context.Context, token, chanID, thingID string) error {
|
|
|
|
if err := es.svc.Disconnect(ctx, token, chanID, thingID); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := disconnectThingEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(record).Err()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func (es eventStore) CanAccessByKey(ctx context.Context, chanID string, key string) (string, error) {
|
|
|
|
return es.svc.CanAccessByKey(ctx, chanID, key)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) CanAccessByID(ctx context.Context, chanID string, thingID string) error {
|
|
|
|
return es.svc.CanAccessByID(ctx, chanID, thingID)
|
2019-07-15 18:28:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) Identify(ctx context.Context, key string) (string, error) {
|
|
|
|
return es.svc.Identify(ctx, key)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
2020-11-23 11:34:29 +01:00
|
|
|
|
|
|
|
func (es eventStore) CreateGroup(ctx context.Context, token string, g groups.Group) (string, error) {
|
|
|
|
return es.svc.CreateGroup(ctx, token, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) ViewGroup(ctx context.Context, token, id string) (groups.Group, error) {
|
|
|
|
return es.svc.ViewGroup(ctx, token, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) ListGroups(ctx context.Context, token string, level uint64, gm groups.Metadata) (groups.GroupPage, error) {
|
|
|
|
return es.svc.ListGroups(ctx, token, level, gm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) ListParents(ctx context.Context, token, childID string, level uint64, gm groups.Metadata) (groups.GroupPage, error) {
|
|
|
|
return es.svc.ListParents(ctx, token, childID, level, gm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) ListChildren(ctx context.Context, token, parentID string, level uint64, gm groups.Metadata) (groups.GroupPage, error) {
|
|
|
|
return es.svc.ListChildren(ctx, token, parentID, level, gm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) ListMembers(ctx context.Context, token, groupID string, offset, limit uint64, gm groups.Metadata) (groups.MemberPage, error) {
|
|
|
|
return es.svc.ListMembers(ctx, token, groupID, offset, limit, gm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) RemoveGroup(ctx context.Context, token, id string) error {
|
|
|
|
return es.svc.RemoveGroup(ctx, token, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) Unassign(ctx context.Context, token, memberID, groupID string) error {
|
|
|
|
return es.svc.Unassign(ctx, token, memberID, groupID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) UpdateGroup(ctx context.Context, token string, g groups.Group) (groups.Group, error) {
|
|
|
|
return es.svc.UpdateGroup(ctx, token, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) Assign(ctx context.Context, token, memberID, groupID string) error {
|
|
|
|
return es.svc.Assign(ctx, token, memberID, groupID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es eventStore) ListMemberships(ctx context.Context, token, memberID string, offset, limit uint64, gm groups.Metadata) (groups.GroupPage, error) {
|
|
|
|
return es.svc.ListMemberships(ctx, token, memberID, offset, limit, gm)
|
|
|
|
}
|