示例#1
0
def export_statement_execution_result(statement_execution_id,
                                      exporter_name,
                                      exporter_params=None):
    with DBSession() as session:
        statement_execution = logic.get_statement_execution_by_id(
            statement_execution_id, session=session)
        api_assert(statement_execution is not None,
                   message="Invalid statement execution")
        verify_query_execution_permission(
            statement_execution.query_execution_id, session=session)

    exporter = get_exporter(exporter_name)
    api_assert(exporter is not None, f"Invalid export name {exporter_name}")

    if exporter_params:
        valid, reason = validate_form(exporter.export_form, exporter_params)
        api_assert(valid, "Invalid exporter params, reason: " + reason)

    task = export_query_execution_task.apply_async(args=[
        exporter.exporter_name,
        statement_execution_id,
        current_user.id,
        exporter_params or {},
    ], )

    return task.task_id
示例#2
0
def get_statement_execution_log(statement_execution_id):
    with DBSession() as session:
        statement_execution = logic.get_statement_execution_by_id(
            statement_execution_id, session=session)
        api_assert(statement_execution is not None,
                   message="Invalid statement execution")
        verify_query_execution_permission(
            statement_execution.query_execution_id, session=session)

        log_path = statement_execution.log_path
        try:
            if log_path.startswith("stream"):
                logs = logic.get_statement_execution_stream_logs(
                    statement_execution_id)
                return list(map(lambda log: log.log, logs))
            else:
                with DBSession() as session:
                    MAX_LOG_RETURN_LINES = 2000
                    result = ""

                    statement_execution = logic.get_statement_execution_by_id(
                        statement_execution_id, session=session)
                    if statement_execution is not None and statement_execution.has_log:
                        with GenericReader(
                                statement_execution.log_path) as reader:
                            result = reader.read_lines(
                                number_of_lines=MAX_LOG_RETURN_LINES)
                            if len(result) == MAX_LOG_RETURN_LINES:
                                result += [
                                    "---------------------------------------------------------------------------",
                                    f"We are truncating results since it reached limit of {MAX_LOG_RETURN_LINES} lines.",
                                ]
                            return result
        except FileDoesNotExist as e:
            abort(RESOURCE_NOT_FOUND_STATUS_CODE, str(e))
示例#3
0
def create_query_execution_notification(query_id,):
    with DBSession() as session:
        verify_query_execution_permission(query_id, session=session)

        return logic.create_query_execution_notification(
            query_execution_id=query_id, uid=current_user.id, session=session
        )
示例#4
0
def download_statement_execution_result(statement_execution_id):
    with DBSession() as session:
        statement_execution = logic.get_statement_execution_by_id(
            statement_execution_id, session=session)
        api_assert(statement_execution is not None,
                   message="Invalid statement execution")
        verify_query_execution_permission(
            statement_execution.query_execution_id, session=session)

        download_file_name = f"result_{statement_execution.query_execution_id}_{statement_execution_id}.csv"

        reader = GenericReader(statement_execution.result_path)
        response = None
        if reader.has_download_url:
            # If the Reader can generate a download,
            # we let user download the file by redirection
            download_url = reader.get_download_url(
                custom_name=download_file_name)
            response = redirect(download_url)
        else:
            # We read the raw file and download it for the user
            reader.start()
            raw = reader.read_raw()
            response = Response(raw)
            response.headers["Content-Type"] = "text/csv"
            response.headers[
                "Content-Disposition"] = f'attachment; filename="{download_file_name}"'
        return response
示例#5
0
def download_statement_execution_result(statement_execution_id):
    with DBSession() as session:
        statement_execution = logic.get_statement_execution_by_id(
            statement_execution_id, session=session)
        api_assert(statement_execution is not None,
                   message="Invalid statement execution")
        verify_query_execution_permission(
            statement_execution.query_execution_id, session=session)

        reader = GenericReader(statement_execution.result_path)
        response = None
        if reader.has_download_url:
            # If the Reader can generate a download,
            # we proxy download the file
            download_url = reader.get_download_url()
            req = requests.get(download_url, stream=True)

            # 10 KB size
            response = Response(req.iter_content(chunk_size=10 * 1024))
        else:
            # We read the raw file and download it for the user
            reader.start()
            raw = reader.read_raw()
            response = Response(raw)
        response.headers["Content-Type"] = "text/plain"
        response.headers[
            "Content-Disposition"] = 'attachment; filename="result_{}_{}.csv"'.format(
                statement_execution.query_execution_id, statement_execution_id)
        return response
示例#6
0
def get_statement_execution_result(statement_execution_id):
    with DBSession() as session:
        try:
            statement_execution = logic.get_statement_execution_by_id(
                statement_execution_id, session=session)
            api_assert(statement_execution is not None,
                       message="Invalid statement execution")
            verify_query_execution_permission(
                statement_execution.query_execution_id, session=session)

            with GenericReader(statement_execution.result_path) as reader:
                result = reader.read_csv(number_of_lines=2001)
                return result
        except FileDoesNotExist as e:
            abort(RESOURCE_NOT_FOUND_STATUS_CODE, str(e))
示例#7
0
def get_statement_execution_result(statement_execution_id, limit=None):
    # TODO: make this customizable
    limit = 1000 if limit is None else limit
    api_assert(limit <= 5000, message="Too many rows requested")

    with DBSession() as session:
        try:
            statement_execution = logic.get_statement_execution_by_id(
                statement_execution_id, session=session)
            api_assert(statement_execution is not None,
                       message="Invalid statement execution")
            verify_query_execution_permission(
                statement_execution.query_execution_id, session=session)

            with GenericReader(statement_execution.result_path) as reader:
                result = reader.read_csv(number_of_lines=limit +
                                         1)  # 1 row for column
                return result
        except FileDoesNotExist as e:
            abort(RESOURCE_NOT_FOUND_STATUS_CODE, str(e))
示例#8
0
def get_datadoc_ids_by_query_execution(query_execution_id):
    with DBSession() as session:
        verify_query_execution_permission(query_execution_id, session=session)
        pair_id = next(
            iter(
                logic.get_datadoc_id_from_query_execution_id(
                    query_execution_id, session=session)),
            None,
        )
        if pair_id is None:
            return None

        doc_id, cell_id = pair_id
        cell_title = None

        if user_can_read(doc_id, current_user.id, session=session):
            cell_info = datadoc_logic.get_data_cell_by_id(cell_id,
                                                          session=session)
            if cell_info:
                cell_title = cell_info.meta.get("title")

        return {"doc_id": doc_id, "cell_id": cell_id, "cell_title": cell_title}
示例#9
0
def delete_query_execution_notification(query_id, ):
    with DBSession() as session:
        verify_query_execution_permission(query_id, session=session)
        logic.delete_query_execution_notification(query_execution_id=query_id,
                                                  uid=current_user.id,
                                                  session=session)
示例#10
0
def get_query_execution_error(query_execution_id):
    with DBSession() as session:
        verify_query_execution_permission(query_execution_id, session=session)
        return logic.get_query_execution_error(query_execution_id,
                                               session=session)
示例#11
0
def get_query_execution(query_execution_id):
    verify_query_execution_permission(query_execution_id)
    execution = logic.get_query_execution_by_id(query_execution_id)
    execution_dict = execution.to_dict(True) if execution is not None else None
    return execution_dict
示例#12
0
def verify_import_config_permissions(import_config: Dict):
    if "query_execution_id" in import_config:
        query_execution_id = import_config["query_execution_id"]
        verify_query_execution_permission(query_execution_id)