Commit 05590d3e authored by David Mendez's avatar David Mendez
Browse files

Start to add endpoind to do join of entities

parent 3872a360
......@@ -16,6 +16,7 @@ from app.blueprints.url_shortening.controllers.url_shortening_controller import
from app.blueprints.element_usage_blueprint.controllers.element_usage_controller import ELEMENT_USAGE_BLUEPRINT
from app.blueprints.visualisation_data.controllers.visualisation_data_controller import VISUALISATION_DATA_BLUEPRINT
from app.blueprints.utils.controllers.utils_controller import UTILS_BLUEPRINT
from app.blueprints.entities_join.controllers.entities_join_controller import ENTITIES_JOIN_BLUEPRINT
def create_app():
......@@ -43,15 +44,56 @@ def create_app():
CACHE.cache._client.behaviors['retry_timeout'] = 10
CACHE.cache._client.behaviors['retry_timeout'] = 600
flask_app.register_blueprint(SWAGGER_BLUEPRINT, url_prefix=f'{base_path}/swagger')
flask_app.register_blueprint(ES_PROXY_BLUEPRINT, url_prefix=f'{base_path}/es_data')
flask_app.register_blueprint(PROPERTIES_CONFIG_BLUEPRINT, url_prefix=f'{base_path}/properties_configuration')
flask_app.register_blueprint(CONTEXTS_BLUEPRINT, url_prefix=f'{base_path}/contexts')
flask_app.register_blueprint(SEARCH_PARSER_BLUEPRINT, url_prefix=f'{base_path}/search_parsing')
flask_app.register_blueprint(URL_SHORTENING_BLUEPRINT, url_prefix=f'{base_path}/url_shortening')
flask_app.register_blueprint(ELEMENT_USAGE_BLUEPRINT, url_prefix=f'{base_path}/frontend_element_usage')
flask_app.register_blueprint(VISUALISATION_DATA_BLUEPRINT, url_prefix=f'{base_path}/visualisations')
flask_app.register_blueprint(UTILS_BLUEPRINT, url_prefix=f'{base_path}/utils')
blueprints_urls = [
{
'blueprint': SWAGGER_BLUEPRINT,
'url': '/swagger'
},
{
'blueprint': ES_PROXY_BLUEPRINT,
'url': '/es_data'
},
{
'blueprint': PROPERTIES_CONFIG_BLUEPRINT,
'url': '/properties_configuration'
},
{
'blueprint': CONTEXTS_BLUEPRINT,
'url': '/contexts'
},
{
'blueprint': SEARCH_PARSER_BLUEPRINT,
'url': '/search_parsing'
},
{
'blueprint': URL_SHORTENING_BLUEPRINT,
'url': '/url_shortening'
},
{
'blueprint': ELEMENT_USAGE_BLUEPRINT,
'url': '/frontend_element_usage'
},
{
'blueprint': VISUALISATION_DATA_BLUEPRINT,
'url': '/visualisations'
},
{
'blueprint': UTILS_BLUEPRINT,
'url': '/utils'
},
{
'blueprint': ENTITIES_JOIN_BLUEPRINT,
'url': '/entities_join'
}
]
for blueprint_desc in blueprints_urls:
blueprint = blueprint_desc['blueprint']
url = blueprint_desc['url']
flask_app.register_blueprint(blueprint, url_prefix=f'{base_path}/{url}')
print(f'{url} LOADED')
print(f'LOADED {len(blueprints_urls)} BLUEPRINTS')
return flask_app
......
"""
The blueprint used for handling requests to do joins of entities
"""
from flask import Blueprint, jsonify, abort, request
from app.request_validation.decorators import validate_form_with, validate_url_params_with
from app.blueprints.entities_join.controllers import marshmallow_schemas
from app.blueprints.entities_join.services import entities_join_service
from app import app_logging
from utils import request_parameters
ENTITIES_JOIN_BLUEPRINT = Blueprint('entities_join', __name__)
@ENTITIES_JOIN_BLUEPRINT.route('/get_link_to_related_items', methods=['POST'])
@validate_form_with(marshmallow_schemas.EntitiesJoinQuery)
def get_link_to_related_items():
"""
returns the hash of the tiny url to the related items with the parameters given
"""
form_data = request.form
destination_entity_browser_state_template = request_parameters.sanitise_parameter(
form_data.get('destination_entity_browser_state_template'))
entity_from = request_parameters.sanitise_parameter(form_data.get('entity_from'))
entity_to = request_parameters.sanitise_parameter(form_data.get('entity_to'))
es_query = request_parameters.sanitise_parameter(form_data.get('es_query'))
selection_description = request_parameters.sanitise_parameter(form_data.get('selection_description'))
app_logging.debug(f'destination_entity_browser_state_template: {destination_entity_browser_state_template}')
app_logging.debug(f'entity_from: {entity_from}')
app_logging.debug(f'entity_to: {entity_to}')
app_logging.debug(f'es_query: {es_query}')
app_logging.debug(f'selection_description: {selection_description}')
try:
json_response = entities_join_service.get_link_to_related_items(destination_entity_browser_state_template,
entity_from, entity_to, es_query,
selection_description)
return jsonify(json_response)
except entities_join_service.EntitiesJoinServiceError as error:
abort(500, msg=f'Internal server error: {str(error)}')
"""
Schemas to validate the input of entities join Endpoint
"""
from marshmallow import Schema, fields
class EntitiesJoinQuery(Schema):
"""
Class that the schema doing an es join
"""
destination_entity_browser_state_template = fields.String(required=True)
entity_from = fields.String(required=True)
entity_to = fields.String(required=True)
es_query = fields.String(required=True)
selection_description = fields.String(required=True)
"""
Service that handles the requests to the entities join
"""
import json
from app import app_logging
from app.es_data import es_data
from app.context_loader import context_loader
from app.config import RUN_CONFIG
from app.es_data import es_mappings
class EntitiesJoinServiceError(Exception):
"""Base class for exceptions in this file."""
def get_link_to_related_items(destination_entity_browser_state_template,
entity_from, entity_to, es_query,
selection_description):
"""
:param destination_entity_browser_state_template: template for building the resulting browser url
:param entity_from: source entity of the items
:param entity_to: destination entity of the join
:param es_query: query in elasticsearch for the dataset
:param selection_description: stringifyed javascript object describing de selection of items in the dataset
"""
return {
'msg': 'hola'
}
......@@ -8,6 +8,7 @@ from app.blueprints.es_proxy.controllers import marshmallow_schemas
from app.blueprints.es_proxy.services import es_proxy_service
from app import app_logging
from app.http_cache import http_cache_utils
from utils import request_parameters
ES_PROXY_BLUEPRINT = Blueprint('es_proxy', __name__)
......@@ -21,10 +22,10 @@ def get_es_data():
"""
form_data = request.form
index_name = sanitise_parameter(form_data.get('index_name'))
raw_es_query = sanitise_parameter(form_data.get('es_query'))
raw_context = sanitise_parameter(form_data.get('context_obj'))
raw_contextual_sort_data = sanitise_parameter(form_data.get('contextual_sort_data'))
index_name = request_parameters.sanitise_parameter(form_data.get('index_name'))
raw_es_query = request_parameters.sanitise_parameter(form_data.get('es_query'))
raw_context = request_parameters.sanitise_parameter(form_data.get('context_obj'))
raw_contextual_sort_data = request_parameters.sanitise_parameter(form_data.get('contextual_sort_data'))
app_logging.debug(f'index_name: {index_name}')
app_logging.debug(f'raw_es_query: {raw_es_query}')
......@@ -66,13 +67,3 @@ def get_es_doc(index_name, doc_id):
except es_proxy_service.ESDataNotFoundError as error:
abort(404)
def sanitise_parameter(param_value):
"""
Makes the parameter null if it is 'null' or 'undefined', in some cases javascript produces those values
:param param_value: value of the parameter
:return: null if param_value in ('null', 'undefined'), the actual value otherwise
"""
if param_value in ('null', 'undefined'):
return None
return param_value
......@@ -510,14 +510,14 @@ paths:
required: true
type: 'string'
default: 'wwwdev.ebi.ac.uk/chembl/g/#browse/<BROWSER_NAME>/full_state/<GENERATED_STATE>'
- name: 'from'
- name: 'entity_from'
in: 'formData'
description: 'Source entity for the join.'
required: true
type: 'string'
enum: [ 'CHEMBL_COMPOUNDS', 'CHEMBL_TARGETS', 'CHEMBL_ASSAYS', 'CHEMBL_DOCUMENTS', 'CHEMBL_CELL_LINES', 'CHEMBL_TISSUES' ]
default: 'CHEMBL_COMPOUNDS'
- name: 'to'
- name: 'entity_to'
in: 'formData'
description: 'The entity in ChEMBL to do the join with.'
required: true
......
"""
Utils functions for the requests parameters
"""
def sanitise_parameter(param_value):
"""
Makes the parameter null if it is 'null' or 'undefined', in some cases javascript produces those values
:param param_value: value of the parameter
:return: null if param_value in ('null', 'undefined'), the actual value otherwise
"""
if param_value in ('null', 'undefined'):
return None
return param_value
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