From 0c4614e2b32c2318e618a0fa10ecf744e146e4a1 Mon Sep 17 00:00:00 2001 From: Chris Tremblay Date: Mon, 8 Jan 2024 14:40:46 -0500 Subject: [PATCH] updates to persist prompts --- petstore/petstoreassistant/pom.xml | 2 +- .../PetStoreAssistantBot.java | 270 +++++++++--------- .../model/AzurePetStoreSessionInfo.java | 19 ++ .../petstoreassistant/model/Prompt.java | 39 +++ .../service/AzureAIServices.java | 4 +- .../service/AzurePetStore.java | 7 +- .../petstoreassistant/service/CosmosDB.java | 34 ++- .../petstoreassistant/service/ICosmosDB.java | 3 +- 8 files changed, 228 insertions(+), 150 deletions(-) create mode 100644 petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/Prompt.java diff --git a/petstore/petstoreassistant/pom.xml b/petstore/petstoreassistant/pom.xml index d863e1b2..1477c857 100644 --- a/petstore/petstoreassistant/pom.xml +++ b/petstore/petstoreassistant/pom.xml @@ -82,7 +82,7 @@ org.apache.logging.log4j log4j-core - 2.13.2 + 2.22.1 diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java index ff3d9576..f0f313c3 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java @@ -15,9 +15,11 @@ import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo; import com.chtrembl.petstoreassistant.model.DPResponse; +import com.chtrembl.petstoreassistant.model.Prompt; import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification; import com.chtrembl.petstoreassistant.service.IAzureAIServices; import com.chtrembl.petstoreassistant.service.IAzurePetStore; +import com.chtrembl.petstoreassistant.service.ICosmosDB; import com.chtrembl.petstoreassistant.utility.PetStoreAssistantUtilities; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; @@ -53,15 +55,18 @@ public class PetStoreAssistantBot extends ActivityHandler { @Autowired private IAzurePetStore azurePetStore; + @Autowired + private ICosmosDB cosmosDB; + private String WELCOME_MESSAGE = "Hello and welcome to the Azure Pet Store, you can ask me questions about our products, your shopping cart and your order, you can also ask me for information about pet animals. How can I help you?"; private UserState userState; Cache cache = Caffeine.newBuilder() - .expireAfterWrite(30, TimeUnit.MINUTES) - .maximumSize(10000) - .build(); - + .expireAfterWrite(30, TimeUnit.MINUTES) + .maximumSize(10000) + .build(); + public PetStoreAssistantBot(UserState withUserState) { this.userState = withUserState; } @@ -76,30 +81,26 @@ public CompletableFuture onTurn(TurnContext turnContext) { @Override protected CompletableFuture onMessageActivity(TurnContext turnContext) { String text = turnContext.getActivity().getText().toLowerCase().trim(); - - if(isErroneousRequest(text)) - { + + if (isErroneousRequest(text)) { return null; } AzurePetStoreSessionInfo azurePetStoreSessionInfo = configureSession(turnContext, text); - if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) - { + if (azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) { // get the text without the session id and csrf token text = azurePetStoreSessionInfo.getNewText(); } - - //the client browser initialized - if(text.equals("...")) - { + + // the client browser initialized + if (text.equals("...")) { return turnContext.sendActivity( - MessageFactory.text(WELCOME_MESSAGE)).thenApply(sendResult -> null); + MessageFactory.text(WELCOME_MESSAGE)).thenApply(sendResult -> null); } CompletableFuture debug = getDebug(turnContext, text, azurePetStoreSessionInfo); - if(debug != null) - { + if (debug != null) { return debug; } @@ -118,28 +119,22 @@ protected CompletableFuture onMessageActivity(TurnContext turnContext) { dpResponse = this.azurePetStore.updateCart(azurePetStoreSessionInfo, dpResponse.getProducts().get(0).getProductId()); } - } - else - { - dpResponse.setDpResponseText("update shopping cart request without session... text: "+text); + } else { + dpResponse.setDpResponseText("update shopping cart request without session... text: " + text); } break; case VIEW_SHOPPING_CART: if (azurePetStoreSessionInfo != null) { dpResponse = this.azurePetStore.viewCart(azurePetStoreSessionInfo); - } - else - { - dpResponse.setDpResponseText("view shopping cart request without session... text: "+text); + } else { + dpResponse.setDpResponseText("view shopping cart request without session... text: " + text); } break; case PLACE_ORDER: if (azurePetStoreSessionInfo != null) { dpResponse = this.azurePetStore.completeCart(azurePetStoreSessionInfo); - } - else - { - dpResponse.setDpResponseText("place order request without session... text: "+text); + } else { + dpResponse.setDpResponseText("place order request without session... text: " + text); } break; case SEARCH_FOR_DOG_FOOD: @@ -151,153 +146,162 @@ protected CompletableFuture onMessageActivity(TurnContext turnContext) { case MORE_PRODUCT_INFORMATION: case SEARCH_FOR_PRODUCTS: if (azurePetStoreSessionInfo == null) { - dpResponse.setDpResponseText("search for products request without session... text: "+text); - } - else - { + dpResponse.setDpResponseText("search for products request without session... text: " + text); + } else { dpResponse = this.azureOpenAI.search(text, dpResponse.getClassification()); } break; case SOMETHING_ELSE: if (azurePetStoreSessionInfo == null) { - dpResponse.setDpResponseText("chatgpt request without session... text: "+text); - } - else - { - if(!text.isEmpty()) - { + dpResponse.setDpResponseText("chatgpt request without session... text: " + text); + } else { + if (!text.isEmpty()) { dpResponse = this.azureOpenAI.completion(text, dpResponse.getClassification()); - } - else - { - dpResponse.setDpResponseText("chatgpt called without a search query... text: "+text); + } else { + dpResponse.setDpResponseText("chatgpt called without a search query... text: " + text); } } break; } - if((dpResponse.getDpResponseText() == null)) - { + if ((dpResponse.getDpResponseText() == null)) { String responseText = "I am not sure how to handle that."; - if((azurePetStoreSessionInfo == null)) - { + if ((azurePetStoreSessionInfo == null)) { responseText += " It may be because I did not have your session information."; } dpResponse.setDpResponseText(responseText); } - if(dpResponse.isImageContentCard()) - { + if (azurePetStoreSessionInfo != null) { + azurePetStoreSessionInfo + .addPrompt(new Prompt(dpResponse.getClassification(), text, dpResponse.getDpResponseText())); + this.cache.put(azurePetStoreSessionInfo.getId(), azurePetStoreSessionInfo); + + this.cosmosDB.storePrompt(azurePetStoreSessionInfo); + } + + if (dpResponse.isImageContentCard()) { return PetStoreAssistantUtilities.getImageCard(turnContext, dpResponse); } + return turnContext.sendActivity( MessageFactory.text(dpResponse.getDpResponseText())).thenApply(sendResult -> null); - } + } - // this method only gets invoked once, regardless of browser/user, state isnt working right for some reason (DP related, not in issue with emulator) + // this method only gets invoked once, regardless of browser/user, state isnt + // working right for some reason (DP related, not in issue with emulator) @Override protected CompletableFuture onMembersAdded( List membersAdded, TurnContext turnContext) { - - // return membersAdded.stream() - // .filter( - // member -> !StringUtils - // .equals(member.getId(), turnContext.getActivity().getRecipient().getId())) - // .map(channel -> turnContext - // .sendActivity( - // MessageFactory.text(this.WELCOME_MESSAGE + id))) - // .collect(CompletableFutures.toFutureList()).thenApply(resourceResponses -> null); - - return turnContext.sendActivity( + // return membersAdded.stream() + // .filter( + // member -> !StringUtils + // .equals(member.getId(), turnContext.getActivity().getRecipient().getId())) + // .map(channel -> turnContext + // .sendActivity( + // MessageFactory.text(this.WELCOME_MESSAGE + id))) + // .collect(CompletableFutures.toFutureList()).thenApply(resourceResponses -> + // null); + + return turnContext.sendActivity( MessageFactory.text("")).thenApply(sendResult -> null); + } + + private boolean isErroneousRequest(String text) { + // some times for unknown reasons, activity occurs with text: page_metadata, + // let's ignore these + if (text.contains("page_metadata") || text.isEmpty()) { + return true; } + return false; + } - private boolean isErroneousRequest(String text) { - //some times for unknown reasons, activity occurs with text: page_metadata, let's ignore these - if(text.contains("page_metadata") || text.isEmpty()) - { - return true; - } - return false; + private AzurePetStoreSessionInfo configureSession(TurnContext turnContext, String text) { + // bot turn state and recipient not working with SoulMachines/DP (works in + // emulator) however this id appears to be unique per browser tab when running + // with SoulMachines/DP + // format is XKQtkRt4hDBdwzwP2bwhs-us|0000014, so we will hack off the dynamic + // ending piece, If you want to test locally this id will be different every + // single time so you will want to change to + // turnContext.getActivity().getrecipient().getId() when running locally + String id = turnContext.getActivity().getId().trim(); + if (id.contains("-")) { + id = id.substring(0, id.indexOf("-")); } - private AzurePetStoreSessionInfo configureSession(TurnContext turnContext, String text) { - // bot turn state and recipient not working with SoulMachines/DP (works in emulator) however this id appears to be unique per browser tab. - // format is XKQtkRt4hDBdwzwP2bwhs-us|0000014, so we will hack off the dynamic ending piece - String id = turnContext.getActivity().getId().trim(); - if(id.contains("-")) - { - id = id.substring(0, id.indexOf("-")); - } - - AzurePetStoreSessionInfo azurePetStoreSessionInfo = this.cache.getIfPresent(id); - - // strip out session id and csrf token if one was passed in - AzurePetStoreSessionInfo incomingAzurePetStoreSessionInfo = PetStoreAssistantUtilities - .getAzurePetStoreSessionInfo(text); - if (incomingAzurePetStoreSessionInfo != null) { - text = incomingAzurePetStoreSessionInfo.getNewText(); - //turnContext.getActivity().getId() is unique per browser over the broken recipient for some reason - this.cache.put(id, incomingAzurePetStoreSessionInfo); - azurePetStoreSessionInfo = incomingAzurePetStoreSessionInfo; - azurePetStoreSessionInfo.setId(id); - } - else if(azurePetStoreSessionInfo != null) - { - azurePetStoreSessionInfo.setNewText(text); - } + AzurePetStoreSessionInfo azurePetStoreSessionInfo = this.cache.getIfPresent(id); + List existingPrompts = null; + if (azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getPrompts() != null) { + existingPrompts = azurePetStoreSessionInfo.getPrompts(); + } - return azurePetStoreSessionInfo; + // strip out session id and csrf token if one was passed in + AzurePetStoreSessionInfo incomingAzurePetStoreSessionInfo = PetStoreAssistantUtilities + .getAzurePetStoreSessionInfo(text); + if (incomingAzurePetStoreSessionInfo != null) { + text = incomingAzurePetStoreSessionInfo.getNewText(); + // turnContext.getActivity().getId() is unique per browser over the broken + // recipient for some reason + this.cache.put(id, incomingAzurePetStoreSessionInfo); + azurePetStoreSessionInfo = incomingAzurePetStoreSessionInfo; + azurePetStoreSessionInfo.setId(id); + } else if (azurePetStoreSessionInfo != null) { + azurePetStoreSessionInfo.setNewText(text); } - private CompletableFuture getDebug(TurnContext turnContext, String text, AzurePetStoreSessionInfo azurePetStoreSessionInfo) { - if (text.equals("debug")) - { - if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) - { + azurePetStoreSessionInfo.setPrompts(existingPrompts); + + return azurePetStoreSessionInfo; + } + + private CompletableFuture getDebug(TurnContext turnContext, String text, + AzurePetStoreSessionInfo azurePetStoreSessionInfo) { + if (text.equals("debug")) { + if (azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) { return turnContext.sendActivity( - MessageFactory.text("id:"+azurePetStoreSessionInfo.getId()+", cache size: "+cache.estimatedSize())).thenApply(sendResult -> null); - } - else{ - return turnContext.sendActivity( - MessageFactory.text("azurePetStoreSessionInfo was null, cache size: "+cache.estimatedSize())).thenApply(sendResult -> null); - } + MessageFactory.text( + "id:" + azurePetStoreSessionInfo.getId() + ", cache size: " + cache.estimatedSize())) + .thenApply(sendResult -> null); + } else { + return turnContext.sendActivity( + MessageFactory.text("azurePetStoreSessionInfo was null, cache size: " + cache.estimatedSize())) + .thenApply(sendResult -> null); } - if (text.equals("button card")) { - if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) - { + } + if (text.equals("button card")) { + if (azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) { text = azurePetStoreSessionInfo.getNewText(); - } - String jsonString = "{\"type\":\"buttonWithImage\",\"id\":\"buttonWithImage\",\"data\":{\"title\":\"Soul Machines\",\"imageUrl\":\"https://www.soulmachines.com/wp-content/uploads/cropped-sm-favicon-180x180.png\",\"description\":\"Soul Machines is the leader in astonishing AGI\",\"imageAltText\":\"some text\",\"buttonText\":\"push me\"}}"; + } + String jsonString = "{\"type\":\"buttonWithImage\",\"id\":\"buttonWithImage\",\"data\":{\"title\":\"Soul Machines\",\"imageUrl\":\"https://www.soulmachines.com/wp-content/uploads/cropped-sm-favicon-180x180.png\",\"description\":\"Soul Machines is the leader in astonishing AGI\",\"imageAltText\":\"some text\",\"buttonText\":\"push me\"}}"; - Attachment attachment = new Attachment(); - attachment.setContentType("application/json"); + Attachment attachment = new Attachment(); + attachment.setContentType("application/json"); - attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); - attachment.setName("public-content-card"); + attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); + attachment.setName("public-content-card"); - return turnContext.sendActivity( - MessageFactory.attachment(attachment, "I have something nice to show @showcards(content-card) you.")) - .thenApply(sendResult -> null); - } + return turnContext.sendActivity( + MessageFactory.attachment(attachment, + "I have something nice to show @showcards(content-card) you.")) + .thenApply(sendResult -> null); + } - if (text.equals("image card")) - { - String jsonString = "{\"type\":\"image\",\"id\":\"image-ball\",\"data\":{\"url\": \"https://raw.githubusercontent.com/chtrembl/staticcontent/master/dog-toys/ball.jpg?raw=true\",\"alt\": \"This is a pretty ball\",\"caption\": \"ball blah blah blah\"}}"; - Attachment attachment = new Attachment(); - attachment.setContentType("application/json"); - - attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); - attachment.setName("public-image-ball"); - - return turnContext.sendActivity( - MessageFactory.attachment(attachment, "I have something nice to show @showcards(image-ball) you.")) - .thenApply(sendResult -> null); - } - return null; + if (text.equals("image card")) { + String jsonString = "{\"type\":\"image\",\"id\":\"image-ball\",\"data\":{\"url\": \"https://raw.githubusercontent.com/chtrembl/staticcontent/master/dog-toys/ball.jpg?raw=true\",\"alt\": \"This is a pretty ball\",\"caption\": \"ball blah blah blah\"}}"; + Attachment attachment = new Attachment(); + attachment.setContentType("application/json"); + + attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); + attachment.setName("public-image-ball"); + + return turnContext.sendActivity( + MessageFactory.attachment(attachment, "I have something nice to show @showcards(image-ball) you.")) + .thenApply(sendResult -> null); } - } + return null; + } +} diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/AzurePetStoreSessionInfo.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/AzurePetStoreSessionInfo.java index 286d4aa6..d737e09a 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/AzurePetStoreSessionInfo.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/AzurePetStoreSessionInfo.java @@ -1,13 +1,19 @@ package com.chtrembl.petstoreassistant.model; import java.io.Serializable; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; public class AzurePetStoreSessionInfo implements Serializable{ private String sessionID = null; private String csrfToken = null; + @JsonIgnore private String newText = null; private String id = null; + private List prompts = null; + public AzurePetStoreSessionInfo(String sessionID, String csrfToken, String newText) { super(); this.sessionID = sessionID.trim().toUpperCase(); //JSESSION needs to be UPPER CASE @@ -32,4 +38,17 @@ public String getId() { public void setId(String id) { this.id = id; } + public List getPrompts() { + return prompts; + } + public void setPrompts(List prompts) { + this.prompts = prompts; + } + public void addPrompt(Prompt prompt) { + if (this.prompts == null) { + this.prompts = new java.util.ArrayList(); + } + this.prompts.add(prompt); + } + } diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/Prompt.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/Prompt.java new file mode 100644 index 00000000..e305befe --- /dev/null +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/model/Prompt.java @@ -0,0 +1,39 @@ +package com.chtrembl.petstoreassistant.model; + +import java.io.Serializable; + +import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification; + +public class Prompt implements Serializable{ + private Classification classification = null; + private String question = null; + private String answer = null; + + public Prompt() { + super(); + } + public Prompt(Classification classification, String question, String answer) { + super(); + this.classification = classification; + this.question = question; + this.answer = answer; + } + public Classification getClassification() { + return classification; + } + public void setClassification(Classification classification) { + this.classification = classification; + } + public String getQuestion() { + return question; + } + public void setQuestion(String question) { + this.question = question; + } + public String getAnswer() { + return answer; + } + public void setAnswer(String answer) { + this.answer = answer; + } +} diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java index 8f31ab8d..fed0ac52 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzureAIServices.java @@ -175,7 +175,7 @@ public DPResponse search(String text, Classification classification) { DPResponse dpResponse = new DPResponse(); dpResponse.setClassification(classification); - String dpResponseText = "I found some information on our products. We have a "; + String dpResponseText = "We have a "; String filter = ""; @@ -265,7 +265,7 @@ public DPResponse search(String text, Classification classification) { if(products.size() == 1 || classification.equals(Classification.MORE_PRODUCT_INFORMATION)) { dpResponse.setImageContentCard(true); - dpResponseText = "Check out this product of ours, the " + products.get(0).getName(); + dpResponseText = "Check out this product, the " + products.get(0).getName(); dpResponse.setDpResponseText(dpResponseText); } diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzurePetStore.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzurePetStore.java index c109540a..dd644970 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzurePetStore.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/AzurePetStore.java @@ -7,6 +7,7 @@ import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo; import com.chtrembl.petstoreassistant.model.DPResponse; +import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -30,7 +31,7 @@ public class AzurePetStore implements IAzurePetStore { @Override public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo, String productId) { DPResponse dpResponse = new DPResponse(); - + try { Request request = new Request.Builder() .url(this.UPDATE_CART_URL + "?csrf=" + azurePetStoreSessionInfo.getCsrfToken() @@ -47,7 +48,7 @@ public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo, + azurePetStoreSessionInfo.getCsrfToken()); dpResponse.setDpResponseText("I just added the " - + this.cosmosDB.getCachedProducts().get(productId).getName() + + this.cosmosDB.getProducts().get(productId).getName() + " to your cart."); dpResponse.setUpdateCart(true); @@ -55,7 +56,7 @@ public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo, LOGGER.error("Error updating cart with product id: " + productId + " for session id: " + azurePetStoreSessionInfo.getSessionID() + " " + e.getMessage()); dpResponse.setDpResponseText("I'm sorry, I wasn't able to add the " - + this.cosmosDB.getCachedProducts().get(productId).getName() + + this.cosmosDB.getProducts().get(productId).getName() + " to your cart. " + azurePetStoreSessionInfo.getSessionID() + "|" + azurePetStoreSessionInfo.getCsrfToken()); } diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/CosmosDB.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/CosmosDB.java index 551db641..1b68e5cb 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/CosmosDB.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/CosmosDB.java @@ -13,6 +13,7 @@ import com.azure.cosmos.CosmosClientBuilder; import com.azure.cosmos.CosmosContainer; import com.azure.cosmos.util.CosmosPagedIterable; +import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo; import com.chtrembl.petstoreassistant.model.Product; @Service @@ -26,7 +27,8 @@ public class CosmosDB implements ICosmosDB { private static final String DATABASE_ID = "E-Commerce"; - private static final String CONTAINER_ID = "ProductsV2"; + private static final String PRODUCTS_CONTAINER_ID = "ProductsV2"; + private static final String PROMPTS_CONTAINER_ID = "Prompts"; private CosmosClient client = null; @@ -39,14 +41,13 @@ public void initialize() throws Exception { .endpoint(ENDPOINT) .key(this.cosmosKey) .buildClient(); - this.products = this.getProducts(); + this.cacheProducts(); } - @Override - public HashMap getProducts() { - HashMap products = new HashMap (); + private void cacheProducts() { + this.products = new HashMap (); - CosmosContainer container = client.getDatabase(DATABASE_ID).getContainer(CONTAINER_ID); + CosmosContainer container = client.getDatabase(DATABASE_ID).getContainer(this.PRODUCTS_CONTAINER_ID); String query = "SELECT * FROM ProductsV2"; @@ -55,12 +56,25 @@ public HashMap getProducts() { products.put(product.getProductId(), product); } - LOGGER.info("Retrieved " + products.size() + " products from CosmosDB"); - - return products; + LOGGER.info("Cached " + products.size() + " products from CosmosDB"); } - public HashMap getCachedProducts() { + public HashMap getProducts() { return this.products; } + + public void storePrompt(AzurePetStoreSessionInfo azurePetStoreSessionInfo) { + CosmosContainer container = client.getDatabase(DATABASE_ID).getContainer(this.PROMPTS_CONTAINER_ID); + + try + { + container.upsertItem(azurePetStoreSessionInfo); + LOGGER.info("Upsert prompt record in CosmosDB id: " + azurePetStoreSessionInfo.getId()); + + } + catch (Exception e) + { + LOGGER.error("Error upserting prompt in CosmosDB " + e.getMessage()); + } + } } diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/ICosmosDB.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/ICosmosDB.java index c52fe6cd..8bcb3ebb 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/ICosmosDB.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/service/ICosmosDB.java @@ -2,9 +2,10 @@ import java.util.HashMap; +import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo; import com.chtrembl.petstoreassistant.model.Product; public interface ICosmosDB { public HashMap getProducts(); - public HashMap getCachedProducts(); + public void storePrompt(AzurePetStoreSessionInfo azurePetStoreSessionInfo); }