Skip to content
Snippets Groups Projects
Commit dce7a5b8 authored by Alex Petraky's avatar Alex Petraky :basketball_player_tone1: Committed by Pavel Antonov
Browse files

Добавлен метод Find для сервиса Spaces

parent a759cbd1
No related branches found
No related tags found
No related merge requests found
Showing
with 301 additions and 4 deletions
Subproject commit f10336dc4a4f58111c12dd95afec82be18388803
Subproject commit 2fd02d31a11e9c98547acc4f8416d17c5367411c
......@@ -11,6 +11,7 @@ import (
"time"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
"go.uber.org/zap"
)
......@@ -86,6 +87,27 @@ func (m *accessLoggingMiddleware) Delete(ctx context.Context, spaceId string) (e
return err
}
func (m *accessLoggingMiddleware) Find(ctx context.Context, filter *spaces.Filter, fo *options.FindOptions) (spaces []*spaces.Space, total int, err error) {
begin := time.Now()
m.logger.Debug("Find.Request",
zap.Reflect("principal", auth.GetPrincipal(ctx)),
zap.Reflect("filter", filter),
zap.Reflect("fo", fo),
)
spaces, total, err = m.next.Find(ctx, filter, fo)
m.logger.Debug("Find.Response",
zap.Duration("time", time.Since(begin)),
zap.Reflect("spaces", spaces),
zap.Reflect("total", total),
zap.Error(err),
)
return spaces, total, err
}
func (m *accessLoggingMiddleware) Get(ctx context.Context, spaceId string) (space *spaces.Space, err error) {
begin := time.Now()
......
......@@ -4,6 +4,7 @@ import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/cache"
"git.perx.ru/perxis/perxis-go/pkg/options"
service "git.perx.ru/perxis/perxis-go/pkg/spaces"
)
......@@ -63,6 +64,10 @@ func (m cachingMiddleware) List(ctx context.Context, orgId string) (spaces []*se
return spaces, err
}
func (m cachingMiddleware) Find(ctx context.Context, filter *service.Filter, fo *options.FindOptions) (spaces []*service.Space, total int, err error) {
return m.next.Find(ctx, filter, fo)
}
func (m cachingMiddleware) Update(ctx context.Context, space *service.Space) (err error) {
err = m.next.Update(ctx, space)
......
......@@ -9,6 +9,7 @@ package middleware
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
"go.uber.org/zap"
)
......@@ -59,6 +60,16 @@ func (m *errorLoggingMiddleware) Delete(ctx context.Context, spaceId string) (er
return m.next.Delete(ctx, spaceId)
}
func (m *errorLoggingMiddleware) Find(ctx context.Context, filter *spaces.Filter, fo *options.FindOptions) (spaces []*spaces.Space, total int, err error) {
logger := m.logger
defer func() {
if err != nil {
logger.Warn("response error", zap.Error(err))
}
}()
return m.next.Find(ctx, filter, fo)
}
func (m *errorLoggingMiddleware) Get(ctx context.Context, spaceId string) (space *spaces.Space, err error) {
logger := m.logger
defer func() {
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
logzap "git.perx.ru/perxis/perxis-go/zap"
......@@ -112,6 +113,20 @@ func (m *loggingMiddleware) List(ctx context.Context, orgId string) (spaces []*s
return spaces, err
}
func (m *loggingMiddleware) Find(ctx context.Context, filter *spaces.Filter, fo *options.FindOptions) (spaces []*spaces.Space, total int, err error) {
logger := m.logger.With(
logzap.Caller(ctx),
)
spaces, total, err = m.next.Find(ctx, filter, fo)
if err != nil {
logger.Error("Failed to find", zap.Error(err))
return
}
return spaces, total, err
}
func (m *loggingMiddleware) ListTransfers(ctx context.Context, orgID string) (spaces []*spaces.Space, err error) {
logger := m.logger.With(
logzap.Caller(ctx),
......
......@@ -21,7 +21,7 @@ func WithLog(s spaces.Spaces, logger *zap.Logger, log_access bool) spaces.Spaces
if log_access {
s = AccessLoggingMiddleware(logger)(s)
}
s = LoggingMiddleware(logger)(s)
s = ErrorLoggingMiddleware(logger)(s)
s = RecoveringMiddleware(logger)(s)
return s
......
......@@ -10,6 +10,7 @@ import (
"context"
"fmt"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
"go.uber.org/zap"
)
......@@ -66,6 +67,18 @@ func (m *recoveringMiddleware) Delete(ctx context.Context, spaceId string) (err
return m.next.Delete(ctx, spaceId)
}
func (m *recoveringMiddleware) Find(ctx context.Context, filter *spaces.Filter, fo *options.FindOptions) (spaces []*spaces.Space, total int, err error) {
logger := m.logger
defer func() {
if r := recover(); r != nil {
logger.Error("panic", zap.Error(fmt.Errorf("%v", r)))
err = fmt.Errorf("%v", r)
}
}()
return m.next.Find(ctx, filter, fo)
}
func (m *recoveringMiddleware) Get(ctx context.Context, spaceId string) (space *spaces.Space, err error) {
logger := m.logger
defer func() {
......
......@@ -12,6 +12,7 @@ import (
"context"
"time"
"git.perx.ru/perxis/perxis-go/pkg/options"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
"git.perx.ru/perxis/perxis-go/pkg/telemetry/metrics"
"go.opentelemetry.io/otel"
......@@ -148,6 +149,42 @@ func (_d telemetryMiddleware) Delete(ctx context.Context, spaceId string) (err e
return _d.Spaces.Delete(ctx, spaceId)
}
// Find implements spaces.Spaces
func (_d telemetryMiddleware) Find(ctx context.Context, filter *spaces.Filter, fo *options.FindOptions) (spaces []*spaces.Space, total int, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
attribute.String("service", "Spaces"),
attribute.String("method", "Find"),
))
_d.requestMetrics.Total.Add(ctx, 1, attributes)
start := time.Now()
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Spaces.Find")
defer func() {
_d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes)
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"filter": filter,
"fo": fo}, map[string]interface{}{
"spaces": spaces,
"total": total,
"err": err})
} else if err != nil {
_d.requestMetrics.FailedTotal.Add(ctx, 1, attributes)
_span.RecordError(err)
_span.SetAttributes(attribute.String("event", "error"))
_span.SetAttributes(attribute.String("message", err.Error()))
}
_span.End()
}()
return _d.Spaces.Find(ctx, filter, fo)
}
// Get implements spaces.Spaces
func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string) (space *spaces.Space, err error) {
attributes := otelmetric.WithAttributeSet(attribute.NewSet(
......
......@@ -5,8 +5,10 @@ package mocks
import (
context "context"
spaces "git.perx.ru/perxis/perxis-go/pkg/spaces"
options "git.perx.ru/perxis/perxis-go/pkg/options"
mock "github.com/stretchr/testify/mock"
spaces "git.perx.ru/perxis/perxis-go/pkg/spaces"
)
// Spaces is an autogenerated mock type for the Spaces type
......@@ -80,6 +82,43 @@ func (_m *Spaces) Delete(ctx context.Context, spaceId string) error {
return r0
}
// Find provides a mock function with given fields: ctx, filter, fo
func (_m *Spaces) Find(ctx context.Context, filter *spaces.Filter, fo *options.FindOptions) ([]*spaces.Space, int, error) {
ret := _m.Called(ctx, filter, fo)
if len(ret) == 0 {
panic("no return value specified for Find")
}
var r0 []*spaces.Space
var r1 int
var r2 error
if rf, ok := ret.Get(0).(func(context.Context, *spaces.Filter, *options.FindOptions) ([]*spaces.Space, int, error)); ok {
return rf(ctx, filter, fo)
}
if rf, ok := ret.Get(0).(func(context.Context, *spaces.Filter, *options.FindOptions) []*spaces.Space); ok {
r0 = rf(ctx, filter, fo)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*spaces.Space)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *spaces.Filter, *options.FindOptions) int); ok {
r1 = rf(ctx, filter, fo)
} else {
r1 = ret.Get(1).(int)
}
if rf, ok := ret.Get(2).(func(context.Context, *spaces.Filter, *options.FindOptions) error); ok {
r2 = rf(ctx, filter, fo)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// Get provides a mock function with given fields: ctx, spaceId
func (_m *Spaces) Get(ctx context.Context, spaceId string) (*spaces.Space, error) {
ret := _m.Called(ctx, spaceId)
......
......@@ -5,6 +5,7 @@ import (
"time"
"git.perx.ru/perxis/perxis-go/pkg/errors"
"git.perx.ru/perxis/perxis-go/pkg/options"
"github.com/avast/retry-go/v4"
)
......@@ -26,6 +27,7 @@ type Spaces interface {
Create(ctx context.Context, space *Space) (created *Space, err error)
Get(ctx context.Context, spaceId string) (space *Space, err error)
List(ctx context.Context, orgId string) (spaces []*Space, err error)
Find(ctx context.Context, filter *Filter, fo *options.FindOptions) (spaces []*Space, total int, err error)
Update(ctx context.Context, space *Space) (err error)
UpdateConfig(ctx context.Context, spaceId string, config *Config) (err error)
......
......@@ -5,6 +5,7 @@ package transport
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/options"
spaces "git.perx.ru/perxis/perxis-go/pkg/spaces"
)
......@@ -35,6 +36,15 @@ func (set EndpointsSet) List(arg0 context.Context, arg1 string) (res0 []*spaces.
return response.(*ListResponse).Spaces, res1
}
func (set EndpointsSet) Find(arg0 context.Context, arg1 *spaces.Filter, arg2 *options.FindOptions) (res0 []*spaces.Space, res1 int, res2 error) {
request := FindRequest{Filter: arg1, Options: arg2}
response, res2 := set.FindEndpoint(arg0, &request)
if res2 != nil {
return
}
return response.(*FindResponse).Spaces, response.(*FindResponse).Total, res2
}
func (set EndpointsSet) Update(arg0 context.Context, arg1 *spaces.Space) (res0 error) {
request := UpdateRequest{Space: arg1}
_, res0 = set.UpdateEndpoint(arg0, &request)
......
......@@ -9,6 +9,7 @@ type EndpointsSet struct {
CreateEndpoint endpoint.Endpoint
GetEndpoint endpoint.Endpoint
ListEndpoint endpoint.Endpoint
FindEndpoint endpoint.Endpoint
UpdateEndpoint endpoint.Endpoint
UpdateConfigEndpoint endpoint.Endpoint
DeleteEndpoint endpoint.Endpoint
......
......@@ -2,7 +2,10 @@
package transport
import spaces "git.perx.ru/perxis/perxis-go/pkg/spaces"
import (
"git.perx.ru/perxis/perxis-go/pkg/options"
spaces "git.perx.ru/perxis/perxis-go/pkg/spaces"
)
type (
CreateRequest struct {
......@@ -26,6 +29,15 @@ type (
Spaces []*spaces.Space `json:"spaces"`
}
FindRequest struct {
Filter *spaces.Filter `json:"filter"`
Options *options.FindOptions `json:"options"`
}
FindResponse struct {
Spaces []*spaces.Space `json:"spaces"`
Total int `json:"total"`
}
UpdateRequest struct {
Space *spaces.Space `json:"space"`
}
......
......@@ -15,6 +15,7 @@ func NewClient(conn *grpc.ClientConn, opts ...grpckit.ClientOption) transport.En
CreateEndpoint: grpcerr.ClientMiddleware(c.CreateEndpoint),
GetEndpoint: grpcerr.ClientMiddleware(c.GetEndpoint),
ListEndpoint: grpcerr.ClientMiddleware(c.ListEndpoint),
FindEndpoint: grpcerr.ClientMiddleware(c.FindEndpoint),
UpdateEndpoint: grpcerr.ClientMiddleware(c.UpdateEndpoint),
UpdateConfigEndpoint: grpcerr.ClientMiddleware(c.UpdateConfigEndpoint),
DeleteEndpoint: grpcerr.ClientMiddleware(c.DeleteEndpoint),
......
......@@ -50,6 +50,13 @@ func NewGRPCClient(conn *grpc.ClientConn, addr string, opts ...grpckit.ClientOpt
pb.ListResponse{},
opts...,
).Endpoint(),
FindEndpoint: grpckit.NewClient(
conn, addr, "Find",
_Encode_Find_Request,
_Decode_Find_Response,
pb.FindResponse{},
opts...,
).Endpoint(),
ListTransfersEndpoint: grpckit.NewClient(
conn, addr, "ListTransfers",
_Encode_ListTransfers_Request,
......
......@@ -7,6 +7,7 @@ import (
"context"
"errors"
transportgrpc "git.perx.ru/perxis/perxis-go/pkg/items/transport/grpc"
transport "git.perx.ru/perxis/perxis-go/pkg/spaces/transport"
pb "git.perx.ru/perxis/perxis-go/proto/spaces"
empty "google.golang.org/protobuf/types/known/emptypb"
......@@ -40,6 +41,22 @@ func _Encode_List_Request(ctx context.Context, request interface{}) (interface{}
return &pb.ListRequest{OrgId: req.OrgId}, nil
}
func _Encode_Find_Request(ctx context.Context, request interface{}) (interface{}, error) {
if request == nil {
return nil, errors.New("nil FindRequest")
}
req := request.(*transport.FindRequest)
reqFilter, err := SpaceFilterToProto(req.Filter)
if err != nil {
return nil, err
}
reqOptions, err := transportgrpc.PtrServicesFindOptionsToProto(req.Options)
if err != nil {
return nil, err
}
return &pb.FindRequest{Filter: reqFilter, Options: reqOptions}, nil
}
func _Encode_Update_Request(ctx context.Context, request interface{}) (interface{}, error) {
if request == nil {
return nil, errors.New("nil UpdateRequest")
......@@ -111,6 +128,18 @@ func _Encode_List_Response(ctx context.Context, response interface{}) (interface
return &pb.ListResponse{Spaces: respSpaces}, nil
}
func _Encode_Find_Response(ctx context.Context, response interface{}) (interface{}, error) {
if response == nil {
return nil, errors.New("nil FindResponse")
}
resp := response.(*transport.FindResponse)
respSpaces, err := ListPtrSpaceToProto(resp.Spaces)
if err != nil {
return nil, err
}
return &pb.FindResponse{Spaces: respSpaces, Total: int32(resp.Total)}, nil
}
func _Encode_Update_Response(ctx context.Context, response interface{}) (interface{}, error) {
return &empty.Empty{}, nil
}
......@@ -151,6 +180,24 @@ func _Decode_List_Request(ctx context.Context, request interface{}) (interface{}
return &transport.ListRequest{OrgId: string(req.OrgId)}, nil
}
func _Decode_Find_Request(ctx context.Context, request interface{}) (interface{}, error) {
if request == nil {
return nil, errors.New("nil FindRequest")
}
req := request.(*pb.FindRequest)
opts, err := transportgrpc.ProtoToPtrServicesFindOptions(req.Options)
if err != nil {
return nil, err
}
filter, err := ProtoToPtrSpaceFilter(req.Filter)
if err != nil {
return nil, err
}
return &transport.FindRequest{Filter: filter, Options: opts}, nil
}
func _Decode_Update_Request(ctx context.Context, request interface{}) (interface{}, error) {
if request == nil {
return nil, errors.New("nil UpdateRequest")
......@@ -222,6 +269,18 @@ func _Decode_List_Response(ctx context.Context, response interface{}) (interface
return &transport.ListResponse{Spaces: respSpaces}, nil
}
func _Decode_Find_Response(ctx context.Context, response interface{}) (interface{}, error) {
if response == nil {
return nil, errors.New("nil FindResponse")
}
resp := response.(*pb.FindResponse)
respSpaces, err := ProtoToListPtrSpace(resp.Spaces)
if err != nil {
return nil, err
}
return &transport.FindResponse{Spaces: respSpaces, Total: int(resp.Total)}, nil
}
func _Decode_Update_Response(ctx context.Context, response interface{}) (interface{}, error) {
return &empty.Empty{}, nil
}
......
......@@ -108,3 +108,41 @@ func ProtoToListPtrSpace(protoSpaces []*pb.Space) ([]*service.Space, error) {
}
return spaces, nil
}
func ProtoToPtrSpaceFilter(protoSpaceFilter *pb.Filter) (*service.Filter, error) {
if protoSpaceFilter == nil {
return nil, nil
}
var srvState []service.State
if len(protoSpaceFilter.State) != 0 {
for _, state := range protoSpaceFilter.State {
srvState = append(srvState, service.State(state))
}
}
return &service.Filter{
ID: protoSpaceFilter.Id,
OrgID: protoSpaceFilter.OrgId,
Name: protoSpaceFilter.Name,
State: srvState,
TransferToOrg: protoSpaceFilter.TransferToOrg,
}, nil
}
func SpaceFilterToProto(spaceFilter *service.Filter) (*pb.Filter, error) {
if spaceFilter == nil {
return nil, nil
}
var pbState []pb.State
if len(spaceFilter.State) != 0 {
for _, state := range spaceFilter.State {
pbState = append(pbState, pb.State(state))
}
}
return &pb.Filter{
Id: spaceFilter.ID,
OrgId: spaceFilter.OrgID,
Name: spaceFilter.Name,
State: pbState,
TransferToOrg: spaceFilter.TransferToOrg,
}, nil
}
......@@ -14,6 +14,7 @@ func NewServer(svc spaces.Spaces, opts ...grpckit.ServerOption) pb.SpacesServer
CreateEndpoint: grpcerr.ServerMiddleware(eps.CreateEndpoint),
GetEndpoint: grpcerr.ServerMiddleware(eps.GetEndpoint),
ListEndpoint: grpcerr.ServerMiddleware(eps.ListEndpoint),
FindEndpoint: grpcerr.ServerMiddleware(eps.FindEndpoint),
UpdateEndpoint: grpcerr.ServerMiddleware(eps.UpdateEndpoint),
UpdateConfigEndpoint: grpcerr.ServerMiddleware(eps.UpdateConfigEndpoint),
DeleteEndpoint: grpcerr.ServerMiddleware(eps.DeleteEndpoint),
......
......@@ -15,6 +15,7 @@ type spacesServer struct {
create grpc.Handler
get grpc.Handler
list grpc.Handler
find grpc.Handler
update grpc.Handler
updateConfig grpc.Handler
delete grpc.Handler
......@@ -58,6 +59,12 @@ func NewGRPCServer(endpoints *transport.EndpointsSet, opts ...grpc.ServerOption)
_Encode_List_Response,
opts...,
),
find: grpc.NewServer(
endpoints.FindEndpoint,
_Decode_Find_Request,
_Encode_Find_Response,
opts...,
),
listTransfers: grpc.NewServer(
endpoints.ListTransfersEndpoint,
_Decode_ListTransfers_Request,
......@@ -115,6 +122,14 @@ func (S *spacesServer) List(ctx context.Context, req *pb.ListRequest) (*pb.ListR
return resp.(*pb.ListResponse), nil
}
func (S *spacesServer) Find(ctx context.Context, req *pb.FindRequest) (*pb.FindResponse, error) {
_, resp, err := S.find.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return resp.(*pb.FindResponse), nil
}
func (S *spacesServer) Update(ctx context.Context, req *pb.UpdateRequest) (*empty.Empty, error) {
_, resp, err := S.update.ServeGRPC(ctx, req)
if err != nil {
......
......@@ -16,6 +16,7 @@ func Endpoints(svc spaces.Spaces) EndpointsSet {
DeleteEndpoint: DeleteEndpoint(svc),
GetEndpoint: GetEndpoint(svc),
ListEndpoint: ListEndpoint(svc),
FindEndpoint: FindEndpoint(svc),
ListTransfersEndpoint: ListTransfersEndpoint(svc),
MoveEndpoint: MoveEndpoint(svc),
TransferEndpoint: TransferEndpoint(svc),
......@@ -48,6 +49,14 @@ func ListEndpoint(svc spaces.Spaces) endpoint.Endpoint {
}
}
func FindEndpoint(svc spaces.Spaces) endpoint.Endpoint {
return func(arg0 context.Context, request interface{}) (interface{}, error) {
req := request.(*FindRequest)
res0, res1, res2 := svc.Find(arg0, req.Filter, req.Options)
return &FindResponse{Spaces: res0, Total: res1}, res2
}
}
func UpdateEndpoint(svc spaces.Spaces) endpoint.Endpoint {
return func(arg0 context.Context, request interface{}) (interface{}, error) {
req := request.(*UpdateRequest)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment