<commit_before># frozen_string_literal: true

require 'concurrent'
require 'date'
require 'travis/logs'

module Travis
  module Logs
    module Services
      class SendTimings
        attr_reader :log_id, :database

        private :database

        TIMER_START = /travis_time:start:(?<timer_id>[0-9a-f]+)/
        TIMER_END = /travis_time:end:(?<timer_id>[0-9a-f]+):(?<info>[^\r]+)\r/

        def self.run
          new.run
        end

        def self.send_timings(log_id)
          new.send_timings(log_id)
        end

        def initialize(log_id, database: Travis::Logs.database_connection)
          @log_id   = log_id
          @database = database
        end

        def run
          parsable_log_ids.each do |log_id|
            send_timings log_id
          end
        end

        def send_timings(log_id)
          timer_stack = []

          content.each_line do |l|
            l.scan(/#{TIMER_START}|#{TIMER_END}/) do |start_timer_id, end_timer_id, info|

              if start_timer_id
                timer_stack << start_timer_id
                next
              end

              if timer_stack.empty?
                next
              end
              unless (last_timer_id = timer_stack.pop) == end_timer_id
                next
              end

              # matched TIMER_END regexp, so we have `end_timer_id` and `info` defined

              marker_data = parse_marker_data(info)

              next unless marker_data.key?(:duration)

              # duration is given by nanoseconds
              duration_ms = marker_data.delete(:duration).to_i / (10**6)

              event = {
                duration_ms: duration_ms,
                log_id: log_id,
                cmd_start_time: DateTime.strptime(start_timer_id[0..-10], '%s'), # drop last 9 digits to create time in seconds
                cmd_end_time:   DateTime.strptime(end_timer_id[0..-10],   '%s'), # drop last 9 digits to create time in seconds
              }.merge(marker_data)

              Travis::Honeycomb.send(event) # but this is not the right client…
            end
          end
        end

        def log
          @log ||= begin
            log = database.log_for_id(log_id)
            unless log
              Travis.logger.warn(
                'log not found',
                action: 'archive', id: log_id, result: 'not_found'<commit_msg>Log event data instead of sending it<commit_after># frozen_string_literal: true

require 'concurrent'
require 'date'
require 'travis/logs'

module Travis
  module Logs
    module Services
      class SendTimings
        attr_reader :log_id, :database

        private :database

        TIMER_START = /travis_time:start:(?<timer_id>[0-9a-f]+)/
        TIMER_END = /travis_time:end:(?<timer_id>[0-9a-f]+):(?<info>[^\r]+)\r/

        def self.run
          new.run
        end

        def self.send_timings(log_id)
          new.send_timings(log_id)
        end

        def initialize(log_id, database: Travis::Logs.database_connection)
          @log_id   = log_id
          @database = database
        end

        def run
          parsable_log_ids.each do |log_id|
            send_timings log_id
          end
        end

        def send_timings(log_id)
          timer_stack = []

          content.each_line do |l|
            l.scan(/#{TIMER_START}|#{TIMER_END}/) do |start_timer_id, end_timer_id, info|

              if start_timer_id
                timer_stack << start_timer_id
                next
              end

              if timer_stack.empty?
                next
              end
              unless (last_timer_id = timer_stack.pop) == end_timer_id
                next
              end

              # matched TIMER_END regexp, so we have `end_timer_id` and `info` defined

              marker_data = parse_marker_data(info)

              next unless marker_data.key?(:duration)

              # duration is given by nanoseconds
              duration_ms = marker_data.delete(:duration).to_i / (10**6)

              event = {
                duration_ms: duration_ms,
                log_id: log_id,
                cmd_start_time: DateTime.strptime(start_timer_id[0..-10], '%s'), # drop last 9 digits to create time in seconds
                cmd_end_time:   DateTime.strptime(end_timer_id[0..-10],   '%s'), # drop last 9 digits to create time in seconds
              }.merge(marker_data)

              #Travis::Honeycomb.send(event) # but this is not the right client…
              Travis.logger.info event
            end
          end
        end

        def log
          @log ||= begin
            log = database.log_for_id(log_id)
            unless log
              Travis.logger.warn(
                'log not found',
                action: 'archive', id: log_id, result: 'not_found'
{'fraction_non_alphanumeric': 0.08799667152069898, 'fraction_numerical': 0.006240898689411275, 'mean_word_length': 1.6116241173275394, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '2965485', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1405, 'n_words': 329}