<commit_before>class ReposController < ApplicationController
  # ZOMG too much logic here and we really need to clean this up.
  # ActiveRecord queries are taking wayyy tooo long.
  def show
    organization = Organization.find_by_name(params[:organization_name]) || not_found
    @repo = organization.repos.find_by_name(params[:repo_name])
    @commits = @repo.commits
    @form_result_type = params[:result_type]
    @result_types = @commits.first.benchmark_runs.map(&:category).sort

    form_result_types =
      case @form_result_type
      when 'all'
        @result_types
      when 'none', nil
        []
      else
        [@form_result_type]
      end

    commits_sha1s ||= ['Commit SHA1']
    commits_data ||= {}

    @commits.includes(:benchmark_runs).reverse.each do |commit|
      commit_benchmark_runs = commit.benchmark_runs
      next if commit_benchmark_runs.empty?

      form_result_types.each do |result_type|
        commits_data[result_type] ||= {}

        commit_benchmark_runs.where(category: result_type).each do |benchmark_run|
          commits_sha1s << "
            Commit: #{commit.sha1[0..6]}<br>
            Commit Date: #{commit.created_at}<br>
            Environment: #{benchmark_run.environment}<br>
          ".squish

          benchmark_run.result.each do |key, value|
            commits_data[result_type][key] ||= [key]
            commits_data[result_type][key] << value
          end
        end
      end
    end

    @graphs_columns = build_graphs_columns(commits_sha1s, commits_data)
  end

  private

  def build_graphs_columns(commits_sha1s, commits_data)
    if !commits_data.empty?
      commits_data.map do |result_type, result_data|
        graphs_columns = [commits_sha1s]

        result_data.map do |_, value|
          graphs_columns << value
        end

        graphs_columns
      end
    end
  end
end<commit_msg>Avoid looping through all the commits.<commit_after>class ReposController < ApplicationController
  # ZOMG too much logic here and we really need to clean this up.
  # ActiveRecord queries are taking wayyy tooo long.
  def show
    organization = Organization.find_by_name(params[:organization_name]) || not_found
    @repo = organization.repos.find_by_name(params[:repo_name])
    @commits = @repo.commits
    @form_result_type = params[:result_type]
    benchmark_runs = BenchmarkRun.where(commit_id: @commits.map(&:id)).includes(:commit)
    @result_types = benchmark_runs.pluck(:category).uniq

    form_result_types =
      case @form_result_type
      when 'all'
        @result_types
      when 'none', nil
        []
      else
        [@form_result_type]
      end

    commits_sha1s ||= ['Commit SHA1']
    commits_data ||= {}

    form_result_types.each do |result_type|
      commits_data[result_type] ||= {}

      benchmark_runs.where(category: result_type).each do |benchmark_run|
        commits_sha1s << "
          Commit: #{benchmark_run.commit.sha1[0..6]}<br>
          Commit Date: #{benchmark_run.commit.created_at}<br>
          Environment: #{benchmark_run.environment}<br>
        ".squish

        benchmark_run.result.each do |key, value|
          commits_data[result_type][key] ||= [key]
          commits_data[result_type][key] << value
        end
      end
    end

    @graphs_columns = build_graphs_columns(commits_sha1s, commits_data)
  end

  private

  def build_graphs_columns(commits_sha1s, commits_data)
    if !commits_data.empty?
      commits_data.map do |result_type, result_data|
        graphs_columns = [commits_sha1s]

        result_data.map do |_, value|
          graphs_columns << value
        end

        graphs_columns
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09540636042402827, 'fraction_numerical': 0.004892633867898886, 'mean_word_length': 2.8413361169102296, 'pattern_counts': {'":': 0, '<': 23, '<?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': '5697377', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1157, 'n_words': 230}