From 7ae357a4825fa5abdb207e40813b591e016d13aa Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Thu, 27 Jul 2023 19:09:55 +0000 Subject: [PATCH 1/6] handle gohan errors in beacon overview --- bento_beacon/utils/gohan_utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/bento_beacon/utils/gohan_utils.py b/bento_beacon/utils/gohan_utils.py index 1f910a8..5f160e4 100644 --- a/bento_beacon/utils/gohan_utils.py +++ b/bento_beacon/utils/gohan_utils.py @@ -195,7 +195,7 @@ def gohan_network_call(url, gohan_args): gohan_response = r.json() except requests.exceptions.RequestException as e: - current_app.logger.debug(f"gohan error: {e}") + current_app.logger.error(f"gohan error: {e}") raise APIException(message="error calling gohan variants service") return gohan_response @@ -232,9 +232,18 @@ def gohan_counts_by_assembly_id(): # so check for a table before calling def gohan_counts_for_overview(): tables_url = current_app.config["GOHAN_BASE_URL"] + "/tables?data-type=variant" - has_tables = gohan_network_call(tables_url, {}) - if has_tables: + tables = [] + + try: + tables = gohan_network_call(tables_url, {}) + except APIException: + # note this exception but don't rethrow + current_app.logger.error("cannot reach gohan for overview") + + if tables: return gohan_counts_by_assembly_id() + + # either api is dead or elasticsearch is down return {"error": "gohan unavailable"} # -------------------------------------------- From 7934f89ba49baaad48448f561094db1b00ba89e5 Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Mon, 31 Jul 2023 14:52:51 +0000 Subject: [PATCH 2/6] empty variants table not necessarily an error --- bento_beacon/utils/gohan_utils.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bento_beacon/utils/gohan_utils.py b/bento_beacon/utils/gohan_utils.py index 5f160e4..4b6c751 100644 --- a/bento_beacon/utils/gohan_utils.py +++ b/bento_beacon/utils/gohan_utils.py @@ -228,11 +228,11 @@ def gohan_counts_by_assembly_id(): return gohan_overview().get("assemblyIDs", {}) -# gohan /variants/overview hangs when no variants table -# so check for a table before calling def gohan_counts_for_overview(): + # gohan /variants/overview hangs when no variants table + # so check for tables before calling tables_url = current_app.config["GOHAN_BASE_URL"] + "/tables?data-type=variant" - tables = [] + tables = None try: tables = gohan_network_call(tables_url, {}) @@ -240,10 +240,15 @@ def gohan_counts_for_overview(): # note this exception but don't rethrow current_app.logger.error("cannot reach gohan for overview") + # non-empty tables if tables: return gohan_counts_by_assembly_id() + + # empty tables (fresh instance or elasticsearch down) + if tables is not None: + return {} - # either api is dead or elasticsearch is down + # else bad response from gohan return {"error": "gohan unavailable"} # -------------------------------------------- From 7b50264c3188fc7634e38d9611d3776a9c1bbee7 Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Mon, 31 Jul 2023 17:52:52 +0000 Subject: [PATCH 3/6] add strip().lower(), to improve --- bento_beacon/endpoints/info.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bento_beacon/endpoints/info.py b/bento_beacon/endpoints/info.py index e3db4ef..eebf1cd 100644 --- a/bento_beacon/endpoints/info.py +++ b/bento_beacon/endpoints/info.py @@ -1,7 +1,7 @@ from flask import Blueprint, current_app from ..utils.beacon_response import beacon_info_response from ..utils.katsu_utils import get_filtering_terms, get_filtering_term_resources, katsu_total_individuals_count, katsu_get -from ..utils.gohan_utils import gohan_counts_for_overview +from ..utils.gohan_utils import gohan_counts_for_overview, useGohan info = Blueprint("info", __name__) @@ -11,7 +11,7 @@ def overview(): - if current_app.config["BEACON_CONFIG"].get("useGohan"): + if current_app.config["BEACON_CONFIG"].get("useGohan").strip().lower() in ('true', '1', 't'): variants_count = gohan_counts_for_overview() else: variants_count = {} From 3e0f0bc14c693499e34a33e5745dcdfad0404bdb Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Mon, 31 Jul 2023 17:54:22 +0000 Subject: [PATCH 4/6] if 'UseGohan", expect variants table to exist --- bento_beacon/utils/gohan_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bento_beacon/utils/gohan_utils.py b/bento_beacon/utils/gohan_utils.py index 4b6c751..81df504 100644 --- a/bento_beacon/utils/gohan_utils.py +++ b/bento_beacon/utils/gohan_utils.py @@ -228,6 +228,7 @@ def gohan_counts_by_assembly_id(): return gohan_overview().get("assemblyIDs", {}) +# only runs if "useGohan" true def gohan_counts_for_overview(): # gohan /variants/overview hangs when no variants table # so check for tables before calling @@ -245,8 +246,9 @@ def gohan_counts_for_overview(): return gohan_counts_by_assembly_id() # empty tables (fresh instance or elasticsearch down) + # "useGohan" is true here so we expect variants to exist if tables is not None: - return {} + return {"error": "no variants available"} # else bad response from gohan return {"error": "gohan unavailable"} From 293e6663cf86d95792a5afa7ec2ae8f04a9b4d72 Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Mon, 31 Jul 2023 18:01:20 +0000 Subject: [PATCH 5/6] rv .stip().lower(), this is already a bool --- bento_beacon/endpoints/info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bento_beacon/endpoints/info.py b/bento_beacon/endpoints/info.py index be2019e..c1d0975 100644 --- a/bento_beacon/endpoints/info.py +++ b/bento_beacon/endpoints/info.py @@ -18,7 +18,7 @@ def overview(): - if current_app.config["BEACON_CONFIG"].get("useGohan").strip().lower() in ('true', '1', 't'): + if current_app.config["BEACON_CONFIG"].get("useGohan"): variants_count = gohan_counts_for_overview() else: variants_count = {} From 0b52ff215630db137e9eb0959c64b06a45642a3e Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Mon, 31 Jul 2023 18:09:03 +0000 Subject: [PATCH 6/6] fix beaconId in response meta --- bento_beacon/utils/beacon_response.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bento_beacon/utils/beacon_response.py b/bento_beacon/utils/beacon_response.py index 57504f4..8d294bd 100644 --- a/bento_beacon/utils/beacon_response.py +++ b/bento_beacon/utils/beacon_response.py @@ -115,10 +115,9 @@ def received_request(): def build_response_meta(): returned_schemas = g.get("response_data", {}).get("returnedSchemas", []) returned_granularity = g.get("response_data", {}).get("returnedGranularity", "count") - service_info = current_app.config["BEACON_CONFIG"].get("serviceInfo") received_request_summary = received_request() return { - "beaconId": service_info.get("id"), + "beaconId": current_app.config["BEACON_ID"], "apiVersion": current_app.config["BEACON_SPEC_VERSION"], "returnedSchemas": returned_schemas, "returnedGranularity": returned_granularity, @@ -127,9 +126,8 @@ def build_response_meta(): def build_info_response_meta(): - service_info = current_app.config["BEACON_CONFIG"].get("serviceInfo") return { - "beaconId": service_info.get("id"), + "beaconId": current_app.config["BEACON_ID"], "apiVersion": current_app.config["BEACON_SPEC_VERSION"], "returnedSchemas": [] }