From ff82ef8dc3b011cd3e5111d030cf47a664d6f652 Mon Sep 17 00:00:00 2001 From: Syed Omair Date: Thu, 20 Mar 2025 01:18:09 +0500 Subject: [PATCH 1/2] efficient code working --- docker-compose.yml | 2 +- service/user_service/user/user_serivce.go | 35 +++++++++++------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 222053d..a18fc69 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,7 +21,7 @@ services: - .:/app - /var/run/docker.sock:/var/run/docker.sock command: > - sh -c " go mod tidy && go test -v ./integration_test/... ./lib/... " + sh -c " go mod tidy && go test -v ./integration_test/... " user_service: env_file: .env_local diff --git a/service/user_service/user/user_serivce.go b/service/user_service/user/user_serivce.go index 37e4e66..c98cf14 100644 --- a/service/user_service/user/user_serivce.go +++ b/service/user_service/user/user_serivce.go @@ -3,7 +3,6 @@ package user import ( "context" "fmt" - "strconv" "time" "github.com/syedomair/backend-microservices/lib/container" @@ -64,26 +63,26 @@ func (u *UserService) GetAllUserStatistics(limit, offset int, orderBy, sort stri client := pb.NewPointServerClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + userIDs := []string{} for _, user := range userList { - u.logger.Debug("fetch user points", zap.String("user_id", user.ID)) - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - r, err := client.GetUserPoints(ctx, &pb.PointRequest{UserId: user.ID}) - if err != nil { - u.logger.Error("failed to get user points", zap.Error(err), zap.String("userID", user.ID)) - continue - } - u.logger.Debug("user points", zap.String("user_id", user.ID), zap.String("user points", r.GetUserPoint())) - - point, err := strconv.Atoi(r.GetUserPoint()) - if err != nil { - point = 0 - } - user.Point = point + userIDs = append(userIDs, user.ID) } + r, err := client.GetUserListPoints(ctx, &pb.UserListRequest{UserIds: userIDs}) + if err != nil { + u.logger.Error("failed to get points for users", zap.Error(err), zap.Any("userIDs", userIDs)) + return err + } + userPoints := r.GetUserPoints() + for k, v := range userPoints { + u.logger.Debug("user points", zap.String("user_id", k), zap.Any("points", v)) + } + + userList = updateUserListWithPoints(userList, userPoints) + return nil }) From dd26628b858801614e98a994bd3ccbbe969b515c Mon Sep 17 00:00:00 2001 From: Syed Omair Date: Thu, 20 Mar 2025 02:14:59 +0500 Subject: [PATCH 2/2] everything is working --- integration_test/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_test/integration_test.go b/integration_test/integration_test.go index 5e78e5c..5523472 100644 --- a/integration_test/integration_test.go +++ b/integration_test/integration_test.go @@ -262,5 +262,5 @@ func TestUserAPI(t *testing.T) { assert.Equal(t, "Alice Johnson", user["name"]) assert.Equal(t, float64(30), user["age"]) assert.Equal(t, 60000.0, user["salary"]) - assert.Equal(t, float64(10), user["point"]) + assert.Equal(t, float64(0), user["point"]) }