From 87ff62336436c73c3ec8f4cb69e618ef84ed8029 Mon Sep 17 00:00:00 2001 From: khaledk2 Date: Tue, 26 Nov 2024 00:21:46 +0000 Subject: [PATCH] add endpoints to retuen images inside projects and screens, and no of images inside datasets and plates --- .../api/v1/resources/resource_analyser.py | 116 ++++++++++++++++++ omero_search_engine/api/v1/resources/urls.py | 21 ++++ 2 files changed, 137 insertions(+) diff --git a/omero_search_engine/api/v1/resources/resource_analyser.py b/omero_search_engine/api/v1/resources/resource_analyser.py index 7eed09b..e3b250e 100644 --- a/omero_search_engine/api/v1/resources/resource_analyser.py +++ b/omero_search_engine/api/v1/resources/resource_analyser.py @@ -953,3 +953,119 @@ def process_container_query( }, } } + + +# Return sub container using a container attribute +# for example get the no of sub containers e.g. datasets names, +# inside a container, e.g. project using name. +container_returned_sub_container_template = Template( + """ + { + "values":{ + "filter":{"terms":{"$container_attribute_name":["$container_attribute_value"]}}, + "aggs":{ + "required_values":{ + "cardinality":{ + "field":"$returned_sub_container", + "precision_threshold":4000 + } + }, + "uniquesTerms":{ + "terms":{ + "field":"$returned_sub_container", + "size":10000 + } + } + } + } + } +""" +) + + +containers_no_images = Template( + """{ + "aggs":{ + "required_values":{ + "cardinality":{ + "field":"$conatins_name.keyvalue", + "precision_threshold":4000 + } + }, + "uniquesTerms":{ + "terms":{ + "field":"$conatins_name.keyvalue", + "size":10000 + } + } + } +} +""" +) + + +def get_containers_no_images( + contianer, container_name, sub_container=None, query_details=None +): + containers_subcontainers = {"project": "dataset", "screen": "plate"} + if not sub_container: + if not containers_subcontainers.get(contianer.lower()): + return "No sub container is found, please check the container type" + sub_container = containers_subcontainers[contianer] + res_index = resource_elasticsearchindex.get("image") + aggs_part = container_returned_sub_container_template.substitute( + container_attribute_name="%s_name.keyvalue" % contianer, + container_attribute_value="%s" % container_name, + returned_sub_container="%s_name.keyvalue" % sub_container, + ) + if not query_details: + query = {} + else: + and_filters = query_details.get("and_filters") + or_filters = query_details.get("or_filters") + case_sensitive = query_details.get("case_sensitive") + main_attributes = query.get("main_attributes") + from omero_search_engine.api.v1.resources.utils import ( + elasticsearch_query_builder, + ) + + query_string = elasticsearch_query_builder( + and_filters, or_filters, case_sensitive, main_attributes + ) + query = json.loads(query_string) + # query builder should be called + + query["aggs"] = json.loads(aggs_part) + res = search_index_for_value(res_index, query) + buckets = res["aggregations"]["values"]["uniquesTerms"]["buckets"] + print(len(buckets)) + print(buckets[0]) + returned_results = [] + for bucket in buckets: + returned_results.append( + {"no_images": bucket["doc_count"], "%s_name" % sub_container: bucket["key"]} + ) + return jsonify(returned_results) + + +def return_containes_images(): + screens_query = containers_no_images.substitute(conatins_name="screen_name") + projects_query = containers_no_images.substitute(conatins_name="project_name") + res_index = resource_elasticsearchindex.get("image") + screens = search_index_for_value(res_index, screens_query) + projects = search_index_for_value(res_index, projects_query) + projects = projects["aggregations"]["uniquesTerms"]["buckets"] + screens = screens["aggregations"]["uniquesTerms"]["buckets"] + results = {} + results["projects"] = projects + results["screens"] = screens + returned_results = {} + + for res, items in results.items(): + res_items = [] + returned_results[res] = res_items + for item in items: + res_items.append( + {"no_images": item["doc_count"], "%s_name" % res: item["key"]} + ) + return jsonify(returned_results) diff --git a/omero_search_engine/api/v1/resources/urls.py b/omero_search_engine/api/v1/resources/urls.py index fc1dbf8..e955cb7 100644 --- a/omero_search_engine/api/v1/resources/urls.py +++ b/omero_search_engine/api/v1/resources/urls.py @@ -464,3 +464,24 @@ def container_keys_search(resource_table): csv = False results = get_container_values_for_key(resource_table, container_name, csv) return results + + +@resources.route("/container_images/", methods=["GET"]) +def container_images(): + from omero_search_engine.api.v1.resources.resource_analyser import ( + return_containes_images, + ) + + return return_containes_images() + + +# to do: add query to return the results withiz the sub-container +@resources.route("/sub_container_images/", methods=["GET"]) +def sub_container_images(): + from omero_search_engine.api.v1.resources.resource_analyser import ( + get_containers_no_images, + ) + + container = request.args.get("container") + container_name = request.args.get("container_name") + return get_containers_no_images(container, container_name)