mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-26 13:48:53 +08:00

* Initial commit Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * change active to string Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Set default Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Fix query all users Signed-off-by: GitHub <noreply@github.com> * Set user active on service Signed-off-by: GitHub <noreply@github.com> * Rename active to state Signed-off-by: GitHub <noreply@github.com> * check user active on service Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * format Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * format Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * fix test Signed-off-by: GitHub <noreply@github.com> * Add deactivate user tests Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Rename deactivate to change user status Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Revert to sorting users Signed-off-by: GitHub <noreply@github.com> * change user state Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Change user status to enable and disable Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * change user state to status Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * from enable to activate Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * from activate to enable Signed-off-by: GitHub <noreply@github.com> * not found error by retrievebyID Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Combine enable and disable user Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * Add api docs Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * verify docs Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * change to camel Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * Reword Signed-off-by: b1ackd0t <blackd0t@protonmail.com> * fix default state Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * change from VARCHAR to ENUM Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> * invalid user status test Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com> Signed-off-by: GitHub <noreply@github.com> Signed-off-by: b1ackd0t <blackd0t@protonmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
149 lines
4.9 KiB
Go
149 lines
4.9 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//go:build !test
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
"github.com/mainflux/mainflux/users"
|
|
)
|
|
|
|
var _ users.Service = (*metricsMiddleware)(nil)
|
|
|
|
type metricsMiddleware struct {
|
|
counter metrics.Counter
|
|
latency metrics.Histogram
|
|
svc users.Service
|
|
}
|
|
|
|
// MetricsMiddleware instruments core service by tracking request count and latency.
|
|
func MetricsMiddleware(svc users.Service, counter metrics.Counter, latency metrics.Histogram) users.Service {
|
|
return &metricsMiddleware{
|
|
counter: counter,
|
|
latency: latency,
|
|
svc: svc,
|
|
}
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Register(ctx context.Context, token string, user users.User) (string, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "register").Add(1)
|
|
ms.latency.With("method", "register").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Register(ctx, token, user)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Login(ctx context.Context, user users.User) (string, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "login").Add(1)
|
|
ms.latency.With("method", "login").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Login(ctx, user)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ViewUser(ctx context.Context, token, id string) (users.User, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "view_user").Add(1)
|
|
ms.latency.With("method", "view_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ViewUser(ctx, token, id)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ViewProfile(ctx context.Context, token string) (users.User, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "view_profile").Add(1)
|
|
ms.latency.With("method", "view_profile").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ViewProfile(ctx, token)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ListUsers(ctx context.Context, token string, pm users.PageMetadata) (users.UserPage, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "list_users").Add(1)
|
|
ms.latency.With("method", "list_users").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ListUsers(ctx, token, pm)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) UpdateUser(ctx context.Context, token string, u users.User) (err error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "update_user").Add(1)
|
|
ms.latency.With("method", "update_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.UpdateUser(ctx, token, u)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) GenerateResetToken(ctx context.Context, email, host string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "generate_reset_token").Add(1)
|
|
ms.latency.With("method", "generate_reset_token").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.GenerateResetToken(ctx, email, host)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ChangePassword(ctx context.Context, email, password, oldPassword string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "change_password").Add(1)
|
|
ms.latency.With("method", "change_password").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ChangePassword(ctx, email, password, oldPassword)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ResetPassword(ctx context.Context, email, password string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "reset_password").Add(1)
|
|
ms.latency.With("method", "reset_password").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ResetPassword(ctx, email, password)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) SendPasswordReset(ctx context.Context, host, email, token string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "send_password_reset").Add(1)
|
|
ms.latency.With("method", "send_password_reset").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.SendPasswordReset(ctx, host, email, token)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ListMembers(ctx context.Context, token, groupID string, pm users.PageMetadata) (users.UserPage, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "list_members").Add(1)
|
|
ms.latency.With("method", "list_members").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ListMembers(ctx, token, groupID, pm)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) EnableUser(ctx context.Context, token string, id string) (err error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "enable_user").Add(1)
|
|
ms.latency.With("method", "enable_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.EnableUser(ctx, token, id)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) DisableUser(ctx context.Context, token string, id string) (err error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "disable_user").Add(1)
|
|
ms.latency.With("method", "disable_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.DisableUser(ctx, token, id)
|
|
}
|