<commit_before># frozen_string_literal: true

require 'pusher'
require 'multi_json'

require 'travis/logs'

module Travis
  module Logs
    class Pusher
      def initialize(pusher_client = nil)
        @pusher_client = pusher_client || default_client
      end

      def push(payload)
        pusher_channel(payload).trigger('job:log', pusher_payload(payload))
        if payload['queued_at']
          # TODO: deprecate this code path in favour of 'meta' below
          # https://github.com/travis-ci/reliability/issues/190
          elapsed = Time.now - Time.parse(payload['queued_at'])
          Metriks.timer('logs.time_to_first_log_line.pusher').update(elapsed)
        elsif payload['meta']
          meta = payload['meta']
          elapsed = Time.now - Time.parse(meta['queued_at'])
          Metriks.timer('logs.time_to_first_log_line.pusher').update(elapsed)
          Metriks.timer("logs.time_to_first_log_line.infra.#{meta['infra']}.pusher").update(elapsed)
          Metriks.timer("logs.time_to_first_log_line.queue.#{meta['queue']}.pusher").update(elapsed)
        end
      end

      def pusher_channel_name(payload)
        "#{secure? ? 'private-' : ''}job-#{payload['id']}"
      end

      def webhook(request)
        @pusher_client.webhook(request)
      end

      private def pusher_channel(payload)
        @pusher_client[pusher_channel_name(payload)]
      end

      private def pusher_payload(payload)
        MultiJson.dump('id' => payload['id'],
                       '_log' => payload['chars'],<commit_msg>track time to first log line in honeycomb<commit_after># frozen_string_literal: true

require 'pusher'
require 'multi_json'

require 'travis/logs'

module Travis
  module Logs
    class Pusher
      def initialize(pusher_client = nil)
        @pusher_client = pusher_client || default_client
      end

      def push(payload)
        pusher_channel(payload).trigger('job:log', pusher_payload(payload))
        if payload['queued_at']
          # TODO: deprecate this code path in favour of 'meta' below
          # https://github.com/travis-ci/reliability/issues/190
          elapsed = Time.now - Time.parse(payload['queued_at'])
          Metriks.timer('logs.time_to_first_log_line.pusher').update(elapsed)
        elsif payload['meta']
          meta = payload['meta']
          elapsed = Time.now - Time.parse(meta['queued_at'])
          Metriks.timer('logs.time_to_first_log_line.pusher').update(elapsed)
          Metriks.timer("logs.time_to_first_log_line.infra.#{meta['infra']}.pusher").update(elapsed)
          Metriks.timer("logs.time_to_first_log_line.queue.#{meta['queue']}.pusher").update(elapsed)
          Travis::Honeycomb.context.merge({
            time_to_first_log_line_pusher_ms: elapsed * 1000,
            infra: meta['infra'],
            queue: meta['queue'],
          })
        end
      end

      def pusher_channel_name(payload)
        "#{secure? ? 'private-' : ''}job-#{payload['id']}"
      end

      def webhook(request)
        @pusher_client.webhook(request)
      end

      private def pusher_channel(payload)
        @pusher_client[pusher_channel_name(payload)]
      end

      private def pusher_payload(payload)
        MultiJson.dump('id' => payload['id'],
                       '_log' => payload['chars'],
{'fraction_non_alphanumeric': 0.12534142640364188, 'fraction_numerical': 0.0030349013657056147, 'mean_word_length': 2.8060046189376444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1748533', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1050, 'n_words': 168}