<commit_before>require_relative "text_formatter"

module ActiveRecordRecord
  module SummaryGeneration
    def clear_ar_counts
      Thread.current[:ar_counts] = {}
      Thread.current[:times] = {}
      Thread.current[:request_start_time] = Time.now
      Thread.current[:do_counts] = true
      Thread.current[:objects_key] = :default
    end

    def dump_counts(stat_data, out, prefix = '')
      out.puts TextFormatter.format(stat_data, prefix = '')
    end

    def print_ar_counts
      return if controller_name == 'page_not_found'
      tmp, path = open_temp_file
      tmp.puts "ActiveRecord counts for request to #{controller_name}##{action_name}\n\n"

      printed_something = dump_counts(Thread.current[:ar_counts][:default], tmp)

      tmp.puts "Timings:"
      (Thread.current[:times] || {}).to_a.sort_by { |i| i.last[:sum] }.reverse.each do |key, obj|
        tmp.puts "\n\t#{key}\t\t#{obj[:sum]} seconds\t(#{obj[:count]} times with an average of #{obj[:sum] / obj[:count]} seconds)"

        if Thread.current[:ar_counts][key]
          dump_counts(Thread.current[:ar_counts][key], tmp, "\t\t\t")
        end

        printed_something = true
      end

      if printed_something
        tmp.puts
        tmp.puts "Request took approximately #{Time.now.to_f - Thread.current[:request_start_time].to_f} seconds."
        tmp.close
        system("open #{path} -t -g")
      else
        close_temp_file(tmp, path)
      end
    end

    def open_temp_file
      path = "/tmp/#{Time.now.to_f}.counts.txt"
      tmp = File.open(path, "w")
      return tmp, path
    end

    def close_temp_file(tem_file, path)
      temp_file.close
      File.unlink path
    end
  end
end
<commit_msg>Convert to dependency injection<commit_after>require_relative "text_formatter"

module ActiveRecordRecord
  module SummaryGeneration
    def clear_ar_counts
      Thread.current[:ar_counts] = {}
      Thread.current[:times] = {}
      Thread.current[:request_start_time] = Time.now
      Thread.current[:do_counts] = true
      Thread.current[:objects_key] = :default
    end

    def print_ar_counts(formatter = TextFormatter)
      return if controller_name == 'page_not_found'
      temp_file, path = open_temp_file
      temp_file.puts "ActiveRecord counts for request to #{controller_name}##{action_name}\n\n"

      printed_something, default_text  = formatter.format(Thread.current[:ar_counts][:default])
      temp_file.puts(text) unless default_text.empty?

      temp_file.puts "Timings:"
      (Thread.current[:times] || {}).to_a.sort_by { |i| i.last[:sum] }.reverse.each do |key, obj|
        temp_file.puts "\n\t#{key}\t\t#{obj[:sum]} seconds\t(#{obj[:count]} times with an average of #{obj[:sum] / obj[:count]} seconds)"

        if Thread.current[:ar_counts][key]
          _, custom_text = formatter.format(Thread.current[:ar_counts][key], "\t\t\t")
          temp_file.puts(text) unless custom_text.empty?
        end

        printed_something = true
      end

      if printed_something
        temp_file.puts
        temp_file.puts "Request took approximately #{Time.now.to_f - Thread.current[:request_start_time].to_f} seconds."
        temp_file.close
        system("open #{path} -t -g")
      else
        close_temp_file(temp_file, path)
      end
    end

    private

    def open_temp_file
      path = "/tmp/#{Time.now.to_f}.counts.txt"
      temp_file = File.open(path, "w")
      return temp_file, path
    end

    def close_temp_file(tem_file, path)
      temp_file.close
      File.unlink path
    end
  end
end

{'fraction_non_alphanumeric': 0.12419036891016615, 'fraction_numerical': 0.0, 'mean_word_length': 2.903296703296703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '405885', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1152, 'n_words': 231}