From 5413a6e6655d6a37683b70a38a49ce5c8fec08fe Mon Sep 17 00:00:00 2001 From: Devin Gaffney Date: Tue, 27 Aug 2024 05:58:52 -0700 Subject: [PATCH] CV2-5082 add logic to suppress a callback event to check-api when explicitly requested (#440) * CV2-5082 add logic to suppress a callback event to check-api when explicitly requested * Update app/main/controller/presto_controller.py Co-authored-by: Skye Bender-deMoll <122867176+skyemeedan@users.noreply.github.com> * Update app/main/controller/presto_controller.py Co-authored-by: Skye Bender-deMoll <122867176+skyemeedan@users.noreply.github.com> * Small tweak --------- Co-authored-by: Skye Bender-deMoll <122867176+skyemeedan@users.noreply.github.com> --- app/main/controller/presto_controller.py | 26 +++++++++++-------- .../controller/similarity_async_controller.py | 1 + 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/main/controller/presto_controller.py b/app/main/controller/presto_controller.py index 1a2980ad..eeaa0800 100644 --- a/app/main/controller/presto_controller.py +++ b/app/main/controller/presto_controller.py @@ -30,17 +30,21 @@ def post(self, action, model_type): if action == "add_item": app.logger.info(f"Data looks like {data}") result = similarity.callback_add_item(data.get("body"), model_type) - if data.get("body", {}).get("raw", {}).get("final_task") == "search": - result = similarity.callback_search_item(data.get("body"), model_type) - if result: - result["is_search_result_callback"] = True - callback_url = data.get("body", {}).get("raw", {}).get("callback_url", app.config['CHECK_API_HOST']) or app.config['CHECK_API_HOST'] - if result and data.get("body", {}).get("raw", {}).get("requires_callback"): - app.logger.info(f"Sending callback to {callback_url} for {action} for model of {model_type} with body of {result}") - Webhook.return_webhook(callback_url, action, model_type, result) - output = {"action": action, "model_type": model_type, "data": result} - app.logger.info(f"PrestoResource value is {output}") - return_value = {"action": action, "model_type": model_type, "data": result} + if data.get("body", {}).get("raw", {}).get("suppress_response"): + # requested not to reply to caller with similarity response, so suppress it + return_value = {"action": action, "model_type": model_type, "data": result} + else: + if data.get("body", {}).get("raw", {}).get("final_task") == "search": + # compute a set of items that are similar to the just-stored item and respond to caller with them + result = similarity.callback_search_item(data.get("body"), model_type) + if result: + result["is_search_result_callback"] = True + callback_url = data.get("body", {}).get("raw", {}).get("callback_url", app.config['CHECK_API_HOST']) or app.config['CHECK_API_HOST'] + if result and data.get("body", {}).get("raw", {}).get("requires_callback"): + app.logger.info(f"Sending callback to {callback_url} for {action} for model of {model_type} with body of {result}") + Webhook.return_webhook(callback_url, action, model_type, result) + return_value = {"action": action, "model_type": model_type, "data": result} + app.logger.info(f"PrestoResource value is {return_value}") r = redis_client.get_client() r.lpush(f"{model_type}_{item_id}", json.dumps(data)) r.expire(f"{model_type}_{item_id}", 60*60*24) diff --git a/app/main/controller/similarity_async_controller.py b/app/main/controller/similarity_async_controller.py index b3c422dd..c9f21dd7 100644 --- a/app/main/controller/similarity_async_controller.py +++ b/app/main/controller/similarity_async_controller.py @@ -33,6 +33,7 @@ def post(self, similarity_type): else: package = similarity.get_body_for_media_document(args, 'query') #Default to true for this endpoint instead of false in most other cases + package["suppress_response"] = args.get("suppress_response", False) package["requires_callback"] = args.get("requires_callback", True) response, waiting_for_callback = similarity.async_get_similar_items(package, similarity_type) if not waiting_for_callback: