<commit_before>require 'singleton'


class SQLRecorder
  include Singleton

  @db_queries = []
  def flush
    captured_queries = @db_queries
    @db_queries = []
    return captured_queries
  end

  def record(payload, duration)
    return if invalid_payload?(payload)

    # do not record/analyze explain queries used in dev
    return if payload[:sql].include?('EXPLAIN')

    sql = payload[:sql]
    cleaned_trace = clean_trace(caller)
    explained = ActiveRecord::Base.connection.execute("EXPLAIN QUERY PLAN #{sql}", 'SQLR-EXPLAIN')
    @db_queries << {
      :sql => sql,
      :duration => duration,
      :trace => cleaned_trace,
      :explained => explained,
    }
  end

  def invalid_payload?(payload)
    ignore_query_names = [
      'SCHEMA',
      'SQLR-EXPLAIN',
    ]
    payload[:name] && ignore_query_names.any? { |name| payload[:name].in?(name) }
  end

  def clean_trace(trace)
    Rails.backtrace_cleaner.remove_silencers!
    Rails.backtrace_cleaner.add_silencer { |line| not line =~ /^(app)\// }
    Rails.backtrace_cleaner.clean(trace)
  end
end
SQLRecorder.instance.flush<commit_msg>Use .include? instead of .in?<commit_after>require 'singleton'


class SQLRecorder
  include Singleton

  @db_queries = []
  def flush
    captured_queries = @db_queries
    @db_queries = []
    return captured_queries
  end

  def record(payload, duration)
    return if invalid_payload?(payload)

    # do not record/analyze explain queries used in dev
    return if payload[:sql].include?('EXPLAIN')

    sql = payload[:sql]
    cleaned_trace = clean_trace(caller)
    explained = ActiveRecord::Base.connection.execute("EXPLAIN QUERY PLAN #{sql}", 'SQLR-EXPLAIN')
    @db_queries << {
      :sql => sql,
      :duration => duration,
      :trace => cleaned_trace,
      :explained => explained,
    }
  end

  def invalid_payload?(payload)
    ignore_query_names = [
      'SCHEMA',
      'SQLR-EXPLAIN',
    ]
    payload[:name] && ignore_query_names.any? { |name| payload[:name].include?(name) }
  end

  def clean_trace(trace)
    Rails.backtrace_cleaner.remove_silencers!
    Rails.backtrace_cleaner.add_silencer { |line| not line =~ /^(app)\// }
    Rails.backtrace_cleaner.clean(trace)
  end
end
SQLRecorder.instance.flush
{'fraction_non_alphanumeric': 0.12048192771084337, 'fraction_numerical': 0.0, 'mean_word_length': 3.4220907297830374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '5700734', 'n_tokens_mistral': 759, 'n_tokens_neox': 752, 'n_words': 151}