From 42b44db625f5a0f52a9b5c7fba7834bb6b22c624 Mon Sep 17 00:00:00 2001 From: khaledk2 Date: Wed, 27 Nov 2024 22:25:17 +0000 Subject: [PATCH] add return sub container --- .../api/v1/resources/resource_analyser.py | 117 ++++++++++++++---- omero_search_engine/api/v1/resources/urls.py | 31 +++++ 2 files changed, 127 insertions(+), 21 deletions(-) diff --git a/omero_search_engine/api/v1/resources/resource_analyser.py b/omero_search_engine/api/v1/resources/resource_analyser.py index 0b0e77f..c0dd526 100644 --- a/omero_search_engine/api/v1/resources/resource_analyser.py +++ b/omero_search_engine/api/v1/resources/resource_analyser.py @@ -1053,34 +1053,109 @@ def get_resource_keys(resource, data_source): return res["aggregations"]["value_search"]["required_name"]["buckets"] +# 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 + } + } + } + } + } +""" +) + + +def get_containers_no_images(container_name, query_details=None): + contianer = None + containers_subcontainers = {"project": "dataset", "screen": "plate"} + act_names = get_containets_from_name(container_name) + if len(act_names) == 0: + return "Container %s is not found" % container_name + container_name = act_names[0]["name"] + contianer = act_names[0]["type"] + data_source = act_names[0]["data_source"] + if contianer.lower() in containers_subcontainers: + sub_container = containers_subcontainers[contianer.lower()] + else: + return "Container %s is not supported" % 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_details.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"] + returned_results = [] + for bucket in buckets: + returned_results.append( + { + "image count": bucket["doc_count"], + "%s_name" % sub_container: bucket["key"], + "data_source": data_source, + } + ) + return {"Error": None, "results": {"results": returned_results}} + + def get_containets_from_name(container_name=None, returned_data_source=None): act_names = [] # {} pr_names = get_resource_names("all") for resourse, names_ in pr_names.items(): if len(names_) > 0: print(names_.keys()) - for data_source, names in names_.items(): - act_names.append( - [ - { - "id": name["id"], - "name": name["name"], - "data_source": data_source, - "image count": name["no_images"], - "type": resourse, - } - for name in names - if not container_name - or ( - name.get("name") - and container_name.lower() in name.get("name").lower() - ) - and ( - not returned_data_source or returned_data_source == data_source - ) - ] - ) + act_names_1 = [ + { + "id": name["id"], + "name": name["name"], + "data_source": data_source, + "image count": name["no_images"], + "type": resourse, + } + for name in names + if not container_name + or ( + name.get("name") + and container_name.lower() in name.get("name").lower() + ) + and (not returned_data_source or returned_data_source == data_source) + ] + act_names = act_names + act_names_1 return act_names diff --git a/omero_search_engine/api/v1/resources/urls.py b/omero_search_engine/api/v1/resources/urls.py index d8086ce..fefa110 100644 --- a/omero_search_engine/api/v1/resources/urls.py +++ b/omero_search_engine/api/v1/resources/urls.py @@ -508,3 +508,34 @@ def container_keys_search(resource_table): resource_table, container_name, csv, ret_data_source=data_source ) return results + + +# to do: add query to return the results withiz the sub-container +@resources.route("/sub_container_images/", methods=["POST"]) +def sub_container_images(): + """ + file: swagger_docs/sub_container_images.yml + """ + from omero_search_engine.api.v1.resources.resource_analyser import ( + get_containers_no_images, + ) + + container_name = request.args.get("container_name") + if not container_name: + return jsonify( + build_error_message("{error}".format(error="Container name is required.")) + ) + data = request.data + query = {} + if data: + try: + data = json.loads(data) + except Exception: + return jsonify( + build_error_message( + "{error}".format(error="No proper query data is provided.") + ) + ) + if "query_details" in data: + query = data["query_details"] + return jsonify(get_containers_no_images(container_name, query))