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

* add things group Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add things group repository Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add things group repository Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add things group repository Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add parents and children methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add parents and children methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix logging message and temporary test fix Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix bootstrap test fail Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups to pkg Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups to pkg Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups to pkg Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups to internal Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move groups to internal Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix import Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolve comments, add hierarchy info when retrieving groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * code refactor, separate http into multiple files Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * code refactor, separate http into multiple files Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix group update Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use user id when saving Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use user id when saving Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * rename methods Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove temporary test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments, fix responses in api for groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add checks for name length Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> fix validation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> small change to response Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add comment for Level Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add level to limit hierarchy retrieval Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> tidy vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add level to limit hierarchy retrieval Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> mod tidy Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> revert Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> resolve comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> small naming and code organize refactor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> revert Member type Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> fix typo Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> use ltree Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> use ltree Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add level for retrieving Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add level Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add ltre Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> upgrade postgres version in test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> add ltre Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> remove test for now Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> minor fixes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> fix id setting Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> fix tree endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> minor style changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small changes, adding new lines Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change function signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change primary key Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change function signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * simplufy code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove groups test for now Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix tabulation Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix whitespace Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove white space, fix grammar Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix sqls so that not additional retrieve is needed Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix sqls so that not additional retrieve is needed Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> fix primary key Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> remove retrive by name Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove name and parent update Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix cound sql Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add line Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * simplify sql, fix table name Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add date Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add date Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add timestamp to api Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix var name Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix var name Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
287 lines
8.0 KiB
Go
287 lines
8.0 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package redis
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-redis/redis"
|
|
"github.com/mainflux/mainflux/internal/groups"
|
|
"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,
|
|
}
|
|
}
|
|
|
|
func (es eventStore) CreateThings(ctx context.Context, token string, ths ...things.Thing) ([]things.Thing, error) {
|
|
sths, err := es.svc.CreateThings(ctx, token, ths...)
|
|
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
|
|
}
|
|
|
|
func (es eventStore) UpdateThing(ctx context.Context, token string, thing things.Thing) error {
|
|
if err := es.svc.UpdateThing(ctx, token, thing); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := updateThingEvent{
|
|
id: thing.ID,
|
|
name: thing.Name,
|
|
metadata: thing.Metadata,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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.
|
|
func (es eventStore) UpdateKey(ctx context.Context, token, id, key string) error {
|
|
return es.svc.UpdateKey(ctx, token, id, key)
|
|
}
|
|
|
|
func (es eventStore) ViewThing(ctx context.Context, token, id string) (things.Thing, error) {
|
|
return es.svc.ViewThing(ctx, token, id)
|
|
}
|
|
|
|
func (es eventStore) ListThings(ctx context.Context, token string, offset, limit uint64, name string, metadata things.Metadata) (things.Page, error) {
|
|
return es.svc.ListThings(ctx, token, offset, limit, name, metadata)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (es eventStore) RemoveThing(ctx context.Context, token, id string) error {
|
|
if err := es.svc.RemoveThing(ctx, token, id); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := removeThingEvent{
|
|
id: id,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) CreateChannels(ctx context.Context, token string, channels ...things.Channel) ([]things.Channel, error) {
|
|
schs, err := es.svc.CreateChannels(ctx, token, channels...)
|
|
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
|
|
}
|
|
|
|
func (es eventStore) UpdateChannel(ctx context.Context, token string, channel things.Channel) error {
|
|
if err := es.svc.UpdateChannel(ctx, token, channel); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := updateChannelEvent{
|
|
id: channel.ID,
|
|
name: channel.Name,
|
|
metadata: channel.Metadata,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) ViewChannel(ctx context.Context, token, id string) (things.Channel, error) {
|
|
return es.svc.ViewChannel(ctx, token, id)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (es eventStore) RemoveChannel(ctx context.Context, token, id string) error {
|
|
if err := es.svc.RemoveChannel(ctx, token, id); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := removeChannelEvent{
|
|
id: id,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) Connect(ctx context.Context, token string, chIDs, thIDs []string) error {
|
|
if err := es.svc.Connect(ctx, token, chIDs, thIDs); err != nil {
|
|
return err
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) Disconnect(ctx context.Context, token, chanID, thingID string) error {
|
|
if err := es.svc.Disconnect(ctx, token, chanID, thingID); err != nil {
|
|
return err
|
|
}
|
|
|
|
event := disconnectThingEvent{
|
|
chanID: chanID,
|
|
thingID: thingID,
|
|
}
|
|
record := &redis.XAddArgs{
|
|
Stream: streamID,
|
|
MaxLenApprox: streamLen,
|
|
Values: event.Encode(),
|
|
}
|
|
es.client.XAdd(record).Err()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (es eventStore) CanAccessByKey(ctx context.Context, chanID string, key string) (string, error) {
|
|
return es.svc.CanAccessByKey(ctx, chanID, key)
|
|
}
|
|
|
|
func (es eventStore) CanAccessByID(ctx context.Context, chanID string, thingID string) error {
|
|
return es.svc.CanAccessByID(ctx, chanID, thingID)
|
|
}
|
|
|
|
func (es eventStore) Identify(ctx context.Context, key string) (string, error) {
|
|
return es.svc.Identify(ctx, key)
|
|
}
|
|
|
|
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)
|
|
}
|