Пример #1
0
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 = SparqlQuery(app, session, info["graph_state"])

        query.build_query_from_json(for_editor=False)

        headers = query.selects
        results = []
        if query.graphs:
            query_launcher = SparqlQueryLauncher(app,
                                                 session,
                                                 get_result_query=True,
                                                 federated=query.federated,
                                                 endpoints=query.endpoints)
            headers, results = query_launcher.process_query(query.sparql,
                                                            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': ''}
Пример #2
0
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': ''
    })
Пример #3
0
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
        query = SparqlQuery(current_app, session, request.get_json()["graphState"])
        query.build_query_from_json(preview=False, for_editor=False)

        info = {
            "graph_state": query.json,
            "query": query.sparql,
            "graphs": query.graphs,
            "endpoints": query.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
    })
Пример #4
0
    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
        }
Пример #5
0
def get_preview():
    """Get a preview of query

    Returns
    -------
    json
        resultsPreview: Preview of the query results
        headerPreview: Header of the results table
        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"):
            preview = []
            header = []
        else:
            data = request.get_json()
            if not (data and data.get("graphState")):
                return jsonify({
                    'resultsPreview': [],
                    'headerPreview': [],
                    'error': True,
                    'errorMessage': "Missing graphState parameter"
                }), 400

            query = SparqlQuery(current_app, session, data["graphState"], get_graphs=False)
            query.build_query_from_json(preview=True, for_editor=False)

            endpoints = query.endpoints
            federated = query.federated

            header = query.selects
            preview = []
            if query.graphs:
                query_launcher = SparqlQueryLauncher(current_app, session, get_result_query=True, federated=federated, endpoints=endpoints)
                header, preview = query_launcher.process_query(query.sparql)

    except Exception as e:
        traceback.print_exc(file=sys.stdout)
        return jsonify({
            'resultsPreview': [],
            'headerPreview': [],
            'error': True,
            'errorMessage': str(e)
        }), 500
    return jsonify({
        'resultsPreview': preview,
        'headerPreview': header,
        'error': False,
        'errorMessage': ''
    })