Commit 49d6a58d authored by David Mendez's avatar David Mendez
Browse files

Entities joiner: use a function to generate the join query depending on configuration

parent f71c8cb2
......@@ -36,7 +36,7 @@ def get_tiny_hash_to_related_items(destination_entity_browser_state_template,
app_logging.debug(f'entities join cache_key: {cache_key}')
cache_response = cache.fail_proof_get(key=cache_key)
if cache_response is None:
if cache_response is not None:
save_statistics(entity_from, entity_to, None, True)
return cache_response
......@@ -71,15 +71,17 @@ def get_tiny_hash_to_related_items(destination_entity_browser_state_template,
index_name = standardisation.get_index_name_for_origin_entity(parsed_origin_entity)
fail_if_null(index_name, 'index name', parsed_origin_entity, parsed_destination_entity)
from_property = standardisation.get_from_property(parsed_origin_entity, parsed_destination_entity)
fail_if_null(from_property, 'from property', parsed_origin_entity, parsed_destination_entity)
origin_property = standardisation.get_origin_property(parsed_origin_entity, parsed_destination_entity)
fail_if_null(origin_property, 'origin property', parsed_origin_entity, parsed_destination_entity)
selection_description = json.loads(raw_selection_description)
es_query = json.loads(raw_es_query)
ids = ids_loader.load_ids_for_query(es_query, selection_description, from_property, index_name)
ids = ids_loader.load_ids_for_query(es_query, selection_description, origin_property, index_name)
to_property = standardisation.get_to_property(parsed_origin_entity, parsed_destination_entity)
fail_if_null(to_property, 'to property', parsed_origin_entity, parsed_destination_entity)
destination_query_generator = standardisation.get_destination_query_generator(parsed_origin_entity,
parsed_destination_entity)
fail_if_null(destination_query_generator, 'destination query generator', parsed_origin_entity,
parsed_destination_entity)
settings_path = standardisation.get_settings_path_for_destination_entity(parsed_destination_entity)
fail_if_null(settings_path, 'settings path', parsed_origin_entity, parsed_destination_entity)
......@@ -87,7 +89,7 @@ def get_tiny_hash_to_related_items(destination_entity_browser_state_template,
browser_name = standardisation.get_browser_names_for_destination_entity(parsed_destination_entity)
fail_if_null(browser_name, 'browser name', parsed_origin_entity, parsed_destination_entity)
join_state_hash = get_join_state_hash(ids, to_property, settings_path, browser_name,
join_state_hash = get_join_state_hash(ids, destination_query_generator, settings_path, browser_name,
destination_entity_browser_state_template,
previous_hash)
......@@ -129,18 +131,18 @@ def fail_if_null(value, value_name, parsed_origin_entity, parsed_destination_ent
f'to {parsed_destination_entity.value}')
def get_join_state_hash(ids, to_property, settings_path, browser_name, destination_entity_browser_state_template,
def get_join_state_hash(ids, destination_query_generator, settings_path, browser_name,
destination_entity_browser_state_template,
previous_hash):
"""
:param ids: list of its for the join
:param to_property: entity to which to do the join
:param destination_query_generator: function that generates the join query from a list of ids
:param settings_path: settings path to be used by the user interface
:param browser_name: name of the destination browser used to build the desired url
:param destination_entity_browser_state_template: template to build the url for the destination entity browser
:param previous_hash: hash of the state that originated this join of entities
:return: the hash for the query state used for the join
"""
ids_clauses = " OR ".join([f'"{item_id}"' for item_id in ids])
desired_state = {
'origin': {
'type': 'ENTITIES_JOIN',
......@@ -148,7 +150,7 @@ def get_join_state_hash(ids, to_property, settings_path, browser_name, destinati
},
'list': {
'settings_path': settings_path,
'custom_query': f'{to_property}({ids_clauses})',
'custom_query': destination_query_generator(ids),
'use_custom_query': True,
'search_term': "",
'at_least_one_facet_is_selected': False,
......
......@@ -65,29 +65,44 @@ def get_index_name_for_origin_entity(parsed_origin_entity):
return index_name
def create_simple_query_generator(destination_property):
"""
:param destination_property: property to use to build the query
:return: a function to be used to build the join query
"""
def join_function(ids):
"""
:param ids: ids of the items matches
:return: query to use for the join
"""
ids_clauses = " OR ".join([f'"{item_id}"' for item_id in ids])
return f'{destination_property}({ids_clauses})'
return join_function
JOIN_PROPERTIES = {
'from': {
PossibleOriginEntities.CHEMBL_DRUG_WARNINGS: {
'to': {
PossibleDestinationEntities.CHEMBL_ACTIVITIES: {
'from_property': 'drug_warning.molecule_chembl_id',
'to_property': 'molecule_chembl_id'
'origin_property': 'drug_warning.molecule_chembl_id',
'destination_query_generator': create_simple_query_generator('molecule_chembl_id')
},
PossibleDestinationEntities.CHEMBL_COMPOUNDS: {
'from_property': 'drug_warning.molecule_chembl_id',
'to_property': 'molecule_chembl_id'
'origin_property': 'drug_warning.molecule_chembl_id',
'destination_query_generator': create_simple_query_generator('molecule_chembl_id')
},
PossibleDestinationEntities.CHEMBL_DRUGS: {
'from_property': 'drug_warning.parent_molecule_chembl_id',
'to_property': 'molecule_chembl_id'
'origin_property': 'drug_warning.parent_molecule_chembl_id',
'destination_query_generator': create_simple_query_generator('molecule_chembl_id')
},
PossibleDestinationEntities.CHEMBL_DRUG_MECHANISMS: {
'from_property': 'drug_warning.parent_molecule_chembl_id',
'to_property': 'molecule_chembl_id'
'origin_property': 'drug_warning.parent_molecule_chembl_id',
'destination_query_generator': create_simple_query_generator('molecule_chembl_id')
},
PossibleDestinationEntities.CHEMBL_DRUG_INDICATIONS: {
'from_property': 'drug_warning.parent_molecule_chembl_id',
'to_property': 'molecule_chembl_id'
'origin_property': 'drug_warning.parent_molecule_chembl_id',
'destination_query_generator': create_simple_query_generator('molecule_chembl_id')
}
}
}
......@@ -112,7 +127,7 @@ BROWSER_NAMES_FOR_DESTINATION_ENTITIES = {
}
def get_from_property(parsed_origin_entity, parsed_destination_entity):
def get_origin_property(parsed_origin_entity, parsed_destination_entity):
"""
:param parsed_origin_entity: entity 'from' parsed by the PossibleEntitiesFrom enum
:param parsed_destination_entity: entity 'to' parsed by the PossibleEntitiesTo enum
......@@ -122,10 +137,10 @@ def get_from_property(parsed_origin_entity, parsed_destination_entity):
get('from', {}). \
get(parsed_origin_entity, {}). \
get('to', {}). \
get(parsed_destination_entity, {}).get('from_property')
get(parsed_destination_entity, {}).get('origin_property')
def get_to_property(parsed_origin_entity, parsed_destination_entity):
def get_destination_query_generator(parsed_origin_entity, parsed_destination_entity):
"""
:param parsed_origin_entity: entity 'from' parsed by the PossibleEntitiesFrom enum
:param parsed_destination_entity: entity 'to' parsed by the PossibleEntitiesTo enum
......@@ -135,7 +150,7 @@ def get_to_property(parsed_origin_entity, parsed_destination_entity):
get('from', {}). \
get(parsed_origin_entity, {}). \
get('to', {}). \
get(parsed_destination_entity, {}).get('to_property')
get(parsed_destination_entity, {}).get('destination_query_generator')
def get_settings_path_for_destination_entity(parsed_destination_entity):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment