Merge pull request #7 from NipunaMarcus/master

Add recommendation service impl
This commit is contained in:
Nipuna Marcus 2019-11-16 19:43:03 +05:30 committed by GitHub
commit 7ed17f7e3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 951 additions and 89 deletions

View file

@ -1,5 +1,5 @@
[project]
org-name= "recommendationservice"
version= "1.0"
version= "0.1.0"
[dependencies]

View file

@ -1,13 +1,78 @@
import ballerina/grpc;
import ballerina/log;
import ballerina/kubernetes;
listener grpc:Listener ep = new (9090);
// Product catalog client.
ProductCatalogServiceBlockingClient catalogClient = new ("http://productcatalogservice:3550");
final Empty req = {};
service RecommendationService on ep {
@kubernetes:Service {
serviceType: "ClusterIP",
name: "recommendationservice"
}
service RecommendationService on new grpc:Listener(8080) {
resource function ListRecommendations(grpc:Caller caller, ListRecommendationsRequest value) {
// Implementation goes here.
// Fetch list of products from product catalog stub
var products = catalogClient->ListProducts(req);
if (products is grpc:Error) {
log:printError("Error when retrieving products", products);
// You should return a ListRecommendationsResponse
// Return a fixed set of recommendations on error.
ListRecommendationsRequest response = {
user_id: value.user_id,
product_ids: ["9SIQT8TOJO", "6E92ZMYYFZ", "LS4PSXUNUM"]
};
var e = caller->send(response);
if (e is error) {
log:printError("Error when sending recommendations", e);
}
e = caller->complete();
if (e is error) {
log:printError("Error when sending recommendations", e);
}
} else {
// Get the ListProductResponse from the union typed value.
ListProductsResponse listProductResponse = products[0];
Product[] productList = listProductResponse.products;
// Extract product id from the product list.
string[] productIds = [];
foreach Product product in productList {
productIds.push(product.id);
}
// Filter products which already available in the request.
string[] filteredProducts = [];
foreach string productId in productIds {
boolean isExist = false;
foreach string availableId in value.product_ids {
if (productId == availableId) {
isExist = true;
}
}
if (!isExist) {
filteredProducts.push(productId);
}
}
// Send the list of recommentations
ListRecommendationsRequest response = {
user_id: value.user_id,
product_ids: filteredProducts.reverse()
};
var e = caller->send(response);
if (e is error) {
log:printError("Error when sending recommendations", e);
}
e = caller->complete();
if (e is error) {
log:printError("Error when sending recommendations", e);
}
}
}
}

File diff suppressed because one or more lines are too long

View file

@ -1,83 +0,0 @@
import ballerina/grpc;
public type RecommendationServiceBlockingClient client object {
*grpc:AbstractClientEndpoint;
private grpc:Client grpcClient;
public function __init(string url, grpc:ClientConfiguration? config = ()) {
// initialize client endpoint.
grpc:Client c = new(url, config);
grpc:Error? result = c.initStub(self, "blocking", ROOT_DESCRIPTOR, getDescriptorMap());
if (result is grpc:Error) {
error err = result;
panic err;
} else {
self.grpcClient = c;
}
}
public remote function ListRecommendations(ListRecommendationsRequest req, grpc:Headers? headers = ()) returns ([ListRecommendationsResponse, grpc:Headers]|grpc:Error) {
var payload = check self.grpcClient->blockingExecute("hipstershop.RecommendationService/ListRecommendations", req, headers);
grpc:Headers resHeaders = new;
anydata result = ();
[result, resHeaders] = payload;
var value = typedesc<ListRecommendationsResponse>.constructFrom(result);
if (value is ListRecommendationsResponse) {
return [value, resHeaders];
} else {
return grpc:prepareError(grpc:INTERNAL_ERROR, "Error while constructing the message", value);
}
}
};
public type RecommendationServiceClient client object {
*grpc:AbstractClientEndpoint;
private grpc:Client grpcClient;
public function __init(string url, grpc:ClientConfiguration? config = ()) {
// initialize client endpoint.
grpc:Client c = new(url, config);
grpc:Error? result = c.initStub(self, "non-blocking", ROOT_DESCRIPTOR, getDescriptorMap());
if (result is grpc:Error) {
error err = result;
panic err;
} else {
self.grpcClient = c;
}
}
public remote function ListRecommendations(ListRecommendationsRequest req, service msgListener, grpc:Headers? headers = ()) returns (grpc:Error?) {
return self.grpcClient->nonBlockingExecute("hipstershop.RecommendationService/ListRecommendations", req, msgListener, headers);
}
};
public type ListRecommendationsRequest record {|
string user_id;
string[] product_ids;
|};
public type ListRecommendationsResponse record {|
string[] product_ids;
|};
const string ROOT_DESCRIPTOR = "0A1B7265636F6D6D656E646174696F6E736572766963652E70726F746F120B6869707374657273686F7022560A1A4C6973745265636F6D6D656E646174696F6E735265717565737412170A07757365725F69641801200128095206757365724964121F0A0B70726F647563745F696473180220032809520A70726F64756374496473223E0A1B4C6973745265636F6D6D656E646174696F6E73526573706F6E7365121F0A0B70726F647563745F696473180120032809520A70726F647563744964733283010A155265636F6D6D656E646174696F6E53657276696365126A0A134C6973745265636F6D6D656E646174696F6E7312272E6869707374657273686F702E4C6973745265636F6D6D656E646174696F6E73526571756573741A282E6869707374657273686F702E4C6973745265636F6D6D656E646174696F6E73526573706F6E73652200620670726F746F33";
function getDescriptorMap() returns map<string> {
return {
"recommendationservice.proto":"0A1B7265636F6D6D656E646174696F6E736572766963652E70726F746F120B6869707374657273686F7022560A1A4C6973745265636F6D6D656E646174696F6E735265717565737412170A07757365725F69641801200128095206757365724964121F0A0B70726F647563745F696473180220032809520A70726F64756374496473223E0A1B4C6973745265636F6D6D656E646174696F6E73526573706F6E7365121F0A0B70726F647563745F696473180120032809520A70726F647563744964733283010A155265636F6D6D656E646174696F6E53657276696365126A0A134C6973745265636F6D6D656E646174696F6E7312272E6869707374657273686F702E4C6973745265636F6D6D656E646174696F6E73526571756573741A282E6869707374657273686F702E4C6973745265636F6D6D656E646174696F6E73526573706F6E73652200620670726F746F33"
};
}