diff --git a/application/database/db.py b/application/database/db.py index 10843ab1d..9ddc9b853 100644 --- a/application/database/db.py +++ b/application/database/db.py @@ -28,7 +28,7 @@ import uuid from application.utils.gap_analysis import get_path_score -from application.utils.hash import make_array_hash +from application.utils.hash import make_array_hash, make_cache_key from .. import sqla # type: ignore @@ -1654,7 +1654,7 @@ def add_gap_analysis_result(self, cache_key: str, ga_object: dict): self.session.add(res) self.session.commit() else: - return existing.ga_object + return existing def dbNodeFromNode(doc: cre_defs.Node) -> Optional[Node]: @@ -1836,15 +1836,13 @@ def gap_analysis( cache_key = make_array_hash(node_names) # conn.set(cache_key, flask_json.dumps({"result": grouped_paths})) - if cre_db: - cre_db.add_gap_analysis_result( - cache_key=cache_key, ga_object={"result": extra_paths_dict[key]} + cre_db.add_gap_analysis_result( + cache_key=cache_key, ga_object={"result": grouped_paths} ) for key in extra_paths_dict: - if cre_db: - cre_db.add_gap_analysis_result( - cache_key=cache_key, ga_object={"result": extra_paths_dict[key]} + cre_db.add_gap_analysis_result( + cache_key=make_cache_key(node_names,key), ga_object={"result": extra_paths_dict[key]} ) # conn.set( # cache_key + "->" + key, diff --git a/application/tests/db_test.py b/application/tests/db_test.py index 5142d5011..f119ea51d 100644 --- a/application/tests/db_test.py +++ b/application/tests/db_test.py @@ -1513,7 +1513,7 @@ def test_gap_analysis_duplicate_link_path_existing_higher_and_in_extras( @patch.object(redis, "from_url") @patch.object(db.NEO_DB, "gap_analysis") - def test_gap_analysis_dump_to_cache(self, gap_mock, redis_conn_mock): + def test_gap_analysis_dump_to_cache(self, gap_mock): collection = db.Node_collection() collection.neo_db.connected = True path = [ @@ -1567,19 +1567,14 @@ def test_gap_analysis_dump_to_cache(self, gap_mock, redis_conn_mock): response = db.gap_analysis(collection.neo_db, ["a", "b"], True) self.assertEqual(response, (expected_response[0], {}, {})) - - redis_conn_mock.return_value.set.assert_has_calls( - [ - mock.call( - "d8160c9b3dc20d4e931aeb4f45262155", - flask_json.dumps({"result": expected_response[1]}), - ), - mock.call( - "d8160c9b3dc20d4e931aeb4f45262155->a", - flask_json.dumps({"result": expected_response[2]["a"]}), - ), - ] - ) + self.assertEqual( + collection.get_gap_analysis_result("d8160c9b3dc20d4e931aeb4f45262155"), + flask_json.dumps({"result": expected_response[1]}) + ) + self.assertEqual( + collection.get_gap_analysis_result("d8160c9b3dc20d4e931aeb4f45262155->a"), + flask_json.dumps({"result": expected_response[2]["a"]}), + ) def test_neo_db_parse_node_code(self): name = "name" diff --git a/application/tests/web_main_test.py b/application/tests/web_main_test.py index 2367fbc9c..6ca53da1e 100644 --- a/application/tests/web_main_test.py +++ b/application/tests/web_main_test.py @@ -11,7 +11,7 @@ from application.defs import cre_defs as defs from application.defs import osib_defs from application.web import web_main - +from application.utils.hash import make_array_hash, make_cache_key class MockJob: @property @@ -686,8 +686,11 @@ def test_gap_analysis_weak_links_no_cache(self) -> None: @patch.object(redis, "from_url") def test_gap_analysis_weak_links_response(self, redis_conn_mock) -> None: expected = {"result": "hello"} - redis_conn_mock.return_value.exists.return_value = True - redis_conn_mock.return_value.get.return_value = json.dumps(expected) + collection = db.Node_collection() + standards =["aaa","bbb"] + key = "ccc" + cache_key = make_cache_key(standards=standards,key=key) + collection.add_gap_analysis_result(cache_key=cache_key,ga_object=expected) with self.app.test_client() as client: response = client.get( "/rest/v1/map_analysis_weak_links?standard=aaa&standard=bbb&key=ccc`", diff --git a/application/utils/hash.py b/application/utils/hash.py index f305dd829..0856531d2 100644 --- a/application/utils/hash.py +++ b/application/utils/hash.py @@ -1,5 +1,7 @@ import hashlib +def make_cache_key(standards:list, key:str)->str: + return make_array_hash(standards) + "->" + key def make_array_hash(array: list): return hashlib.md5(":".join(array).encode("utf-8")).hexdigest() diff --git a/application/web/web_main.py b/application/web/web_main.py index fd82b994d..e5529b79f 100644 --- a/application/web/web_main.py +++ b/application/web/web_main.py @@ -36,7 +36,7 @@ from application.utils.spreadsheet import write_csv import oauthlib import google.auth.transport.requests -from application.utils.hash import make_array_hash +from application.utils.hash import make_array_hash, make_cache_key ITEMS_PER_PAGE = 20 @@ -269,9 +269,7 @@ def gap_analysis() -> Any: def gap_analysis_weak_links() -> Any: standards = request.args.getlist("standard") key = request.args.get("key") - conn = redis.connect() - standards_hash = make_array_hash(standards) - cache_key = standards_hash + "->" + key + cache_key = make_cache_key(standards=standards,key=key) database = db.Node_collection() result = database.get_gap_analysis_result(cache_key=cache_key)