<commit_before>module Appsignal
  class JSExceptionTransaction
    attr_reader :uuid, :ext

    def initialize(data)
      @data = data
      @uuid = SecureRandom.uuid
      @ext = Appsignal::Extension.start_transaction(@uuid, Appsignal::Transaction::FRONTEND)

      set_action
      set_metadata
      set_error
      set_sample_data
    end

    def set_action
      @ext.set_action(@data['action'])
    end

    def set_metadata
      @ext.set_metadata(
        'path', @data['path']
      ) if @data['path']
    end

    def set_error
      @ext.set_error(
        @data['name'],
        @data['message'],
        Appsignal::Utils.json_generate(@data['backtrace'])
      )
    end

    def set_sample_data
      {
        :params       => @data['params'],
        :environment  => @data['environment'],
        :tags         => @data['tags']
      }.each do |key, data|
        next unless data.is_a?(Array) || data.is_a?(Hash)
        begin
          @ext.set_sample_data(
            key.to_s,
            Appsignal::Utils.json_generate(data)
          )
        rescue *Appsignal::Transaction::JSON_EXCEPTIONS => e
          Appsignal.logger.error("Error generating JSON (#{e.class}: #{e.message}) for '#{backtrace.inspect}'")
        end
      end
    end

    def complete!<commit_msg>Add session_overview to JS transaction data<commit_after>module Appsignal
  class JSExceptionTransaction
    attr_reader :uuid, :ext

    def initialize(data)
      @data = data
      @uuid = SecureRandom.uuid
      @ext = Appsignal::Extension.start_transaction(@uuid, Appsignal::Transaction::FRONTEND)

      set_action
      set_metadata
      set_error
      set_sample_data
    end

    def set_action
      @ext.set_action(@data['action'])
    end

    def set_metadata
      @ext.set_metadata(
        'path', @data['path']
      ) if @data['path']
    end

    def set_error
      @ext.set_error(
        @data['name'],
        @data['message'],
        Appsignal::Utils.json_generate(@data['backtrace'])
      )
    end

    def set_sample_data
      {
        :params       => @data['params'],
        :session_data => @data['session_data'],
        :environment  => @data['environment'],
        :tags         => @data['tags']
      }.each do |key, data|
        next unless data.is_a?(Array) || data.is_a?(Hash)
        begin
          @ext.set_sample_data(
            key.to_s,
            Appsignal::Utils.json_generate(data)
          )
        rescue *Appsignal::Transaction::JSON_EXCEPTIONS => e
          Appsignal.logger.error("Error generating JSON (#{e.class}: #{e.message}) for '#{backtrace.inspect}'")
        end
      end
    end

    def complete!
{'fraction_non_alphanumeric': 0.12481259370314843, 'fraction_numerical': 0.0, 'mean_word_length': 2.2117930204572804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '400203', 'n_tokens_mistral': 813, 'n_tokens_neox': 795, 'n_words': 156}