<commit_before>require 'socket'
require 'appsignal/transaction/transaction_formatter'

module Appsignal
  class Transaction
    def self.create(key, env)
      Thread.current[:appsignal_transaction_id] = key
      Appsignal.transactions[key] = Appsignal::Transaction.new(key, env)
    end

    def self.current
      Appsignal.transactions[Thread.current[:appsignal_transaction_id]]
    end

    attr_reader :id, :events, :process_action_event, :action, :exception, :env

    def initialize(id, env)
      @id = id
      @events = []
      @process_action_event = nil
      @exception = nil
      @env = env
    end

    def request
      ActionDispatch::Request.new(@env)
    end

    def set_process_action_event(event)
      @process_action_event = event
      if event && event.payload
        @action = "#{event.payload[:controller]}##{event.payload[:action]}"
      end
    end

    def add_event(event)
      @events << event
    end

    def add_exception(ex)
      @exception = ex
    end

    def exception?
      !!exception
    end

    def slow_request?
      return false unless process_action_event && process_action_event.payload
      Appsignal.config[:slow_request_threshold] <=
        process_action_event.duration
    end

    def slower?(transaction)
      process_action_event.duration > transaction.process_action_event.duration
    end

    def truncate!
      @process_action_event.payload.clear
      @events.clear
    end

    def convert_values_to_primitives!
      Appsignal::ParamsSanitizer.sanitize!(@process_action_event.payload)
      @events.each do |event|
        Appsignal::ParamsSanitizer.sanitize!(event.payload)
      end
    end

    def type
      return :exception if exception?
      return :slow_request if slow_request?
      :regular_request
    end

    def to_hash
      if exception?
        TransactionFormatter.faulty(self)
      elsif slow_request?
        TransactionFormatter.slow(self)
      else
        TransactionFormatter.regular(self)
      end.to_hash
    end<commit_msg>Inline short statement<commit_after>require 'socket'
require 'appsignal/transaction/transaction_formatter'

module Appsignal
  class Transaction
    def self.create(key, env)
      Thread.current[:appsignal_transaction_id] = key
      Appsignal.transactions[key] = Appsignal::Transaction.new(key, env)
    end

    def self.current
      Appsignal.transactions[Thread.current[:appsignal_transaction_id]]
    end

    attr_reader :id, :events, :process_action_event, :action, :exception, :env

    def initialize(id, env)
      @id = id
      @events = []
      @process_action_event = nil
      @exception = nil
      @env = env
    end

    def request
      ActionDispatch::Request.new(@env)
    end

    def set_process_action_event(event)
      @process_action_event = event
      if event && event.payload
        @action = "#{event.payload[:controller]}##{event.payload[:action]}"
      end
    end

    def add_event(event)
      @events << event
    end

    def add_exception(ex)
      @exception = ex
    end

    def exception?
      !!exception
    end

    def slow_request?
      return false unless process_action_event && process_action_event.payload
      Appsignal.config[:slow_request_threshold] <=
        process_action_event.duration
    end

    def slower?(transaction)
      process_action_event.duration > transaction.process_action_event.duration
    end

    def truncate!
      @process_action_event.payload.clear
      @events.clear
    end

    def convert_values_to_primitives!
      Appsignal::ParamsSanitizer.sanitize!(@process_action_event.payload)
      @events.each { |o| Appsignal::ParamsSanitizer.sanitize!(o.payload) }
    end

    def type
      return :exception if exception?
      return :slow_request if slow_request?
      :regular_request
    end

    def to_hash
      if exception?
        TransactionFormatter.faulty(self)
      elsif slow_request?
        TransactionFormatter.slow(self)
      else
        TransactionFormatter.regular(self)
      end.to_hash
    end
{'fraction_non_alphanumeric': 0.08292201382033564, 'fraction_numerical': 0.0, 'mean_word_length': 2.863679694947569, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '1180684', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1157, 'n_words': 245}