示例#1
0
def save_query():
    """Perform a sparql query

    Returns
    -------
    json
        query results
    """
    q = 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 = SparqlQuery(current_app, session)
        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),
            'task_id': None
        }), 500

    return jsonify({
        'error': False,
        'errorMessage': '',
        'task_id': task.id
    })
示例#2
0
def query():
    """Perform a sparql query

    Returns
    -------
    json
        query results
    """
    q = 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",
            'header': [],
            'data': []
        }), 500

    # No endpoint selected
    if not endpoints:
        return jsonify({
            'error': True,
            'errorMessage': "No endpoint selected",
            'header': [],
            'data': []
        }), 500

    try:
        query = SparqlQuery(current_app, session)

        query.set_graphs_and_endpoints(graphs=graphs, endpoints=endpoints)

        federated = query.is_federated()
        replace_froms = query.replace_froms()

        sparql = query.format_query(q, replace_froms=replace_froms, federated=federated)
        # header, data = query_launcher.process_query(query)
        header = query.selects
        data = []
        if query.graphs or query.endpoints:
            query_launcher = SparqlQueryLauncher(current_app, session, get_result_query=True, federated=federated, endpoints=endpoints)
            header, data = query_launcher.process_query(sparql)

    except Exception as e:
        current_app.logger.error(str(e).replace('\\n', '\n'))
        traceback.print_exc(file=sys.stdout)
        return jsonify({
            'error': True,
            'errorMessage': str(e).replace('\\n', '\n'),
            'header': [],
            'data': []
        }), 500

    return jsonify({
        'header': header,
        'data': data
    })
示例#3
0
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"]
    })