<commit_before># Derived from code found in http://stackoverflow.com/questions/5490411/counting-the-number-of-queries-performed

require 'set' ##
$done = false ##

class QueryCounter
  def self.count(&block)
    new.count(&block)
  end

  def self.track(&block)
    new.track(&block)
  end

  def initialize
    @queries = 0
    @other_hits = 0

    @queries_timing = 0
    @other_timing = 0

    ## @per_query_counts = Hash.new { |h, n| h[n] ||= 0 }
    ## @other_statements = Set.new
  end

  attr_accessor :queries, :queries_timing
  attr_accessor :other_hits, :other_timing
  attr_accessor :names
  def callback(_name, start, finish, _id, payload)
    #byebug unless payload[:name] == "SCHEMA" || $done
    case payload[:name]
    when "SCHEMA", "CACHE", nil # ROLLBACK, BEGIN, END
      @other_hits += 1
      @other_timing += (finish - start)
      ## @other_statements << (payload[:name] || payload[:sql])
      args = (payload[:binds] || []).map { |a| a[1] }
      # puts "#{payload[:name]}: #{payload[:sql]}, #{args.inspect}"
    else
      @queries += 1
      @queries_timing += (finish-start)
      # byebug if payload[:binds].first.kind_of?(Array) && payload[:binds].first.size != 2
      # args = (payload[:binds] || []).map { |a| a[1] }
      # puts "#{payload[:name]}: #{payload[:sql]}, #{args.inspect}"
      ## @per_query_counts[[payload[:sql], args]] += 1
    end
#  rescue => e
#    byebug
  end

  def callback_proc
    lambda(&method(:callback))
  end

  def count(&block)
    track(&block)
    @queries
  end

  def track(&block)
    ActiveSupport::Notifications.subscribed(callback_proc, 'sql.active_record', &block)
    self
  end

  def to_i
    @queries
  end

  def inspect
    "<QueryCounter queries=#{@queries} [#{@queries_timing}]#{" other_hits=#{@other_hits} #{@other_timing}" if @other_hits > 0} >"
  end
  alias_method :to_s, :inspect
end
<commit_msg>query counter: add rollback/begin/end to other<commit_after># Derived from code found in http://stackoverflow.com/questions/5490411/counting-the-number-of-queries-performed

require 'set' ##
$done = false ##

class QueryCounter
  def self.count(&block)
    new.count(&block)
  end

  def self.track(&block)
    new.track(&block)
  end

  def initialize
    @queries = 0
    @other_hits = 0

    @queries_timing = 0
    @other_timing = 0

    ## @per_query_counts = Hash.new { |h, n| h[n] ||= 0 }
    ## @other_statements = Set.new
  end

  OTHER_SQL = ["ROLLBACK", "BEGIN", "COMMIT"].freeze

  attr_accessor :queries, :queries_timing
  attr_accessor :other_hits, :other_timing
  attr_accessor :names
  def callback(_name, start, finish, _id, payload)
    #byebug unless payload[:name] == "SCHEMA" || $done
    if payload[:name] == "SCHEMA" || (payload[:name].nil? && OTHER_SQL.include?(payload[:sql]))
      #puts "other: #{payload[:sql]}"
      @other_hits += 1
      @other_timing += (finish - start)
      ## @other_statements << (payload[:name] || payload[:sql])
      # args = (payload[:binds] || []).map { |a| a[1] }
      # puts "#{payload[:name]}: #{payload[:sql]}, #{args.inspect}"
    else
      @queries += 1
      @queries_timing += (finish-start)
      # byebug if payload[:binds].first.kind_of?(Array) && payload[:binds].first.size != 2
      # args = (payload[:binds] || []).map { |a| a[1] }
      # puts "#{payload[:name]}: #{payload[:sql]}, #{args.inspect}"
      ## @per_query_counts[[payload[:sql], args]] += 1
    end
#  rescue => e
#    byebug
  end

  def callback_proc
    lambda(&method(:callback))
  end

  def count(&block)
    track(&block)
    @queries
  end

  def track(&block)
    ActiveSupport::Notifications.subscribed(callback_proc, 'sql.active_record', &block)
    self
  end

  def to_i
    @queries
  end

  def inspect
    "<QueryCounter queries=#{@queries} [#{@queries_timing}]#{" other_hits=#{@other_hits} #{@other_timing}" if @other_hits > 0} >"
  end
  alias_method :to_s, :inspect
end

{'fraction_non_alphanumeric': 0.1689672801635992, 'fraction_numerical': 0.009713701431492843, 'mean_word_length': 3.4516496018202503, 'pattern_counts': {'":': 0, '<': 9, '<?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': '102247', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1411, 'n_words': 302}