def get_graph_state(): """Summary Returns ------- json preview: list of result preview header: result header 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) graph_state = result.get_graph_state(formated=True) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'graphState': {}, 'id': file_id, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'graphState': graph_state, 'id': file_id, 'error': False, 'errorMessage': '' })
def togle_public_query(): """Publish a query template from a result Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ try: json = request.get_json() result_info = {"id": json["queryId"]} result = Result(current_app, session, result_info) result.publish_query(json["newStatus"], admin=True) results_handler = ResultsHandler(current_app, session) public_queries = results_handler.get_admin_public_queries() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'queries': [], 'error': True, 'errorMessage': 'Failed to publish query: \n{}'.format(str(e)) }), 500 return jsonify({ 'queries': public_queries, 'error': False, 'errorMessage': '' })
def get_preview(): """Summary Returns ------- json preview: list of result preview header: result header 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) headers, preview = result.get_file_preview() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'preview': [], 'header': [], 'id': file_id, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'preview': preview, 'header': headers, 'id': file_id, 'error': False, 'errorMessage': '' })
def download_result(): """Download result file""" try: data = request.get_json() if not (data and data.get("fileId")): return jsonify({ '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({ 'error': True, 'errorMessage': "You do not have access to this result" }), 401 dir_path = result.get_dir_path() file_name = result.get_file_name() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({'error': True, 'errorMessage': str(e)}), 500 return (send_from_directory(dir_path, file_name))
def set_description(): """Update a result description Returns ------- json files: all files error: True if error, else False errorMessage: the error message of error, else an empty string """ try: json = request.get_json() result_info = {"id": json["id"]} new_desc = json["newDesc"] result = Result(current_app, session, result_info) result.update_description(new_desc) results_handler = ResultsHandler(current_app, session) files = results_handler.get_files_info() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'files': [], 'error': True, 'errorMessage': str(e) }), 500 return jsonify({'files': files, 'error': False, 'errorMessage': ''})
def template_query(): """Template a query from a result Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ try: json = request.get_json() result_info = {"id": json["id"]} result = Result(current_app, session, result_info) result.template_query(json["template"]) results_handler = ResultsHandler(current_app, session) files = results_handler.get_files_info() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'files': [], 'error': True, 'errorMessage': 'Failed to publish query: \n{}'.format(str(e)) }), 500 return jsonify({'files': files, 'error': False, 'errorMessage': ''})
def get_preview(): """Summary Returns ------- json preview: list of result preview header: result header 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({ 'preview': [], 'header': [], 'id': None, 'error': True, 'errorMessage': "Missing file Id" }), 400 file_id = data["fileId"] result_info = {"id": file_id} result = Result(current_app, session, result_info) if not result: return jsonify({ 'preview': [], 'header': [], 'id': file_id, 'error': True, 'errorMessage': "You do not have access to this query" }), 401 headers, preview = result.get_file_preview() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'preview': [], 'header': [], 'id': file_id, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'preview': preview, 'header': headers, 'id': file_id, 'error': False, 'errorMessage': '' })
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_state(): """Summary Returns ------- json preview: list of result preview header: result header 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': {}, 'id': None, 'error': True, 'errorMessage': "Missing fileId parameter" }), 400 file_id = data["fileId"] formated = data.get("formated", True) result_info = {"id": file_id} result = Result(current_app, session, result_info) if not result: return jsonify({ 'graphState': {}, 'id': file_id, 'error': True, 'errorMessage': "You do not have access to this graph" }), 401 graph_state = result.get_graph_state(formated=formated) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'graphState': {}, 'id': file_id, 'error': True, 'errorMessage': str(e) }), 500 return jsonify({ 'graphState': graph_state, 'id': file_id, 'error': False, 'errorMessage': '' })
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 download_result(): """Download result file""" try: file_id = request.get_json()["fileId"] result_info = {"id": file_id} result = Result(current_app, session, result_info) dir_path = result.get_dir_path() file_name = result.get_file_name() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({'error': True, 'errorMessage': str(e)}), 500 return (send_from_directory(dir_path, file_name))
def form_query(): """Create a form from a result 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("id")): return jsonify({ 'files': [], 'error': True, 'errorMessage': "Missing id parameter" }), 400 result_info = {"id": data["id"]} result = Result(current_app, session, result_info) if not result: return jsonify({ 'files': [], 'error': True, 'errorMessage': 'Failed to publish query: \n{}'.format( "You do not have access to this query") }), 401 result.form_query(data.get("form", False)) results_handler = ResultsHandler(current_app, session) files = results_handler.get_files_info() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'files': [], 'error': True, 'errorMessage': 'Failed to create form template query: \n{}'.format(str(e)) }), 500 return jsonify({'files': files, 'error': False, 'errorMessage': ''})
def set_description(): """Update a result description Returns ------- json files: all files 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("id") and data.get("newDesc")): return jsonify({ 'files': [], 'error': True, 'errorMessage': "Missing parameters" }), 400 result_info = {"id": data["id"]} new_desc = data["newDesc"] result = Result(current_app, session, result_info) if not result: return jsonify({ 'files': [], 'error': True, 'errorMessage': "You do not have access to this result" }), 500 result.update_description(new_desc) results_handler = ResultsHandler(current_app, session) files = results_handler.get_files_info() except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'files': [], 'error': True, 'errorMessage': str(e) }), 500 return jsonify({'files': files, 'error': False, 'errorMessage': ''})
def delete_results(self, files_id): """Delete files Parameters ---------- files_id : list list of file id to delete Returns ------- list list of remaining files """ for file_id in files_id: result = Result(self.app, self.session, {"id": file_id}) self.app.celery.control.revoke(result.celery_id, terminate=True) result.delete_result() return self.get_files_info()
def save_form(): """Update a form Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ try: # Get query and endpoints and graphs of the query data = request.get_json() if not (data and data.get("graphState") and data.get("formId")): return jsonify({ 'error': True, 'errorMessage': "Missing graphState or formId parameter" }), 400 result_info = {"id": data["formId"]} result = Result(current_app, session, result_info) if not result: return jsonify({ 'error': True, 'errorMessage': 'Failed to edit form: \n{}'.format( "You do not have access to this form") }), 401 result.update_graph(data.get("graphState")) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': str(e), }), 500 return jsonify({'error': False, 'errorMessage': ''})
def send2galaxy(): """Send a result file into Galaxy 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") and data.get("fileToSend")): return jsonify({ 'error': True, 'errorMessage': "Missing parameters" }), 400 result_info = {"id": data["fileId"]} result = Result(current_app, session, result_info) if not result: return jsonify({ 'error': True, 'errorMessage': 'Failed to publish query: \n{}'.format( "You do not have access to this query") }), 401 result.send2galaxy(data["fileToSend"]) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': 'Failed to publish query: \n{}'.format(str(e)) }), 500 return jsonify({'error': False, 'errorMessage': ''})
def create_result(self, has_form=False): """Create a result entry in db Returns ------- dict Result info """ # Query: transcript concerned by DE and included in QTL if has_form: with open("tests/data/graphState_simple_query_form.json", "r") as file: file_content = file.read() else: with open("tests/data/graphState_simple_query.json", "r") as file: file_content = file.read() json_query = json.loads(file_content) # Get query and endpoints and graphs of the query query = SparqlQuery(self.app, self.session, json_query) query_launcher = SparqlQueryLauncher(self.app, self.session) query.build_query_from_json(preview=False, for_editor=False) info = { "graph_state": json_query, "query": query.sparql, "celery_id": '00000000-0000-0000-0000-000000000000', "graphs": query.graphs, "endpoints": query.endpoints } # Save job in database database result = Result(self.app, self.session, info) result.save_in_db() # Execute query and write result to file headers, results = query_launcher.process_query(query.sparql) file_size = result.save_result_in_file(headers, results) # Update database status result.update_db_status("success", size=file_size) return { "id": result.id, "path": result.file_path, "start": result.start, "end": result.end, "size": file_size }
def send2galaxy(): """Send a result file into Galaxy Returns ------- json error: True if error, else False errorMessage: the error message of error, else an empty string """ try: json = request.get_json() result_info = {"id": json["fileId"]} result = Result(current_app, session, result_info) result.send2galaxy(json["fileToSend"]) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ 'error': True, 'errorMessage': 'Failed to publish query: \n{}'.format(str(e)) }), 500 return jsonify({'error': False, 'errorMessage': ''})
def query(self, session, info): """Save the query results in filesystem and db Parameters ---------- session : dict AskOmics session graph_state : dict JSON graph state Returns ------- dict error: True if error, else False errorMessage: the error message of error, else an empty string """ try: info["celery_id"] = self.request.id result = Result(app, session, info, force_no_db=True) # Save job in database database result.set_celery_id(self.request.id) result.update_db_status("started", update_celery=True, update_date=True) # launch query query_builder = SparqlQueryBuilder(app, session) query = query_builder.build_query_from_json(info["graph_state"], for_editor=False) endpoints = query_builder.endpoints federated = query_builder.federated headers = query_builder.selects results = [] if query_builder.graphs: query_launcher = SparqlQueryLauncher(app, session, get_result_query=True, federated=federated, endpoints=endpoints) headers, results = query_launcher.process_query(query, isql_api=True) # write result to a file file_size = result.save_result_in_file(headers, results) # Update database status result.update_db_status("success", size=file_size) except Exception as e: traceback.print_exc(file=sys.stdout) trace = traceback.format_exc() result.update_db_status("error", error=True, error_message=str(e), traceback=trace) result.rollback() raise e return {'error': True, 'errorMessage': str(e)} return {'error': False, 'errorMessage': ''}
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 publicize_result(self, result_id, public=True): """Publicize a result""" result_info = {"id": result_id} result = Result(self.app, self.session, result_info) result.publish_query(public)
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 sparql_query(self, session, info): """Save the sparql query results in filesystem and db Parameters ---------- session : dict AskOmics session info : dict sparql query Returns ------- dict error: True if error, else False errorMessage: the error message of error, else an empty string """ try: info["celery"] = self.request.id result = Result(app, session, info, force_no_db=True) # Save job in db result.set_celery_id(self.request.id) result.update_db_status("started", update_celery=True) query_launcher = SparqlQueryLauncher(app, session, get_result_query=True, federated=info["federated"], endpoints=info["endpoints"]) header, data = query_launcher.process_query(info["sparql_query"], isql_api=True) # Write results in file file_size = result.save_result_in_file(header, data) # Update database status result.update_db_status("success", size=file_size) except Exception as e: traceback.print_exc(file=sys.stdout) trace = traceback.format_exc() result.update_db_status("error", error=True, error_message=str(e), traceback=trace) raise e return {'error': True, 'errorMessage': str(e)} return {'error': False, 'errorMessage': ''}
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"] })