<commit_before>require 'travis/logs/helpers/metrics'
require 'travis/logs/helpers/s3'
require 'active_support/core_ext/object/try'
require 'uri'

module Travis
  module Logs
    module Services
      class ArchiveLog
        include Helpers::Metrics

        METRIKS_PREFIX = "logs.archive"

        def self.metriks_prefix
          METRIKS_PREFIX
        end

        class VerificationFailed < StandardError
          def initialize(log_id, target_url, expected, actual)
            super("Expected #{target_url} (from log id: #{log_id}) to have the content length #{expected.inspect}, but had #{actual.inspect}")
          end
        end

        attr_reader :log_id

        def initialize(log_id)
          @log_id = log_id
        end

        def run
          return unless fetch
          mark_as_archiving
          check_if_blank
          store
          verify
          confirm
        ensure
          mark_as_archiving(false)
        end

        def log
          @log ||= begin
            log = connection[:logs].where(id: log_id).first
            unless log
              Travis.logger.warn "[warn] log with id:#{payload['id']} could not be found"
              mark('log.not_found')
            end
            log
          end
        end
        alias_method :fetch, :log

        def mark_as_archiving(archiving = true)
          connection[:logs].where(id: log_id).update(archiving: archiving)
        end

        def check_if_blank
          if content.blank?
            Travis.logger.warn "[warn] log with id:#{payload['id']} was blank"
            mark("log.empty")
            content = ""
          end
        end

        def store
          retrying(:store) do
            measure('store') do
              s3.store(content)
            end
          end
        end

        def verify
          retrying(:verify) do
            measure('verify') do
              unless content.bytesize == archived_content_length
                raise VerificationFailed.new(log_id, target_url, expected, actual)
              end
            end<commit_msg>use log_id<commit_after>require 'travis/logs/helpers/metrics'
require 'travis/logs/helpers/s3'
require 'active_support/core_ext/object/try'
require 'uri'

module Travis
  module Logs
    module Services
      class ArchiveLog
        include Helpers::Metrics

        METRIKS_PREFIX = "logs.archive"

        def self.metriks_prefix
          METRIKS_PREFIX
        end

        class VerificationFailed < StandardError
          def initialize(log_id, target_url, expected, actual)
            super("Expected #{target_url} (from log id: #{log_id}) to have the content length #{expected.inspect}, but had #{actual.inspect}")
          end
        end

        attr_reader :log_id

        def initialize(log_id)
          @log_id = log_id
        end

        def run
          return unless fetch
          mark_as_archiving
          check_if_blank
          store
          verify
          confirm
        ensure
          mark_as_archiving(false)
        end

        def log
          @log ||= begin
            log = connection[:logs].where(id: log_id).first
            unless log
              Travis.logger.warn "[warn] log with id:#{log_id} could not be found"
              mark('log.not_found')
            end
            log
          end
        end
        alias_method :fetch, :log

        def mark_as_archiving(archiving = true)
          connection[:logs].where(id: log_id).update(archiving: archiving)
        end

        def check_if_blank
          if content.blank?
            Travis.logger.warn "[warn] log with id:#{log_id} was blank"
            mark("log.empty")
            content = ""
          end
        end

        def store
          retrying(:store) do
            measure('store') do
              s3.store(content)
            end
          end
        end

        def verify
          retrying(:verify) do
            measure('verify') do
              unless content.bytesize == archived_content_length
                raise VerificationFailed.new(log_id, target_url, expected, actual)
              end
            end
{'fraction_non_alphanumeric': 0.07568088696071343, 'fraction_numerical': 0.0009640877319836105, 'mean_word_length': 1.5969962453066333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3818141', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1109, 'n_words': 286}