<commit_before>class ActiveRecord::Base
  def transaction_changed_attributes
    @transaction_changed_attributes ||= HashWithIndifferentAccess.new
  end

  def self.bq_attributes(opts = {})
    unless RUBY_ENGINE == 'opal' || !BqStream.dataset
      if opts == :all
        bq_atr_of_interest = column_names.map(&:to_sym)
      elsif opts[:only]
        raise 'opts must be an array' unless opts[:only].is_a? Array
        bq_atr_of_interest = opts[:only].map(&:to_sym)
      elsif opts[:except]
        raise 'opts must be an array.' unless opts[:except].is_a? Array
        bq_atr_of_interest = column_names.map(&:to_sym).select do |column|
          !opts[:except].include?(column)
        end
      else
        raise 'You must declare an opts hash with a key of :all, :only '\
          'or :except) and a value as an array, if using :only or :except.'
      end
      if BqStream.back_date
        bq_atr_of_interest.each do |attribute|
          record = BqStream::OldestRecord.find_by(table_name: name, attr: attribute)
          BqStream::OldestRecord.create(table_name: name, attr: attribute) unless record
        end
      end

      after_save do
        changes.each do |k, v|
          transaction_changed_attributes[k] = v[1]
        end
      end

      after_commit on: [:create] do
        queue_create(bq_atr_of_interest)
        @transaction_changed_attributes = nil
      end

      after_commit on: [:update] do
        queue_update(bq_atr_of_interest)
        @transaction_changed_attributes = nil
      end

      after_commit on: [:destroy] do
        queue_destroy
        @transaction_changed_attributes = nil
      end

      after_rollback do
        @transaction_changed_attributes = nil
      end
    end
  end

  def queue_create(attributes_of_interest)
    attributes.each do |k, v|
      next unless attributes_of_interest.include?(k.to_sym) && !v.nil?
      BqStream::QueuedItem.create(table_name: self.class.to_s,
                                  record_id: id, attr: k,
                                  new_value: v.to_s)
    end
  end

  def queue_update(attributes_of_interest)
    transaction_changed_attributes.each do |k, v|
      next unless attributes_of_interest.include?(k.to_sym)
      BqStream::QueuedItem.create(table_name: self.class.to_s, record_id: id,
                                  attr: k, new_value: v.to_s)
    end
  rescue Exception => e
    BqStream.log(:error, "#{Time.now}: EXCEPTION: #{e}")
  end unless RUBY_ENGINE == 'opal'

  def queue_destroy
    BqStream::QueuedItem.create(table_name: self.class.to_s, record_id: id,
                                attr: 'Destroyed', new_value: 'True')
  end
end<commit_msg>lets new_value of timestamp to be setn to BigQuery as utc<commit_after>class ActiveRecord::Base
  def transaction_changed_attributes
    @transaction_changed_attributes ||= HashWithIndifferentAccess.new
  end

  def self.bq_attributes(opts = {})
    unless RUBY_ENGINE == 'opal' || !BqStream.dataset
      if opts == :all
        bq_atr_of_interest = column_names.map(&:to_sym)
      elsif opts[:only]
        raise 'opts must be an array' unless opts[:only].is_a? Array
        bq_atr_of_interest = opts[:only].map(&:to_sym)
      elsif opts[:except]
        raise 'opts must be an array.' unless opts[:except].is_a? Array
        bq_atr_of_interest = column_names.map(&:to_sym).select do |column|
          !opts[:except].include?(column)
        end
      else
        raise 'You must declare an opts hash with a key of :all, :only '\
          'or :except) and a value as an array, if using :only or :except.'
      end
      if BqStream.back_date
        bq_atr_of_interest.each do |attribute|
          record = BqStream::OldestRecord.find_by(table_name: name, attr: attribute)
          BqStream::OldestRecord.create(table_name: name, attr: attribute) unless record
        end
      end

      after_save do
        changes.each do |k, v|
          transaction_changed_attributes[k] = v[1]
        end
      end

      after_commit on: [:create] do
        queue_create(bq_atr_of_interest)
        @transaction_changed_attributes = nil
      end

      after_commit on: [:update] do
        queue_update(bq_atr_of_interest)
        @transaction_changed_attributes = nil
      end

      after_commit on: [:destroy] do
        queue_destroy
        @transaction_changed_attributes = nil
      end

      after_rollback do
        @transaction_changed_attributes = nil
      end
    end
  end

  def queue_create(attributes_of_interest)
    attributes.each do |k, v|
      next unless attributes_of_interest.include?(k.to_sym) && !v.nil?
      BqStream::QueuedItem.create(table_name: self.class.to_s,
                                  record_id: id, attr: k,
                                  new_value: (v.try(:utc) ? v.utc : v).to_s)
    end
  end

  def queue_update(attributes_of_interest)
    transaction_changed_attributes.each do |k, v|
      next unless attributes_of_interest.include?(k.to_sym)
      BqStream::QueuedItem.create(table_name: self.class.to_s, record_id: id,
                                  attr: k, new_value: (v.try(:utc) ? v.utc : v).to_s)
    end
  rescue Exception => e
    BqStream.log(:error, "#{Time.now}: EXCEPTION: #{e}")
  end unless RUBY_ENGINE == 'opal'

  def queue_destroy
    BqStream::QueuedItem.create(table_name: self.class.to_s, record_id: id,
                                attr: 'Destroyed', new_value: 'True')
  end
end
{'fraction_non_alphanumeric': 0.09310850439882698, 'fraction_numerical': 0.00036656891495601173, 'mean_word_length': 2.478011472275335, 'pattern_counts': {'":': 0, '<': 3, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2323656', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1715, 'n_words': 405}