From 9634693f37397344f61c621e8e4d75d0bdc56015 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Tue, 26 Jun 2018 14:01:05 -0700 Subject: [PATCH] recommendationservice: exclude input ids Signed-off-by: Ahmet Alp Balkan --- src/recommendationservice/recommendation_server.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/recommendationservice/recommendation_server.py b/src/recommendationservice/recommendation_server.py index 1ea14f5..b75c3a0 100644 --- a/src/recommendationservice/recommendation_server.py +++ b/src/recommendationservice/recommendation_server.py @@ -12,13 +12,17 @@ class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer): # fetch list of products from product catalog stub cat_response = stub.ListProducts(demo_pb2.Empty()) - num_prodcuts = len(cat_response.products) - num_return = min(max_responses, num_prodcuts) + + product_ids = [x.id for x in cat_response.products] + filtered_products = list(set(product_ids)-set(request.product_ids)) + + num_products = len(filtered_products) + num_return = min(max_responses, num_products) # sample list of indicies to return - indices = random.sample(range(num_prodcuts), num_return) + indices = random.sample(range(num_products), num_return) # fetch product ids from indices - prod_list = [cat_response.products[i].id for i in indices] + prod_list = [filtered_products[i] for i in indices] print("handling request: {}".format(prod_list)) # build and return response