Beispiel #1
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))
Beispiel #2
0
 def _get_statement_execution_download_url(self,
                                           statement_execution_id: int):
     statement_execution = logic.get_statement_execution_by_id(
         statement_execution_id)
     if statement_execution.result_path:
         with GenericReader(statement_execution.result_path) as reader:
             if reader.has_download_url:
                 return reader.get_download_url()
     return None
    def get_resource_path(self, session=None):
        statement_execution = get_statement_execution_by_id(
            self.statement_execution_id, False, session=session)
        with GenericReader(statement_execution.result_path) as reader:
            store_type = reader.store_type
            resource_path = reader.uri
            resource_type = STORE_TYPE_TO_RESOURCE_TYPE.get(store_type, None)
            if resource_type:
                return [resource_type, resource_path]

        return [None, None]
Beispiel #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
Beispiel #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
Beispiel #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))
Beispiel #7
0
 def _get_statement_execution_result(
     self,
     statement_execution_id: int,
     raw: bool = False,  # If raw, return unparsed csv text
     number_of_lines: int = 2001,
 ):
     statement_execution = logic.get_statement_execution_by_id(
         statement_execution_id)
     if statement_execution.result_path:
         with GenericReader(statement_execution.result_path) as reader:
             if raw:
                 result = "\n".join(
                     reader.read_lines(number_of_lines=number_of_lines))
             else:
                 result = reader.read_csv(number_of_lines=number_of_lines)
             return result
     return None
Beispiel #8
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))