From c7a8e3b6e926001cc672263e83e762e3232859f5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Apr 2024 13:57:26 +0300 Subject: [PATCH 1/9] add promo stats activation with time duration --- docs/proto/promo.proto | 16 +++++ .../promocode/promocode_controller.go | 26 ++++++-- internal/controller/promocode/route.go | 1 - internal/repository/promocode_stats.go | 63 ++++++++++++++++++- internal/services/promocode_service.go | 11 ++++ 5 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 docs/proto/promo.proto diff --git a/docs/proto/promo.proto b/docs/proto/promo.proto new file mode 100644 index 0000000..45a796a --- /dev/null +++ b/docs/proto/promo.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package codeword; + +message Time { + int64 from = 1; + int64 to = 2; +} + +service PromoCodeService { + rpc GetAllPromoActivations(Time) returns (PromoActivationResp); +} + +message PromoActivationResp { + map response = 1; +} diff --git a/internal/controller/promocode/promocode_controller.go b/internal/controller/promocode/promocode_controller.go index 630a416..24ec9bd 100644 --- a/internal/controller/promocode/promocode_controller.go +++ b/internal/controller/promocode/promocode_controller.go @@ -102,13 +102,13 @@ func (p *PromoCodeController) GetList(c *fiber.Ctx) error { func (p *PromoCodeController) Activate(c *fiber.Ctx) error { err := p.authMiddleware(c) - fmt.Println("SKER0",err) + fmt.Println("SKER0", err) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err}) } userID := c.Locals(models.AuthJWTDecodedUserIDKey).(string) - fmt.Println("SKER1",userID) + fmt.Println("SKER1", userID) if userID == "" { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "failed to get jwt payload"}) } @@ -121,10 +121,10 @@ func (p *PromoCodeController) Activate(c *fiber.Ctx) error { if req.Codeword == "" && req.FastLink == "" { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "codeword or fastlink is required"}) } - fmt.Println("SKER2",req) + fmt.Println("SKER2", req) greetings, err := p.promoCodeService.ActivatePromo(c.Context(), &req, userID) - fmt.Println("SKER3",err) + fmt.Println("SKER3", err) if err != nil { p.logger.Error("Failed to activate promocode", zap.Error(err)) @@ -206,7 +206,23 @@ func (p *PromoCodeController) GetStats(c *fiber.Ctx) error { promoStats, err := p.promoCodeService.GetStats(c.Context(), req) if err != nil { p.logger.Error("Failed getting promo stats", zap.Error(err)) - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: "+err.Error()}) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: " + err.Error()}) } return c.Status(fiber.StatusOK).JSON(promoStats) } + +func (p *PromoCodeController) GetAllPromoActivations(c *fiber.Ctx) error { + var req models.Time + + if err := c.BodyParser(&req); err != nil { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"}) + } + + result, err := p.promoCodeService.GetAllPromoActivations(c.Context(), &req) + if err != nil { + p.logger.Error("Failed getting all promo activations", zap.Error(err)) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: " + err.Error()}) + } + + return c.Status(fiber.StatusOK).JSON(result) +} diff --git a/internal/controller/promocode/route.go b/internal/controller/promocode/route.go index d89fcdc..077a0dd 100644 --- a/internal/controller/promocode/route.go +++ b/internal/controller/promocode/route.go @@ -10,7 +10,6 @@ func (p *PromoCodeController) Register(router fiber.Router) { router.Delete("/:promocodeID", p.Delete) router.Post("/fastlink", p.CreateFastLink) router.Post("/stats", p.GetStats) - } func (p *PromoCodeController) Name() string { diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index dcca919..81e5ccd 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -69,11 +69,68 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string) var promoCodeStats models.PromoCodeStats err = r.mdb.FindOne(ctx, filter).Decode(&promoCodeStats) if err != nil { - if err == mongo.ErrNoDocuments { - return models.PromoCodeStats{}, nil - } + if err == mongo.ErrNoDocuments { + return models.PromoCodeStats{}, nil + } return models.PromoCodeStats{}, err } return promoCodeStats, nil } + +func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) { + pipeline := []bson.M{ + { + "$project": bson.M{ + "_id": 1, + "usageArray": bson.M{"$objectToArray": "$usageMap"}, + }, + }, + { + "$unwind": "$usageArray", + }, + { + "$unwind": "$usageArray.v", + }, + { + "$match": bson.M{ + "usageArray.v.time": bson.M{ + "$gte": req.From, + "$lte": req.To, + }, + }, + }, + { + "$group": bson.M{ + "_id": "$_id", + "users": bson.M{"$push": bson.M{ + "UserID": "$usageArray.v.userID", + }}, + }, + }, + } + + cursor, err := r.mdb.Aggregate(ctx, pipeline) + if err != nil { + return nil, err + } + + result := make(map[string][]string) + for cursor.Next(ctx) { + var data struct { + ID string `bson:"_id"` + Users []struct { + UserID string `bson:"UserID"` + } `bson:"users"` + } + err := cursor.Decode(&data) + if err != nil { + return nil, err + } + for _, user := range data.Users { + result[data.ID] = append(result[data.ID], user.UserID) + } + } + + return result, nil +} diff --git a/internal/services/promocode_service.go b/internal/services/promocode_service.go index 2ed3136..4d45c43 100644 --- a/internal/services/promocode_service.go +++ b/internal/services/promocode_service.go @@ -28,6 +28,7 @@ type PromoCodeRepository interface { type PromoStatsRepository interface { UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error) + GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) } type PromoDeps struct { @@ -268,3 +269,13 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq return resp, nil } + +func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) { + result, err := s.statsRepo.GetAllPromoActivations(ctx, req) + if err != nil { + s.logger.Error("error getting all promo activations data", zap.Error(err)) + return nil, err + } + + return result, nil +} From 0912f5496d1f7b319f35fd4417550c7681a38dbb Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Apr 2024 14:23:22 +0300 Subject: [PATCH 2/9] add proto --- docs/proto/promo.proto | 7 +- internal/proto/codeword/promo.pb.go | 296 +++++++++++++++++++++++ internal/proto/codeword/promo_grpc.pb.go | 101 ++++++++ 3 files changed, 403 insertions(+), 1 deletion(-) create mode 100644 internal/proto/codeword/promo.pb.go create mode 100644 internal/proto/codeword/promo_grpc.pb.go diff --git a/docs/proto/promo.proto b/docs/proto/promo.proto index 45a796a..052aca2 100644 --- a/docs/proto/promo.proto +++ b/docs/proto/promo.proto @@ -2,6 +2,8 @@ syntax = "proto3"; package codeword; +option go_package = "./codeword_rpc"; + message Time { int64 from = 1; int64 to = 2; @@ -12,5 +14,8 @@ service PromoCodeService { } message PromoActivationResp { - map response = 1; + message Activations { + repeated string values = 1; + } + map response = 1; } diff --git a/internal/proto/codeword/promo.pb.go b/internal/proto/codeword/promo.pb.go new file mode 100644 index 0000000..4262b0f --- /dev/null +++ b/internal/proto/codeword/promo.pb.go @@ -0,0 +1,296 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.25.0-devel +// protoc v3.14.0 +// source: promo.proto + +package codeword_rpc + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Time struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + From int64 `protobuf:"varint,1,opt,name=from,proto3" json:"from,omitempty"` + To int64 `protobuf:"varint,2,opt,name=to,proto3" json:"to,omitempty"` +} + +func (x *Time) Reset() { + *x = Time{} + if protoimpl.UnsafeEnabled { + mi := &file_promo_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Time) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Time) ProtoMessage() {} + +func (x *Time) ProtoReflect() protoreflect.Message { + mi := &file_promo_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Time.ProtoReflect.Descriptor instead. +func (*Time) Descriptor() ([]byte, []int) { + return file_promo_proto_rawDescGZIP(), []int{0} +} + +func (x *Time) GetFrom() int64 { + if x != nil { + return x.From + } + return 0 +} + +func (x *Time) GetTo() int64 { + if x != nil { + return x.To + } + return 0 +} + +type PromoActivationResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Response map[string]*PromoActivationResp_Activations `protobuf:"bytes,1,rep,name=response,proto3" json:"response,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` +} + +func (x *PromoActivationResp) Reset() { + *x = PromoActivationResp{} + if protoimpl.UnsafeEnabled { + mi := &file_promo_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PromoActivationResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PromoActivationResp) ProtoMessage() {} + +func (x *PromoActivationResp) ProtoReflect() protoreflect.Message { + mi := &file_promo_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PromoActivationResp.ProtoReflect.Descriptor instead. +func (*PromoActivationResp) Descriptor() ([]byte, []int) { + return file_promo_proto_rawDescGZIP(), []int{1} +} + +func (x *PromoActivationResp) GetResponse() map[string]*PromoActivationResp_Activations { + if x != nil { + return x.Response + } + return nil +} + +type PromoActivationResp_Activations struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Values []string `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"` +} + +func (x *PromoActivationResp_Activations) Reset() { + *x = PromoActivationResp_Activations{} + if protoimpl.UnsafeEnabled { + mi := &file_promo_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PromoActivationResp_Activations) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PromoActivationResp_Activations) ProtoMessage() {} + +func (x *PromoActivationResp_Activations) ProtoReflect() protoreflect.Message { + mi := &file_promo_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PromoActivationResp_Activations.ProtoReflect.Descriptor instead. +func (*PromoActivationResp_Activations) Descriptor() ([]byte, []int) { + return file_promo_proto_rawDescGZIP(), []int{1, 0} +} + +func (x *PromoActivationResp_Activations) GetValues() []string { + if x != nil { + return x.Values + } + return nil +} + +var File_promo_proto protoreflect.FileDescriptor + +var file_promo_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x70, 0x72, 0x6f, 0x6d, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x63, + 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x2a, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x12, + 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x66, + 0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x02, 0x74, 0x6f, 0x22, 0xed, 0x01, 0x0a, 0x13, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, + 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x47, 0x0a, 0x08, 0x72, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, + 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, + 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x1a, 0x25, 0x0a, 0x0b, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x09, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x1a, 0x66, 0x0a, 0x0d, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, + 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x3f, + 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x29, 0x2e, + 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, + 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x41, 0x63, 0x74, + 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, + 0x02, 0x38, 0x01, 0x32, 0x5b, 0x0a, 0x10, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x43, 0x6f, 0x64, 0x65, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x47, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x41, 0x6c, + 0x6c, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x12, 0x0e, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x54, 0x69, 0x6d, + 0x65, 0x1a, 0x1d, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, + 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, + 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x72, + 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_promo_proto_rawDescOnce sync.Once + file_promo_proto_rawDescData = file_promo_proto_rawDesc +) + +func file_promo_proto_rawDescGZIP() []byte { + file_promo_proto_rawDescOnce.Do(func() { + file_promo_proto_rawDescData = protoimpl.X.CompressGZIP(file_promo_proto_rawDescData) + }) + return file_promo_proto_rawDescData +} + +var file_promo_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_promo_proto_goTypes = []interface{}{ + (*Time)(nil), // 0: codeword.Time + (*PromoActivationResp)(nil), // 1: codeword.PromoActivationResp + (*PromoActivationResp_Activations)(nil), // 2: codeword.PromoActivationResp.Activations + nil, // 3: codeword.PromoActivationResp.ResponseEntry +} +var file_promo_proto_depIdxs = []int32{ + 3, // 0: codeword.PromoActivationResp.response:type_name -> codeword.PromoActivationResp.ResponseEntry + 2, // 1: codeword.PromoActivationResp.ResponseEntry.value:type_name -> codeword.PromoActivationResp.Activations + 0, // 2: codeword.PromoCodeService.GetAllPromoActivations:input_type -> codeword.Time + 1, // 3: codeword.PromoCodeService.GetAllPromoActivations:output_type -> codeword.PromoActivationResp + 3, // [3:4] is the sub-list for method output_type + 2, // [2:3] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name +} + +func init() { file_promo_proto_init() } +func file_promo_proto_init() { + if File_promo_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_promo_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Time); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_promo_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PromoActivationResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_promo_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PromoActivationResp_Activations); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_promo_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_promo_proto_goTypes, + DependencyIndexes: file_promo_proto_depIdxs, + MessageInfos: file_promo_proto_msgTypes, + }.Build() + File_promo_proto = out.File + file_promo_proto_rawDesc = nil + file_promo_proto_goTypes = nil + file_promo_proto_depIdxs = nil +} diff --git a/internal/proto/codeword/promo_grpc.pb.go b/internal/proto/codeword/promo_grpc.pb.go new file mode 100644 index 0000000..9e225c9 --- /dev/null +++ b/internal/proto/codeword/promo_grpc.pb.go @@ -0,0 +1,101 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. + +package codeword_rpc + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// PromoCodeServiceClient is the client API for PromoCodeService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type PromoCodeServiceClient interface { + GetAllPromoActivations(ctx context.Context, in *Time, opts ...grpc.CallOption) (*PromoActivationResp, error) +} + +type promoCodeServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewPromoCodeServiceClient(cc grpc.ClientConnInterface) PromoCodeServiceClient { + return &promoCodeServiceClient{cc} +} + +func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in *Time, opts ...grpc.CallOption) (*PromoActivationResp, error) { + out := new(PromoActivationResp) + err := c.cc.Invoke(ctx, "/codeword.PromoCodeService/GetAllPromoActivations", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// PromoCodeServiceServer is the server API for PromoCodeService service. +// All implementations must embed UnimplementedPromoCodeServiceServer +// for forward compatibility +type PromoCodeServiceServer interface { + GetAllPromoActivations(context.Context, *Time) (*PromoActivationResp, error) + mustEmbedUnimplementedPromoCodeServiceServer() +} + +// UnimplementedPromoCodeServiceServer must be embedded to have forward compatible implementations. +type UnimplementedPromoCodeServiceServer struct { +} + +func (UnimplementedPromoCodeServiceServer) GetAllPromoActivations(context.Context, *Time) (*PromoActivationResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetAllPromoActivations not implemented") +} +func (UnimplementedPromoCodeServiceServer) mustEmbedUnimplementedPromoCodeServiceServer() {} + +// UnsafePromoCodeServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to PromoCodeServiceServer will +// result in compilation errors. +type UnsafePromoCodeServiceServer interface { + mustEmbedUnimplementedPromoCodeServiceServer() +} + +func RegisterPromoCodeServiceServer(s grpc.ServiceRegistrar, srv PromoCodeServiceServer) { + s.RegisterService(&PromoCodeService_ServiceDesc, srv) +} + +func _PromoCodeService_GetAllPromoActivations_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Time) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(PromoCodeServiceServer).GetAllPromoActivations(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/codeword.PromoCodeService/GetAllPromoActivations", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(PromoCodeServiceServer).GetAllPromoActivations(ctx, req.(*Time)) + } + return interceptor(ctx, in, info, handler) +} + +// PromoCodeService_ServiceDesc is the grpc.ServiceDesc for PromoCodeService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var PromoCodeService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "codeword.PromoCodeService", + HandlerType: (*PromoCodeServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetAllPromoActivations", + Handler: _PromoCodeService_GetAllPromoActivations_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "promo.proto", +} From 2ecb506c79aceeb79b77ae2b9d26a8158f2a8761 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Apr 2024 15:09:28 +0300 Subject: [PATCH 3/9] init rpc serve --- go.mod | 2 +- go.sum | 92 +++++++++++++++++++ internal/app/app.go | 16 ++++ .../promocode/promocode_controller.go | 16 ---- .../controller/rpc_controllers/promoCode.go | 13 +++ internal/initialize/config.go | 2 + internal/repository/promocode_stats.go | 16 +++- internal/server/grpc/rpc_server.go | 74 +++++++++++++++ internal/services/promocode_service.go | 6 +- 9 files changed, 214 insertions(+), 23 deletions(-) create mode 100644 internal/controller/rpc_controllers/promoCode.go create mode 100644 internal/server/grpc/rpc_server.go diff --git a/go.mod b/go.mod index fc04dda..427424c 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-redis/redis/v8 v8.11.5 github.com/gofiber/fiber/v2 v2.51.0 github.com/golang-jwt/jwt/v5 v5.2.0 + github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 github.com/joho/godotenv v1.5.1 github.com/pioz/faker v1.7.3 github.com/rs/xid v1.5.0 @@ -29,7 +30,6 @@ require ( github.com/golang/snappy v0.0.1 // indirect github.com/google/uuid v1.4.0 // indirect github.com/klauspost/compress v1.16.7 // indirect - github.com/kr/pretty v0.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect diff --git a/go.sum b/go.sum index 523b7ea..bac60dd 100644 --- a/go.sum +++ b/go.sum @@ -1,38 +1,64 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/caarlos0/env/v8 v8.0.0 h1:POhxHhSpuxrLMIdvTGARuZqR4Jjm8AYmoi/JKlcScs0= github.com/caarlos0/env/v8 v8.0.0/go.mod h1:7K4wMY9bH0esiXSSHlfHLX5xKGQMnkH5Fk4TDSSSzfo= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ= github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= +github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -54,18 +80,27 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pierrec/lz4/v4 v4.1.19 h1:tYLzDnjDXh9qIxSTKHwXwOYmm9d887Y7Y1ZkyXYHAN4= github.com/pierrec/lz4/v4 v4.1.19/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pioz/faker v1.7.3 h1:Tez8Emuq0UN+/d6mo3a9m/9ZZ/zdfJk0c5RtRatrceM= github.com/pioz/faker v1.7.3/go.mod h1:xSpay5w/oz1a6+ww0M3vfpe40pSIykeUPeWEc3TvVlc= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/twmb/franz-go v1.15.4 h1:qBCkHaiutetnrXjAUWA99D9FEcZVMt2AYwkH3vWEQTw= @@ -86,35 +121,67 @@ github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6 github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -132,16 +199,35 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917 h1:nz5NESFLZbJGPFxDT/HCn+V1mZ8JGNoY4nUpmW/Y2eg= google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917/go.mod h1:pZqR+glSb11aJ+JQcczCvgf47+duRuzNSKqE8YAQnV0= google.golang.org/genproto/googleapis/api v0.0.0-20240116215550-a9fa1716bcac h1:OZkkudMUu9LVQMCoRUbI/1p5VCo9BOrlvkqMvWtqa6s= google.golang.org/genproto/googleapis/api v0.0.0-20240116215550-a9fa1716bcac/go.mod h1:B5xPO//w8qmBDjGReYLpR6UJPnkldGkCSMoH/2vxJeg= google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ= google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917/go.mod h1:xtjpI3tXFPP051KaWnhvxkiubL/6dJ18vLVf7q2pTOU= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU= google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= @@ -149,14 +235,20 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d h1:gbaDt35HMDqOK84WYmDIlXMI7rstUcRqNttaT6Kx1do= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM= diff --git a/internal/app/app.go b/internal/app/app.go index 9e082d9..3bc7917 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -3,8 +3,10 @@ package app import ( "codeword/internal/controller/promocode" "codeword/internal/controller/recovery" + "codeword/internal/controller/rpc_controllers" "codeword/internal/initialize" "codeword/internal/repository" + "codeword/internal/server/grpc" httpserver "codeword/internal/server/http" "codeword/internal/services" "codeword/internal/worker/purge_worker" @@ -106,6 +108,14 @@ func Run(ctx context.Context, cfg initialize.Config, logger *zap.Logger) error { recoveryController := recovery.NewRecoveryController(logger, recoveryService, cfg.DefaultRedirectionURL) promoCodeController := promocode.NewPromoCodeController(promocode.Deps{Logger: logger, PromoCodeService: promoService, AuthMiddleware: authMiddleware}) + controllerRpc := rpc_controllers.InitRpcControllers(promoService) + + grpcServer, err := grpc.NewGRPC(logger) + if err != nil { + logger.Error("error init rpc server", zap.Error(err)) + return err + } + grpcServer.Register(controllerRpc) recoveryWC := recovery_worker.NewRecoveryWC(recovery_worker.Deps{ Logger: logger, @@ -134,9 +144,15 @@ func Run(ctx context.Context, cfg initialize.Config, logger *zap.Logger) error { } }() + go grpcServer.Run(grpc.DepsGrpcRun{ + Host: cfg.GrpcHost, + Port: cfg.GrpcPort, + }) + server.ListRoutes() shutdownGroup.Add(closer.CloserFunc(server.Shutdown)) + shutdownGroup.Add(closer.CloserFunc(grpcServer.Stop)) shutdownGroup.Add(closer.CloserFunc(mdb.Client().Disconnect)) shutdownGroup.Add(closer.CloserFunc(recoveryWC.Stop)) shutdownGroup.Add(closer.CloserFunc(purgeWC.Stop)) diff --git a/internal/controller/promocode/promocode_controller.go b/internal/controller/promocode/promocode_controller.go index 24ec9bd..fae5bdc 100644 --- a/internal/controller/promocode/promocode_controller.go +++ b/internal/controller/promocode/promocode_controller.go @@ -210,19 +210,3 @@ func (p *PromoCodeController) GetStats(c *fiber.Ctx) error { } return c.Status(fiber.StatusOK).JSON(promoStats) } - -func (p *PromoCodeController) GetAllPromoActivations(c *fiber.Ctx) error { - var req models.Time - - if err := c.BodyParser(&req); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"}) - } - - result, err := p.promoCodeService.GetAllPromoActivations(c.Context(), &req) - if err != nil { - p.logger.Error("Failed getting all promo activations", zap.Error(err)) - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: " + err.Error()}) - } - - return c.Status(fiber.StatusOK).JSON(result) -} diff --git a/internal/controller/rpc_controllers/promoCode.go b/internal/controller/rpc_controllers/promoCode.go new file mode 100644 index 0000000..3aa237f --- /dev/null +++ b/internal/controller/rpc_controllers/promoCode.go @@ -0,0 +1,13 @@ +package rpc_controllers + +import "codeword/internal/services" + +type RpcRegister struct { + Service *services.PromoCodeService +} + +func InitRpcControllers(service *services.PromoCodeService) *RpcRegister { + return &RpcRegister{ + Service: service, + } +} diff --git a/internal/initialize/config.go b/internal/initialize/config.go index 23c78b1..f540f03 100644 --- a/internal/initialize/config.go +++ b/internal/initialize/config.go @@ -39,6 +39,8 @@ type Config struct { PublicKey string `env:"JWT_PUBLIC_KEY,required"` Issuer string `env:"JWT_ISSUER,required"` Audience string `env:"JWT_AUDIENCE,required"` + GrpcHost string `env:"GRPC_HOST" envDefault:"localhost"` + GrpcPort string `env:"GRPC_PORT" envDefault:"9000"` } func LoadConfig() (*Config, error) { diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index 81e5ccd..ca0ae14 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -2,6 +2,7 @@ package repository import ( "codeword/internal/models" + codeword_rpc "codeword/internal/proto/codeword" "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" @@ -78,7 +79,7 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string) return promoCodeStats, nil } -func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) { +func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { pipeline := []bson.M{ { "$project": bson.M{ @@ -115,7 +116,7 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *model return nil, err } - result := make(map[string][]string) + result := make(map[string]*codeword_rpc.PromoActivationResp_Activations) for cursor.Next(ctx) { var data struct { ID string `bson:"_id"` @@ -127,10 +128,17 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *model if err != nil { return nil, err } + + if _, ok := result[data.ID]; !ok { + result[data.ID] = &codeword_rpc.PromoActivationResp_Activations{} + } + for _, user := range data.Users { - result[data.ID] = append(result[data.ID], user.UserID) + result[data.ID].Values = append(result[data.ID].Values, user.UserID) } } - return result, nil + return &codeword_rpc.PromoActivationResp{ + Response: result, + }, nil } diff --git a/internal/server/grpc/rpc_server.go b/internal/server/grpc/rpc_server.go new file mode 100644 index 0000000..75a69fb --- /dev/null +++ b/internal/server/grpc/rpc_server.go @@ -0,0 +1,74 @@ +package grpc + +import ( + "codeword/internal/controller/rpc_controllers" + codeword_rpc "codeword/internal/proto/codeword" + "context" + "fmt" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap" + grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery" + "go.uber.org/zap" + "google.golang.org/grpc" + "net" + "time" +) + +type GRPC struct { + grpc *grpc.Server + logger *zap.Logger +} + +func NewGRPC(logger *zap.Logger) (*GRPC, error) { + grpcStreamInterceptor := grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( + grpc_zap.StreamServerInterceptor(logger), + grpc_recovery.StreamServerInterceptor(), + )) + + grpcUnaryInterceptor := grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( + grpc_zap.UnaryServerInterceptor(logger), + grpc_recovery.UnaryServerInterceptor(), + )) + + return &GRPC{ + grpc: grpc.NewServer(grpcStreamInterceptor, grpcUnaryInterceptor, grpc.ConnectionTimeout(5*time.Second)), + logger: logger, + }, nil +} + +type DepsGrpcRun struct { + Host string + Port string +} + +func (g *GRPC) Run(config DepsGrpcRun) { + connectionString := fmt.Sprintf("%s:%s", config.Host, config.Port) + + g.logger.Info("Starting GRPC Server", zap.String("host", connectionString)) + + if err := g.listen(connectionString); err != nil && err != grpc.ErrServerStopped { + g.logger.Error("GRPC Listen error", zap.Error(err)) + } +} + +func (g *GRPC) Stop(_ context.Context) error { + g.grpc.GracefulStop() + g.logger.Info("Shutting down GRPC server...") + + return nil +} + +func (g *GRPC) Register(reg *rpc_controllers.RpcRegister) *GRPC { + codeword_rpc.RegisterPromoCodeServiceServer(g.grpc, reg.Service) + // another + return g +} + +func (g *GRPC) listen(address string) error { + listener, err := net.Listen("tcp", address) + if err != nil { + return err + } + + return g.grpc.Serve(listener) +} diff --git a/internal/services/promocode_service.go b/internal/services/promocode_service.go index 4d45c43..fdc8c24 100644 --- a/internal/services/promocode_service.go +++ b/internal/services/promocode_service.go @@ -3,6 +3,7 @@ package services import ( "codeword/internal/kafka/tariff" "codeword/internal/models" + codeword_rpc "codeword/internal/proto/codeword" "codeword/internal/proto/discount" "codeword/internal/repository" "codeword/internal/utils/genID" @@ -28,7 +29,7 @@ type PromoCodeRepository interface { type PromoStatsRepository interface { UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error) - GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) + GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) } type PromoDeps struct { @@ -45,6 +46,7 @@ type PromoCodeService struct { statsRepo PromoStatsRepository kafka *tariff.Producer discountClient discount.DiscountServiceClient + codeword_rpc.UnimplementedPromoCodeServiceServer } func NewPromoCodeService(deps PromoDeps) *PromoCodeService { @@ -270,7 +272,7 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq return resp, nil } -func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) { +func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { result, err := s.statsRepo.GetAllPromoActivations(ctx, req) if err != nil { s.logger.Error("error getting all promo activations data", zap.Error(err)) From 5863c5ec74d2033bb62a21f3adbdb8fae2418c1b Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Apr 2024 20:44:32 +0300 Subject: [PATCH 4/9] update protobuf --- docs/proto/promo.proto | 11 +++++++++-- internal/repository/promocode_stats.go | 10 ++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/proto/promo.proto b/docs/proto/promo.proto index 052aca2..296fc65 100644 --- a/docs/proto/promo.proto +++ b/docs/proto/promo.proto @@ -4,18 +4,25 @@ package codeword; option go_package = "./codeword_rpc"; +import "google/protobuf/empty.proto"; + message Time { int64 from = 1; int64 to = 2; } service PromoCodeService { - rpc GetAllPromoActivations(Time) returns (PromoActivationResp); + rpc GetAllPromoActivations(google.protobuf.Empty) returns (PromoActivationResp); } message PromoActivationResp { + message UserTime { + string UserID = 1; + int64 Time = 2; + } + message Activations { - repeated string values = 1; + repeated UserTime values = 1; } map response = 1; } diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index ca0ae14..2d96c3d 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -93,19 +93,12 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codew { "$unwind": "$usageArray.v", }, - { - "$match": bson.M{ - "usageArray.v.time": bson.M{ - "$gte": req.From, - "$lte": req.To, - }, - }, - }, { "$group": bson.M{ "_id": "$_id", "users": bson.M{"$push": bson.M{ "UserID": "$usageArray.v.userID", + "Time": "$usageArray.v.time", }}, }, }, @@ -122,6 +115,7 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codew ID string `bson:"_id"` Users []struct { UserID string `bson:"UserID"` + Time int64 } `bson:"users"` } err := cursor.Decode(&data) From ceb8952bd3801e3ecf9787ba6f161de282ba36cc Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Apr 2024 20:47:44 +0300 Subject: [PATCH 5/9] update proto gen --- internal/proto/codeword/promo.pb.go | 167 +++++++++++++++++------ internal/proto/codeword/promo_grpc.pb.go | 13 +- 2 files changed, 130 insertions(+), 50 deletions(-) diff --git a/internal/proto/codeword/promo.pb.go b/internal/proto/codeword/promo.pb.go index 4262b0f..6666ec0 100644 --- a/internal/proto/codeword/promo.pb.go +++ b/internal/proto/codeword/promo.pb.go @@ -9,6 +9,7 @@ package codeword_rpc import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" reflect "reflect" sync "sync" ) @@ -122,18 +123,73 @@ func (x *PromoActivationResp) GetResponse() map[string]*PromoActivationResp_Acti return nil } +type PromoActivationResp_UserTime struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UserID string `protobuf:"bytes,1,opt,name=UserID,proto3" json:"UserID,omitempty"` + Time int64 `protobuf:"varint,2,opt,name=Time,proto3" json:"Time,omitempty"` +} + +func (x *PromoActivationResp_UserTime) Reset() { + *x = PromoActivationResp_UserTime{} + if protoimpl.UnsafeEnabled { + mi := &file_promo_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PromoActivationResp_UserTime) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PromoActivationResp_UserTime) ProtoMessage() {} + +func (x *PromoActivationResp_UserTime) ProtoReflect() protoreflect.Message { + mi := &file_promo_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PromoActivationResp_UserTime.ProtoReflect.Descriptor instead. +func (*PromoActivationResp_UserTime) Descriptor() ([]byte, []int) { + return file_promo_proto_rawDescGZIP(), []int{1, 0} +} + +func (x *PromoActivationResp_UserTime) GetUserID() string { + if x != nil { + return x.UserID + } + return "" +} + +func (x *PromoActivationResp_UserTime) GetTime() int64 { + if x != nil { + return x.Time + } + return 0 +} + type PromoActivationResp_Activations struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Values []string `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"` + Values []*PromoActivationResp_UserTime `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"` } func (x *PromoActivationResp_Activations) Reset() { *x = PromoActivationResp_Activations{} if protoimpl.UnsafeEnabled { - mi := &file_promo_proto_msgTypes[2] + mi := &file_promo_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -146,7 +202,7 @@ func (x *PromoActivationResp_Activations) String() string { func (*PromoActivationResp_Activations) ProtoMessage() {} func (x *PromoActivationResp_Activations) ProtoReflect() protoreflect.Message { - mi := &file_promo_proto_msgTypes[2] + mi := &file_promo_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -159,10 +215,10 @@ func (x *PromoActivationResp_Activations) ProtoReflect() protoreflect.Message { // Deprecated: Use PromoActivationResp_Activations.ProtoReflect.Descriptor instead. func (*PromoActivationResp_Activations) Descriptor() ([]byte, []int) { - return file_promo_proto_rawDescGZIP(), []int{1, 0} + return file_promo_proto_rawDescGZIP(), []int{1, 1} } -func (x *PromoActivationResp_Activations) GetValues() []string { +func (x *PromoActivationResp_Activations) GetValues() []*PromoActivationResp_UserTime { if x != nil { return x.Values } @@ -173,32 +229,40 @@ var File_promo_proto protoreflect.FileDescriptor var file_promo_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x70, 0x72, 0x6f, 0x6d, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x63, - 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x2a, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x12, - 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x66, - 0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, - 0x02, 0x74, 0x6f, 0x22, 0xed, 0x01, 0x0a, 0x13, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, - 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x47, 0x0a, 0x08, 0x72, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, - 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, - 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x1a, 0x25, 0x0a, 0x0b, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, - 0x6f, 0x6e, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x01, 0x20, - 0x03, 0x28, 0x09, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x1a, 0x66, 0x0a, 0x0d, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, - 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x3f, - 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x29, 0x2e, - 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, - 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x41, 0x63, 0x74, - 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, - 0x02, 0x38, 0x01, 0x32, 0x5b, 0x0a, 0x10, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x43, 0x6f, 0x64, 0x65, - 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x47, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x41, 0x6c, - 0x6c, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, - 0x73, 0x12, 0x0e, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x54, 0x69, 0x6d, - 0x65, 0x1a, 0x1d, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, - 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, - 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x72, - 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2a, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, + 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, + 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x74, 0x6f, + 0x22, 0xcd, 0x02, 0x0a, 0x13, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, + 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x47, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x63, 0x6f, 0x64, + 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x1a, 0x36, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x16, 0x0a, + 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, + 0x73, 0x65, 0x72, 0x49, 0x44, 0x12, 0x12, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x03, 0x52, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x1a, 0x4d, 0x0a, 0x0b, 0x41, 0x63, 0x74, + 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x3e, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, + 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x54, 0x69, 0x6d, 0x65, + 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x1a, 0x66, 0x0a, 0x0d, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x3f, 0x0a, 0x05, 0x76, + 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x63, 0x6f, 0x64, + 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, + 0x32, 0x63, 0x0a, 0x10, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x53, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x12, 0x4f, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x50, 0x72, + 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x16, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1d, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, + 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x52, 0x65, 0x73, 0x70, 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x77, + 0x6f, 0x72, 0x64, 0x5f, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -213,23 +277,26 @@ func file_promo_proto_rawDescGZIP() []byte { return file_promo_proto_rawDescData } -var file_promo_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_promo_proto_msgTypes = make([]protoimpl.MessageInfo, 5) var file_promo_proto_goTypes = []interface{}{ (*Time)(nil), // 0: codeword.Time (*PromoActivationResp)(nil), // 1: codeword.PromoActivationResp - (*PromoActivationResp_Activations)(nil), // 2: codeword.PromoActivationResp.Activations - nil, // 3: codeword.PromoActivationResp.ResponseEntry + (*PromoActivationResp_UserTime)(nil), // 2: codeword.PromoActivationResp.UserTime + (*PromoActivationResp_Activations)(nil), // 3: codeword.PromoActivationResp.Activations + nil, // 4: codeword.PromoActivationResp.ResponseEntry + (*emptypb.Empty)(nil), // 5: google.protobuf.Empty } var file_promo_proto_depIdxs = []int32{ - 3, // 0: codeword.PromoActivationResp.response:type_name -> codeword.PromoActivationResp.ResponseEntry - 2, // 1: codeword.PromoActivationResp.ResponseEntry.value:type_name -> codeword.PromoActivationResp.Activations - 0, // 2: codeword.PromoCodeService.GetAllPromoActivations:input_type -> codeword.Time - 1, // 3: codeword.PromoCodeService.GetAllPromoActivations:output_type -> codeword.PromoActivationResp - 3, // [3:4] is the sub-list for method output_type - 2, // [2:3] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 4, // 0: codeword.PromoActivationResp.response:type_name -> codeword.PromoActivationResp.ResponseEntry + 2, // 1: codeword.PromoActivationResp.Activations.values:type_name -> codeword.PromoActivationResp.UserTime + 3, // 2: codeword.PromoActivationResp.ResponseEntry.value:type_name -> codeword.PromoActivationResp.Activations + 5, // 3: codeword.PromoCodeService.GetAllPromoActivations:input_type -> google.protobuf.Empty + 1, // 4: codeword.PromoCodeService.GetAllPromoActivations:output_type -> codeword.PromoActivationResp + 4, // [4:5] is the sub-list for method output_type + 3, // [3:4] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_promo_proto_init() } @@ -263,6 +330,18 @@ func file_promo_proto_init() { } } file_promo_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PromoActivationResp_UserTime); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_promo_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*PromoActivationResp_Activations); i { case 0: return &v.state @@ -281,7 +360,7 @@ func file_promo_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_promo_proto_rawDesc, NumEnums: 0, - NumMessages: 4, + NumMessages: 5, NumExtensions: 0, NumServices: 1, }, diff --git a/internal/proto/codeword/promo_grpc.pb.go b/internal/proto/codeword/promo_grpc.pb.go index 9e225c9..1fabd5e 100644 --- a/internal/proto/codeword/promo_grpc.pb.go +++ b/internal/proto/codeword/promo_grpc.pb.go @@ -7,6 +7,7 @@ import ( grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" ) // This is a compile-time assertion to ensure that this generated file @@ -18,7 +19,7 @@ const _ = grpc.SupportPackageIsVersion7 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type PromoCodeServiceClient interface { - GetAllPromoActivations(ctx context.Context, in *Time, opts ...grpc.CallOption) (*PromoActivationResp, error) + GetAllPromoActivations(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PromoActivationResp, error) } type promoCodeServiceClient struct { @@ -29,7 +30,7 @@ func NewPromoCodeServiceClient(cc grpc.ClientConnInterface) PromoCodeServiceClie return &promoCodeServiceClient{cc} } -func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in *Time, opts ...grpc.CallOption) (*PromoActivationResp, error) { +func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PromoActivationResp, error) { out := new(PromoActivationResp) err := c.cc.Invoke(ctx, "/codeword.PromoCodeService/GetAllPromoActivations", in, out, opts...) if err != nil { @@ -42,7 +43,7 @@ func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in // All implementations must embed UnimplementedPromoCodeServiceServer // for forward compatibility type PromoCodeServiceServer interface { - GetAllPromoActivations(context.Context, *Time) (*PromoActivationResp, error) + GetAllPromoActivations(context.Context, *emptypb.Empty) (*PromoActivationResp, error) mustEmbedUnimplementedPromoCodeServiceServer() } @@ -50,7 +51,7 @@ type PromoCodeServiceServer interface { type UnimplementedPromoCodeServiceServer struct { } -func (UnimplementedPromoCodeServiceServer) GetAllPromoActivations(context.Context, *Time) (*PromoActivationResp, error) { +func (UnimplementedPromoCodeServiceServer) GetAllPromoActivations(context.Context, *emptypb.Empty) (*PromoActivationResp, error) { return nil, status.Errorf(codes.Unimplemented, "method GetAllPromoActivations not implemented") } func (UnimplementedPromoCodeServiceServer) mustEmbedUnimplementedPromoCodeServiceServer() {} @@ -67,7 +68,7 @@ func RegisterPromoCodeServiceServer(s grpc.ServiceRegistrar, srv PromoCodeServic } func _PromoCodeService_GetAllPromoActivations_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(Time) + in := new(emptypb.Empty) if err := dec(in); err != nil { return nil, err } @@ -79,7 +80,7 @@ func _PromoCodeService_GetAllPromoActivations_Handler(srv interface{}, ctx conte FullMethod: "/codeword.PromoCodeService/GetAllPromoActivations", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(PromoCodeServiceServer).GetAllPromoActivations(ctx, req.(*Time)) + return srv.(PromoCodeServiceServer).GetAllPromoActivations(ctx, req.(*emptypb.Empty)) } return interceptor(ctx, in, info, handler) } From 32627fcce243095c05a6060012b25f15d4748d06 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 25 Apr 2024 20:55:07 +0300 Subject: [PATCH 6/9] remove time req body --- internal/repository/promocode_stats.go | 7 +++++-- internal/services/promocode_service.go | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index 2d96c3d..94467c8 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -79,7 +79,7 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string) return promoCodeStats, nil } -func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { +func (r *StatsRepository) GetAllPromoActivations(ctx context.Context) (*codeword_rpc.PromoActivationResp, error) { pipeline := []bson.M{ { "$project": bson.M{ @@ -128,7 +128,10 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codew } for _, user := range data.Users { - result[data.ID].Values = append(result[data.ID].Values, user.UserID) + result[data.ID].Values = append(result[data.ID].Values, &codeword_rpc.PromoActivationResp_UserTime{ + UserID: user.UserID, + Time: user.Time, + }) } } diff --git a/internal/services/promocode_service.go b/internal/services/promocode_service.go index fdc8c24..5633682 100644 --- a/internal/services/promocode_service.go +++ b/internal/services/promocode_service.go @@ -12,6 +12,7 @@ import ( "fmt" "go.mongodb.org/mongo-driver/bson/primitive" "go.uber.org/zap" + "google.golang.org/protobuf/types/known/emptypb" "time" ) @@ -29,7 +30,7 @@ type PromoCodeRepository interface { type PromoStatsRepository interface { UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error) - GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) + GetAllPromoActivations(ctx context.Context) (*codeword_rpc.PromoActivationResp, error) } type PromoDeps struct { @@ -272,8 +273,8 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq return resp, nil } -func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { - result, err := s.statsRepo.GetAllPromoActivations(ctx, req) +func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, _ *emptypb.Empty) (*codeword_rpc.PromoActivationResp, error) { + result, err := s.statsRepo.GetAllPromoActivations(ctx) if err != nil { s.logger.Error("error getting all promo activations data", zap.Error(err)) return nil, err From e5f4768a5127a357c5c9bee453bd34111b94cea5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 26 Apr 2024 10:24:48 +0300 Subject: [PATCH 7/9] return time req --- docs/proto/promo.proto | 4 +- internal/proto/codeword/promo.pb.go | 70 +++++++++++------------- internal/proto/codeword/promo_grpc.pb.go | 13 ++--- 3 files changed, 40 insertions(+), 47 deletions(-) diff --git a/docs/proto/promo.proto b/docs/proto/promo.proto index 296fc65..8d9540c 100644 --- a/docs/proto/promo.proto +++ b/docs/proto/promo.proto @@ -4,15 +4,13 @@ package codeword; option go_package = "./codeword_rpc"; -import "google/protobuf/empty.proto"; - message Time { int64 from = 1; int64 to = 2; } service PromoCodeService { - rpc GetAllPromoActivations(google.protobuf.Empty) returns (PromoActivationResp); + rpc GetAllPromoActivations(Time) returns (PromoActivationResp); } message PromoActivationResp { diff --git a/internal/proto/codeword/promo.pb.go b/internal/proto/codeword/promo.pb.go index 6666ec0..f8e1484 100644 --- a/internal/proto/codeword/promo.pb.go +++ b/internal/proto/codeword/promo.pb.go @@ -9,7 +9,6 @@ package codeword_rpc import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - emptypb "google.golang.org/protobuf/types/known/emptypb" reflect "reflect" sync "sync" ) @@ -229,40 +228,38 @@ var File_promo_proto protoreflect.FileDescriptor var file_promo_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x70, 0x72, 0x6f, 0x6d, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x63, - 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2a, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, - 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, - 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x74, 0x6f, - 0x22, 0xcd, 0x02, 0x0a, 0x13, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x47, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, 0x63, 0x6f, 0x64, - 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, - 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x1a, 0x36, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x16, 0x0a, - 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, - 0x73, 0x65, 0x72, 0x49, 0x44, 0x12, 0x12, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x03, 0x52, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x1a, 0x4d, 0x0a, 0x0b, 0x41, 0x63, 0x74, - 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x3e, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, - 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x54, 0x69, 0x6d, 0x65, - 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x1a, 0x66, 0x0a, 0x0d, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x3f, 0x0a, 0x05, 0x76, - 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x63, 0x6f, 0x64, - 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, - 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, - 0x32, 0x63, 0x0a, 0x10, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x53, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x12, 0x4f, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x50, 0x72, - 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x16, - 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, - 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1d, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, - 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x52, 0x65, 0x73, 0x70, 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x77, - 0x6f, 0x72, 0x64, 0x5f, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x2a, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x12, + 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x66, + 0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x02, 0x74, 0x6f, 0x22, 0xcd, 0x02, 0x0a, 0x13, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, + 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x47, 0x0a, 0x08, 0x72, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2b, 0x2e, + 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, + 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x1a, 0x36, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x54, 0x69, 0x6d, 0x65, + 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x44, 0x12, 0x12, 0x0a, 0x04, 0x54, 0x69, 0x6d, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x54, 0x69, 0x6d, 0x65, 0x1a, 0x4d, 0x0a, 0x0b, + 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x3e, 0x0a, 0x06, 0x76, + 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x63, 0x6f, + 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, + 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x54, + 0x69, 0x6d, 0x65, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x1a, 0x66, 0x0a, 0x0d, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, + 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x3f, + 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x29, 0x2e, + 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, + 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x41, 0x63, 0x74, + 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, + 0x02, 0x38, 0x01, 0x32, 0x5b, 0x0a, 0x10, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x43, 0x6f, 0x64, 0x65, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x47, 0x0a, 0x16, 0x47, 0x65, 0x74, 0x41, 0x6c, + 0x6c, 0x50, 0x72, 0x6f, 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x12, 0x0e, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x54, 0x69, 0x6d, + 0x65, 0x1a, 0x1d, 0x2e, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x2e, 0x50, 0x72, 0x6f, + 0x6d, 0x6f, 0x41, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, + 0x42, 0x10, 0x5a, 0x0e, 0x2e, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x72, + 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -284,13 +281,12 @@ var file_promo_proto_goTypes = []interface{}{ (*PromoActivationResp_UserTime)(nil), // 2: codeword.PromoActivationResp.UserTime (*PromoActivationResp_Activations)(nil), // 3: codeword.PromoActivationResp.Activations nil, // 4: codeword.PromoActivationResp.ResponseEntry - (*emptypb.Empty)(nil), // 5: google.protobuf.Empty } var file_promo_proto_depIdxs = []int32{ 4, // 0: codeword.PromoActivationResp.response:type_name -> codeword.PromoActivationResp.ResponseEntry 2, // 1: codeword.PromoActivationResp.Activations.values:type_name -> codeword.PromoActivationResp.UserTime 3, // 2: codeword.PromoActivationResp.ResponseEntry.value:type_name -> codeword.PromoActivationResp.Activations - 5, // 3: codeword.PromoCodeService.GetAllPromoActivations:input_type -> google.protobuf.Empty + 0, // 3: codeword.PromoCodeService.GetAllPromoActivations:input_type -> codeword.Time 1, // 4: codeword.PromoCodeService.GetAllPromoActivations:output_type -> codeword.PromoActivationResp 4, // [4:5] is the sub-list for method output_type 3, // [3:4] is the sub-list for method input_type diff --git a/internal/proto/codeword/promo_grpc.pb.go b/internal/proto/codeword/promo_grpc.pb.go index 1fabd5e..9e225c9 100644 --- a/internal/proto/codeword/promo_grpc.pb.go +++ b/internal/proto/codeword/promo_grpc.pb.go @@ -7,7 +7,6 @@ import ( grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" - emptypb "google.golang.org/protobuf/types/known/emptypb" ) // This is a compile-time assertion to ensure that this generated file @@ -19,7 +18,7 @@ const _ = grpc.SupportPackageIsVersion7 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type PromoCodeServiceClient interface { - GetAllPromoActivations(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PromoActivationResp, error) + GetAllPromoActivations(ctx context.Context, in *Time, opts ...grpc.CallOption) (*PromoActivationResp, error) } type promoCodeServiceClient struct { @@ -30,7 +29,7 @@ func NewPromoCodeServiceClient(cc grpc.ClientConnInterface) PromoCodeServiceClie return &promoCodeServiceClient{cc} } -func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PromoActivationResp, error) { +func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in *Time, opts ...grpc.CallOption) (*PromoActivationResp, error) { out := new(PromoActivationResp) err := c.cc.Invoke(ctx, "/codeword.PromoCodeService/GetAllPromoActivations", in, out, opts...) if err != nil { @@ -43,7 +42,7 @@ func (c *promoCodeServiceClient) GetAllPromoActivations(ctx context.Context, in // All implementations must embed UnimplementedPromoCodeServiceServer // for forward compatibility type PromoCodeServiceServer interface { - GetAllPromoActivations(context.Context, *emptypb.Empty) (*PromoActivationResp, error) + GetAllPromoActivations(context.Context, *Time) (*PromoActivationResp, error) mustEmbedUnimplementedPromoCodeServiceServer() } @@ -51,7 +50,7 @@ type PromoCodeServiceServer interface { type UnimplementedPromoCodeServiceServer struct { } -func (UnimplementedPromoCodeServiceServer) GetAllPromoActivations(context.Context, *emptypb.Empty) (*PromoActivationResp, error) { +func (UnimplementedPromoCodeServiceServer) GetAllPromoActivations(context.Context, *Time) (*PromoActivationResp, error) { return nil, status.Errorf(codes.Unimplemented, "method GetAllPromoActivations not implemented") } func (UnimplementedPromoCodeServiceServer) mustEmbedUnimplementedPromoCodeServiceServer() {} @@ -68,7 +67,7 @@ func RegisterPromoCodeServiceServer(s grpc.ServiceRegistrar, srv PromoCodeServic } func _PromoCodeService_GetAllPromoActivations_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(emptypb.Empty) + in := new(Time) if err := dec(in); err != nil { return nil, err } @@ -80,7 +79,7 @@ func _PromoCodeService_GetAllPromoActivations_Handler(srv interface{}, ctx conte FullMethod: "/codeword.PromoCodeService/GetAllPromoActivations", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(PromoCodeServiceServer).GetAllPromoActivations(ctx, req.(*emptypb.Empty)) + return srv.(PromoCodeServiceServer).GetAllPromoActivations(ctx, req.(*Time)) } return interceptor(ctx, in, info, handler) } From 6ca31c1a4386948d5f29a89cc9af128ff04f6c35 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 26 Apr 2024 10:39:04 +0300 Subject: [PATCH 8/9] add check on nil time req --- internal/repository/promocode_stats.go | 33 ++++++++++++++++++-------- internal/services/promocode_service.go | 7 +++--- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index 94467c8..e65c415 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -79,8 +79,9 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string) return promoCodeStats, nil } -func (r *StatsRepository) GetAllPromoActivations(ctx context.Context) (*codeword_rpc.PromoActivationResp, error) { - pipeline := []bson.M{ +func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { + var pipeline []bson.M + pipeline = append(pipeline, []bson.M{ { "$project": bson.M{ "_id": 1, @@ -93,17 +94,29 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context) (*codeword { "$unwind": "$usageArray.v", }, - { - "$group": bson.M{ - "_id": "$_id", - "users": bson.M{"$push": bson.M{ - "UserID": "$usageArray.v.userID", - "Time": "$usageArray.v.time", - }}, + }...) + + if req.To != 0 && req.From != 0 { + pipeline = append(pipeline, bson.M{ + "$match": bson.M{ + "usageArray.v.time": bson.M{ + "$gte": req.From, + "$lte": req.To, + }, }, - }, + }) } + pipeline = append(pipeline, bson.M{ + "$group": bson.M{ + "_id": "$_id", + "users": bson.M{"$push": bson.M{ + "UserID": "$usageArray.v.userID", + "Time": "$usageArray.v.time", + }}, + }, + }) + cursor, err := r.mdb.Aggregate(ctx, pipeline) if err != nil { return nil, err diff --git a/internal/services/promocode_service.go b/internal/services/promocode_service.go index 5633682..fdc8c24 100644 --- a/internal/services/promocode_service.go +++ b/internal/services/promocode_service.go @@ -12,7 +12,6 @@ import ( "fmt" "go.mongodb.org/mongo-driver/bson/primitive" "go.uber.org/zap" - "google.golang.org/protobuf/types/known/emptypb" "time" ) @@ -30,7 +29,7 @@ type PromoCodeRepository interface { type PromoStatsRepository interface { UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error) - GetAllPromoActivations(ctx context.Context) (*codeword_rpc.PromoActivationResp, error) + GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) } type PromoDeps struct { @@ -273,8 +272,8 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq return resp, nil } -func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, _ *emptypb.Empty) (*codeword_rpc.PromoActivationResp, error) { - result, err := s.statsRepo.GetAllPromoActivations(ctx) +func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { + result, err := s.statsRepo.GetAllPromoActivations(ctx, req) if err != nil { s.logger.Error("error getting all promo activations data", zap.Error(err)) return nil, err From e45a21de2eb91d477dd224a548c8d620a6c42541 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 26 Apr 2024 14:54:05 +0300 Subject: [PATCH 9/9] change getting all promo, now search first promo user activation --- internal/repository/promocode_stats.go | 49 ++++++++++++-------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index e65c415..f05f6e7 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -81,38 +81,35 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string) func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { var pipeline []bson.M - pipeline = append(pipeline, []bson.M{ - { - "$project": bson.M{ - "_id": 1, - "usageArray": bson.M{"$objectToArray": "$usageMap"}, - }, + pipeline = append(pipeline, bson.M{ + "$project": bson.M{ + "_id": 1, + "usageArray": bson.M{"$objectToArray": "$usageMap"}, }, - { - "$unwind": "$usageArray", - }, - { - "$unwind": "$usageArray.v", - }, - }...) + }) - if req.To != 0 && req.From != 0 { - pipeline = append(pipeline, bson.M{ - "$match": bson.M{ - "usageArray.v.time": bson.M{ - "$gte": req.From, - "$lte": req.To, - }, - }, - }) - } + pipeline = append(pipeline, bson.M{ + "$unwind": "$usageArray", + }) + + pipeline = append(pipeline, bson.M{ + "$unwind": "$usageArray.v", + }) pipeline = append(pipeline, bson.M{ "$group": bson.M{ - "_id": "$_id", + "_id": "$usageArray.v.userID", + "promoID": bson.M{"$first": "$_id"}, + "Time": bson.M{"$first": "$usageArray.v.time"}, + }, + }) + + pipeline = append(pipeline, bson.M{ + "$group": bson.M{ + "_id": "$promoID", "users": bson.M{"$push": bson.M{ - "UserID": "$usageArray.v.userID", - "Time": "$usageArray.v.time", + "UserID": "$_id", + "Time": "$Time", }}, }, })