def get_abstraction(): """Get abstraction Returns ------- json abstraction: abstraction error: True if error, else False errorMessage: the error message of error, else an empty string """ try: tse = TriplestoreExplorer(current_app, session) abstraction = tse.get_abstraction() files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'diskSpace': disk_space, 'abstraction': [], 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'diskSpace': disk_space, 'abstraction': abstraction, 'error': False, 'errorMessage': '' })
def get_abstraction(): """Get abstraction Returns ------- json abstraction: abstraction error: True if error, else False errorMessage: the error message of error, else an empty string """ try: # If public datasets and queries are protected, dont return anything to unlogged users if "user" not in session and current_app.iniconfig.getboolean("askomics", "protect_public"): abstraction = {} disk_space = None else: tse = TriplestoreExplorer(current_app, session) abstraction = tse.get_abstraction() files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user() if "user" in session else None except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'diskSpace': None, 'abstraction': {}, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'diskSpace': disk_space, 'abstraction': abstraction, 'error': False, 'errorMessage': '' })
def upload_url(): """Upload a distant file with an URL Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user() if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"]["quota"]: return jsonify({ 'errorMessage': "Exceeded quota", "error": True }), 500 data = request.get_json() try: files = FilesHandler(current_app, session) files.download_url(data["url"]) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ "error": True, "errorMessage": str(e) }), 500 return jsonify({ "error": False, "errorMessage": "" })
def get_size_occupied_by_user(self): """Get size of logged user Returns ------- int Size """ files_utils = FilesUtils(self.app, self.session) return files_utils.get_size_occupied_by_user() if "user" in self.session else None
def get_sparql_query(): """Get sparql query of result for the query editor Returns ------- json query: the sparql query error: True if error, else False errorMessage: the error message of error, else an empty string """ try: files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None file_id = request.get_json()["fileId"] result_info = {"id": file_id} result = Result(current_app, session, result_info) query = SparqlQuery(current_app, session) sparql = result.get_sparql_query() # get graphs and endpoints used in the query graphs = result.graphs endpoints = result.endpoints # Get all graphs and endpoint, and mark as selected the used one graphs, endpoints = query.get_graphs_and_endpoints( selected_graphs=graphs, selected_endpoints=endpoints) # Build sparql query from json if needed if sparql is None: query.json = result.get_graph_state() query.build_query_from_json(for_editor=True) sparql = query.sparql except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'query': {}, 'graphs': [], 'endpoints': [], 'diskSpace': 0, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'query': sparql, 'graphs': graphs, 'endpoints': endpoints, 'diskSpace': disk_space, 'error': False, 'errorMessage': '' })
def save_result(): """Save a query in filesystem and db, using a celery task Returns ------- json task_id: celery task id error: True if error, else False errorMessage: the error message of error, else an empty string """ try: files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"][ "quota"]: return jsonify({ 'error': True, 'errorMessage': "Exceeded quota", 'task_id': None }), 500 # Get query and endpoints and graphs of the query graph_state = request.get_json()["graphState"] query_builder = SparqlQueryBuilder(current_app, session) query = query_builder.build_query_from_json(graph_state, preview=False, for_editor=False) endpoints = query_builder.endpoints graphs = query_builder.graphs info = { "graph_state": graph_state, "query": query, "graphs": graphs, "endpoints": endpoints, "celery_id": None } result = Result(current_app, session, info) info["id"] = result.save_in_db() session_dict = {"user": session["user"]} task = current_app.celery.send_task("query", (session_dict, info)) result.update_celery(task.id) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': str(e), 'task_id': None }), 500 return jsonify({'error': False, 'errorMessage': '', 'task_id': task.id})
def get_graph_and_sparql_query(): """Get query (graphState or Sparql) Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ try: file_id = request.get_json()["fileId"] result_info = {"id": file_id} result = Result(current_app, session, result_info) # Get graph state and sparql query graph_state = result.get_graph_state(formated=True) sparql_query = result.get_sparql_query() # Get disk space files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None # Get graphs and endpoints graphs = result.graphs endpoints = result.endpoints # Get all graphs and endpoint, and mark as selected the used one query = SparqlQuery(current_app, session) graphs, endpoints = query.get_graphs_and_endpoints( selected_graphs=graphs, selected_endpoints=endpoints) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'graphState': {}, 'sparqlQuery': "", 'graphs': [], 'endpoints': [], 'diskSpace': 0, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'graphState': graph_state, 'sparqlQuery': sparql_query, 'graphs': graphs, 'endpoints': endpoints, 'diskSpace': disk_space, 'error': False, 'errorMessage': '' })
def upload_chunk(): """Upload a file chunk Returns ------- json path: name of the local file. To append the next chunk into it error: True if error, else False errorMessage: the error message of error, else an empty string """ files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"]["quota"]: return jsonify({ 'errorMessage': "Exceeded quota", "path": '', "error": True }), 400 data = request.get_json() if not (data and all([ key in data for key in ["first", "last", "size", "name", "type", "size", "chunk"] ])): return jsonify({ "path": '', "error": True, "errorMessage": "Missing parameters" }), 400 if not (data["first"] or data.get("path")): return jsonify({ "path": '', "error": True, "errorMessage": "Missing path parameter" }), 400 try: files = FilesHandler(current_app, session) path = files.persist_chunk(data) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ "path": '', "error": True, "errorMessage": str(e) }), 500 return jsonify({"path": path, "error": False, "errorMessage": ""})
def upload_url(): """Upload a distant file with an URL Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"]["quota"]: return jsonify({'errorMessage': "Exceeded quota", "error": True}), 400 data = request.get_json() if not (data and data.get("url")): return jsonify({ "error": True, "errorMessage": "Missing url parameter" }), 400 try: if session["user"]["quota"] > 0: with requests.get(data["url"], stream=True) as r: # Check header for total size, and check quota. if r.headers.get('Content-length'): total_size = int( r.headers.get('Content-length')) + disk_space if total_size >= session["user"]["quota"]: return jsonify({ 'errorMessage': "File will exceed quota", "error": True }), 400 session_dict = {'user': session['user']} current_app.celery.send_task('download_file', (session_dict, data["url"])) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({"error": True, "errorMessage": str(e)}), 500 return jsonify({"error": False, "errorMessage": ""})
def init(): """Get the default sparql query Returns ------- json """ try: # Disk space files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None # Get graphs and endpoints query = SparqlQuery(current_app, session) graphs, endpoints = query.get_graphs_and_endpoints(all_selected=True) # Default query default_query = query.prefix_query(query.get_default_query()) console_enabled = can_access(session['user']) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ "error": True, "errorMessage": str(e), "defaultQuery": "", "graphs": {}, "endpoints": {}, "diskSpace": None, "console_enabled": False }), 500 return jsonify({ "error": False, "errorMessage": "", "defaultQuery": default_query, "graphs": graphs, "endpoints": endpoints, "diskSpace": disk_space, "console_enabled": console_enabled })
def upload_datasets(): """Download a galaxy datasets into AskOmics Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user() if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"]["quota"]: return jsonify({ 'errorMessage': "Exceeded quota", "error": True }), 400 data = request.get_json() if not (data and data.get("datasets_id")): return jsonify({ 'error': True, 'errorMessage': "Missing datasets_id parameter" }), 400 datasets_id = data["datasets_id"] try: galaxy = Galaxy(current_app, session) galaxy.download_datasets(datasets_id) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'error': False, 'errorMessage': '' })
def save_query(): """Perform a sparql query Returns ------- json query results """ query = request.get_json()['query'] graphs = request.get_json()['graphs'] endpoints = request.get_json()['endpoints'] local_endpoint_f = current_app.iniconfig.get('triplestore', 'endpoint') try: local_endpoint_f = current_app.iniconfig.get('federation', 'local_endpoint') except Exception: pass # No graph selected in local TS if not graphs and local_endpoint_f in endpoints: return jsonify({ 'error': True, 'errorMessage': "No graph selected in local triplestore", 'task_id': None }), 500 # No endpoint selected if not endpoints: return jsonify({ 'error': True, 'errorMessage': "No endpoint selected", 'task_id': None }), 500 try: files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"][ "quota"]: return jsonify({ 'error': True, 'errorMessage': "Exceeded quota", 'task_id': None }), 500 # Is query federated? query_builder = SparqlQueryBuilder(current_app, session) query_builder.set_graphs_and_endpoints(graphs=graphs, endpoints=endpoints) federated = query_builder.is_federated() replace_froms = query_builder.replace_froms() formatted_query = query_builder.format_query( query, limit=None, replace_froms=replace_froms, federated=federated) info = { "sparql_query": query, # Store the non formatted query in db "graphs": graphs, "endpoints": endpoints, "federated": federated, "celery_id": None } result = Result(current_app, session, info) info["id"] = result.save_in_db() # execute the formatted query info["sparql_query"] = formatted_query session_dict = {"user": session["user"]} task = current_app.celery.send_task("sparql_query", (session_dict, info)) result.update_celery(task.id) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': str(e), 'task_id': None }), 500 return jsonify({'error': False, 'errorMessage': '', 'task_id': task.id})
def save_query(): """Perform a sparql query Returns ------- json query results """ if not can_access(session['user']): return jsonify({"error": True, "errorMessage": "Admin required"}), 401 data = request.get_json() if not (data and data.get("query")): return jsonify({ 'error': True, 'errorMessage': "Missing query parameter", 'header': [], 'data': [] }), 400 q = data['query'] graphs = data.get('graphs', []) endpoints = data.get('endpoints', []) local_endpoint_f = current_app.iniconfig.get('triplestore', 'endpoint') try: local_endpoint_f = current_app.iniconfig.get('federation', 'local_endpoint') except Exception: pass # No graph selected in local TS if not graphs and local_endpoint_f in endpoints: return jsonify({ 'error': True, 'errorMessage': "No graph selected in local triplestore", 'result_id': None }), 400 # No endpoint selected if not endpoints: return jsonify({ 'error': True, 'errorMessage': "No endpoint selected", 'result_id': None }), 400 try: files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"][ "quota"]: return jsonify({ 'error': True, 'errorMessage': "Exceeded quota", 'result_id': None }), 400 # Is query federated? query = SparqlQuery(current_app, session, get_graphs=False) query.set_graphs_and_endpoints(graphs=graphs, endpoints=endpoints) federated = query.is_federated() replace_froms = query.replace_froms() formatted_query = query.format_query(q, limit=None, replace_froms=replace_froms, federated=federated) info = { "sparql_query": q, # Store the non formatted query in db "graphs": graphs, "endpoints": endpoints, "federated": federated, "celery_id": None } result = Result(current_app, session, info) info["id"] = result.save_in_db() # execute the formatted query info["sparql_query"] = formatted_query session_dict = {"user": session["user"]} task = current_app.celery.send_task("sparql_query", (session_dict, info)) result.update_celery(task.id) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': str(e), 'result_id': None }), 500 return jsonify({ 'error': False, 'errorMessage': '', 'result_id': info["id"] })
def get_graph_and_sparql_query(): """Get query (graphState or Sparql) Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ try: data = request.get_json() if not (data and data.get("fileId")): return jsonify({ 'graphState': {}, 'sparqlQuery': "", 'graphs': [], 'endpoints': [], 'diskSpace': 0, 'error': True, 'errorMessage': "Missing fileId parameter" }), 400 file_id = data["fileId"] result_info = {"id": file_id} result = Result(current_app, session, result_info) if not result: return jsonify({ 'graphState': {}, 'sparqlQuery': "", 'graphs': [], 'endpoints': [], 'diskSpace': 0, 'error': True, 'errorMessage': "You do not have access to this query" }), 401 # Get graph state and sparql query graph_state = result.get_graph_state(formated=True) sparql_query = result.get_sparql_query() # Get disk space files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None # Get graphs and endpoints graphs = result.graphs endpoints = result.endpoints # Get all graphs and endpoint, and mark as selected the used one query = SparqlQuery(current_app, session) graphs, endpoints = query.get_graphs_and_endpoints( selected_graphs=graphs, selected_endpoints=endpoints) console_enabled = can_access(session['user']) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'graphState': {}, 'sparqlQuery': "", 'graphs': [], 'endpoints': [], 'diskSpace': 0, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'graphState': graph_state, 'sparqlQuery': sparql_query, 'graphs': graphs, 'endpoints': endpoints, 'diskSpace': disk_space, 'error': False, 'errorMessage': '', 'console_enabled': console_enabled })
def save_result(): """Save a query in filesystem and db, using a celery task Returns ------- json result_id: result id error: True if error, else False errorMessage: the error message of error, else an empty string """ try: files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user() if "user" in session else None if session["user"]["quota"] > 0 and disk_space >= session["user"]["quota"]: return jsonify({ 'error': True, 'errorMessage': "Exceeded quota", 'result_id': None }), 400 # Get query and endpoints and graphs of the query data = request.get_json() if not (data and data.get("graphState")): return jsonify({ 'result_id': None, 'error': True, 'errorMessage': "Missing graphState parameter" }), 400 query = SparqlQuery(current_app, session, data["graphState"], get_graphs=False) query.build_query_from_json(preview=False, for_editor=False) federated = query.is_federated() info = { "graph_state": data["graphState"], "query": query.sparql, "graphs": query.graphs, "endpoints": query.endpoints, "federated": federated, "celery_id": None, "selects": query.selects, } result = Result(current_app, session, info) info["id"] = result.save_in_db() session_dict = {"user": session["user"]} task = current_app.celery.send_task("query", (session_dict, info)) result.update_celery(task.id) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': str(e), 'result_id': None }), 500 return jsonify({ 'error': False, 'errorMessage': '', 'result_id': info["id"] })