2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-01-24 21:18:53 +01:00
|
|
|
//go:build !test
|
2018-04-19 13:40:22 +02:00
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
2018-04-04 10:15:23 +02:00
|
|
|
"fmt"
|
2017-09-23 01:03:27 +02:00
|
|
|
"time"
|
|
|
|
|
2018-04-04 10:15:23 +02:00
|
|
|
log "github.com/mainflux/mainflux/logger"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2017-09-23 01:03:27 +02:00
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
var _ things.Service = (*loggingMiddleware)(nil)
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
type loggingMiddleware struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
logger log.Logger
|
2018-05-15 17:13:09 +02:00
|
|
|
svc things.Service
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
// LoggingMiddleware adds logging facilities to the core service.
|
2018-05-15 17:13:09 +02:00
|
|
|
func LoggingMiddleware(svc things.Service, logger log.Logger) things.Service {
|
2018-03-11 18:06:01 +01:00
|
|
|
return &loggingMiddleware{logger, svc}
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, ths ...things.Thing) (saved []things.Thing, err error) {
|
2019-10-29 05:59:54 -06:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method create_things for token %s and things %s took %s to complete", token, saved, time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
return lm.svc.CreateThings(ctx, token, ths...)
|
2019-10-29 05:59:54 -06:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) UpdateThing(ctx context.Context, token string, thing things.Thing) (err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method update_thing for token %s and thing %s took %s to complete", token, thing.ID, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.UpdateThing(ctx, token, thing)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
func (lm *loggingMiddleware) ShareThing(ctx context.Context, token, thingID string, actions, userIDs []string) (err error) {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method share_thing for token %s and thing %s took %s to complete", token, thingID, time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return lm.svc.ShareThing(ctx, token, thingID, actions, userIDs)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) UpdateKey(ctx context.Context, token, id, key string) (err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method update_key for thing %s and key %s took %s to complete", id, key, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.UpdateKey(ctx, token, id, key)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) ViewThing(ctx context.Context, token, id string) (thing things.Thing, err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method view_thing for token %s and thing %s took %s to complete", token, id, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.ViewThing(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
func (lm *loggingMiddleware) ListThings(ctx context.Context, token string, pm things.PageMetadata) (_ things.Page, err error) {
|
2019-04-25 14:37:51 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-05-30 15:33:49 +02:00
|
|
|
nlog := ""
|
2020-12-08 21:30:47 +01:00
|
|
|
if pm.Name != "" {
|
|
|
|
nlog = fmt.Sprintf("with name %s", pm.Name)
|
2019-05-30 15:33:49 +02:00
|
|
|
}
|
2020-12-08 21:30:47 +01:00
|
|
|
message := fmt.Sprintf("Method list_things %s for token %s took %s to complete", nlog, token, time.Since(begin))
|
2019-04-25 14:37:51 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
return lm.svc.ListThings(ctx, token, pm)
|
2019-04-25 14:37:51 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
func (lm *loggingMiddleware) ListThingsByChannel(ctx context.Context, token, chID string, pm things.PageMetadata) (_ things.Page, err error) {
|
2019-01-08 11:53:24 +01:00
|
|
|
defer func(begin time.Time) {
|
2021-02-17 16:21:40 +01:00
|
|
|
message := fmt.Sprintf("Method list_things_by_channel for channel %s took %s to complete", chID, time.Since(begin))
|
2019-01-08 11:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s", message, err))
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
return lm.svc.ListThingsByChannel(ctx, token, chID, pm)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) RemoveThing(ctx context.Context, token, id string) (err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method remove_thing for token %s and thing %s took %s to complete", token, id, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.RemoveThing(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (lm *loggingMiddleware) CreateChannels(ctx context.Context, token string, channels ...things.Channel) (saved []things.Channel, err error) {
|
2019-10-29 05:59:54 -06:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method create_channels for token %s and channels %s took %s to complete", token, saved, time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
return lm.svc.CreateChannels(ctx, token, channels...)
|
2019-10-29 05:59:54 -06:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) UpdateChannel(ctx context.Context, token string, channel things.Channel) (err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method update_channel for token %s and channel %s took %s to complete", token, channel.ID, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.UpdateChannel(ctx, token, channel)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) ViewChannel(ctx context.Context, token, id string) (channel things.Channel, err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method view_channel for token %s and channel %s took %s to complete", token, id, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.ViewChannel(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
func (lm *loggingMiddleware) ListChannels(ctx context.Context, token string, pm things.PageMetadata) (_ things.ChannelsPage, err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-06-11 10:37:25 +02:00
|
|
|
nlog := ""
|
2020-12-08 21:30:47 +01:00
|
|
|
if pm.Name != "" {
|
|
|
|
nlog = fmt.Sprintf("with name %s", pm.Name)
|
2019-06-11 10:37:25 +02:00
|
|
|
}
|
2020-11-23 11:34:29 +01:00
|
|
|
message := fmt.Sprintf("Method list_channels %s for token %s took %s to complete", nlog, token, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
return lm.svc.ListChannels(ctx, token, pm)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
func (lm *loggingMiddleware) ListChannelsByThing(ctx context.Context, token, thID string, pm things.PageMetadata) (_ things.ChannelsPage, err error) {
|
2019-01-08 11:53:24 +01:00
|
|
|
defer func(begin time.Time) {
|
2021-02-17 16:21:40 +01:00
|
|
|
message := fmt.Sprintf("Method list_channels_by_thing for thing %s took %s to complete", thID, time.Since(begin))
|
2019-01-08 11:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s", message, err))
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
return lm.svc.ListChannelsByThing(ctx, token, thID, pm)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) RemoveChannel(ctx context.Context, token, id string) (err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-04-25 14:37:51 +02:00
|
|
|
message := fmt.Sprintf("Method remove_channel for token %s and channel %s took %s to complete", token, id, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.RemoveChannel(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
func (lm *loggingMiddleware) Connect(ctx context.Context, token string, chIDs, thIDs []string) (err error) {
|
2017-12-29 10:47:43 +01:00
|
|
|
defer func(begin time.Time) {
|
2019-11-15 10:44:31 -07:00
|
|
|
message := fmt.Sprintf("Method connect for token %s, channels %s and things %s took %s to complete", token, chIDs, thIDs, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2018-03-11 18:06:01 +01:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
return lm.svc.Connect(ctx, token, chIDs, thIDs)
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
func (lm *loggingMiddleware) Disconnect(ctx context.Context, token string, chIDs, thIDs []string) (err error) {
|
2018-03-11 18:06:01 +01:00
|
|
|
defer func(begin time.Time) {
|
2021-07-10 01:59:12 +03:00
|
|
|
message := fmt.Sprintf("Method disconnect for token %s, channels %v and things %v took %s to complete", token, chIDs, thIDs, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2018-03-11 18:06:01 +01:00
|
|
|
}(time.Now())
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
return lm.svc.Disconnect(ctx, token, chIDs, thIDs)
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func (lm *loggingMiddleware) CanAccessByKey(ctx context.Context, id, key string) (thing string, err error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2018-12-05 13:09:25 +01:00
|
|
|
message := fmt.Sprintf("Method can_access for channel %s and thing %s took %s to complete", id, thing, time.Since(begin))
|
2018-04-04 10:15:23 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
return lm.svc.CanAccessByKey(ctx, id, key)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) CanAccessByID(ctx context.Context, chanID, thingID string) (err error) {
|
2019-07-15 18:28:15 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method can_access_by_id for channel %s and thing %s took %s to complete", chanID, thingID, time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.CanAccessByID(ctx, chanID, thingID)
|
2019-07-15 18:28:15 +02:00
|
|
|
}
|
2021-02-22 19:41:59 +01:00
|
|
|
|
|
|
|
func (lm *loggingMiddleware) IsChannelOwner(ctx context.Context, owner, chanID string) (err error) {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method is_channel_owner for channel %s and user %s took %s to complete", chanID, owner, time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return lm.svc.IsChannelOwner(ctx, owner, chanID)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id string, err error) {
|
2018-05-17 20:17:02 +02:00
|
|
|
defer func(begin time.Time) {
|
2020-11-23 11:34:29 +01:00
|
|
|
message := fmt.Sprintf("Method identify for token %s and thing %s took %s to complete", key, id, time.Since(begin))
|
2018-05-17 20:17:02 +02:00
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return lm.svc.Identify(ctx, key)
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
2020-11-23 11:34:29 +01:00
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID string, pm things.PageMetadata) (tp things.Page, err error) {
|
2020-11-23 11:34:29 +01:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
message := fmt.Sprintf("Method list_members for token %s and group id %s took %s to complete", token, groupID, time.Since(begin))
|
|
|
|
if err != nil {
|
|
|
|
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
return lm.svc.ListMembers(ctx, token, groupID, pm)
|
2020-11-23 11:34:29 +01:00
|
|
|
}
|