<commit_before>class ReposController < ApplicationController
  def show
    @organization = find_organization_by_name(params[:organization_name])
    @repo = find_organization_repos_by_name(@organization, params[:repo_name])

    display_count = params[:display_count].to_i

    @benchmark_run_display_count =
      if BenchmarkRun::PAGINATE_COUNT.include?(display_count)
        display_count
      else
        BenchmarkRun::DEFAULT_PAGINATE_COUNT
      end

    if @form_result_type = params[:result_type]
      @benchmark_type = find_benchmark_type_by_category(@form_result_type)

      benchmark_result_type_ids = @benchmark_type
        .benchmark_runs
        .pluck(:benchmark_result_type_id)
        .uniq
        .sort

      @charts = benchmark_result_type_ids.map do |benchmark_result_type_id|
        benchmark_result_type = BenchmarkResultType.find(benchmark_result_type_id)

        benchmark_runs = fetch_benchmark_runs(
          'Commit', @form_result_type, benchmark_result_type, @benchmark_run_display_count
        )

        next if benchmark_runs.empty?

        chart_builder = ChartBuilder.new(benchmark_runs.sort_by do |benchmark_run|
          benchmark_run.initiator.created_at
        end)

        columns = chart_builder.build_columns do |benchmark_run|
          environment = YAML.load(benchmark_run.environment)

          if environment.is_a?(Hash)
            temp = ""

            environment.each do |key, value|
              temp << "#{key}: #{value}<br>"
            end

            environment = temp
          end

          commit = benchmark_run.initiator

          "Commit: #{commit.sha1[0..6]}<br>" \
          "Commit Date: #{commit.created_at}<br>" \
          "Commit Message: #{commit.message.truncate(30)}<br>" \
          "#{environment}"
        end

        [columns, benchmark_result_type]
      end.compact
    end

    respond_to do |format|
      format.html do
        @result_types = fetch_categories
      end

      format.js
    end
  end

  def show_releases
    @organization = find_organization_by_name(params[:organization_name])
    @repo = find_organization_repos_by_name(@organization, params[:repo_name])

    if @form_result_type = params[:result_type]
      @benchmark_type = find_benchmark_type_by_category(@form_result_type)

      benchmark_result_type_ids = @benchmark_type
        .benchmark_runs
        .pluck(:benchmark_result_type_id)
        .uniq
        .sort

      @charts = benchmark_result_type_ids.map do |benchmark_result_type_id|
        benchmark_result_type = BenchmarkResultType.find(benchmark_result_type_id)

        benchmark_runs = fetch_benchmark_runs(
          'Release', @form_result_type, benchmark_result_type
        ).to_a

        next if benchmark_runs.empty?
        benchmark_runs = BenchmarkRun.sort_by_initiator_version(benchmark_runs)

        if latest_benchmark_run = BenchmarkRun.latest_commit_benchmark_run(
          @benchmark_type, benchmark_result_type)

          benchmark_runs << latest_benchmark_run
        end

        columns = ChartBuilder.new(benchmark_runs).build_columns do |benchmark_run|
          environment = YAML.load(benchmark_run.environment)

          if environment.is_a?(Hash)
            temp = ""

            environment.each do |key, value|
              temp << "#{key}: #{value}<br>"
            end

            environment = temp
          end

          "Version: #{benchmark_run.initiator.version}<br> #{environment}"
        end

        [columns, benchmark_result_type]
      end.compact
    end

    respond_to do |format|
      format.html do
        @result_types = fetch_categories
      end

      format.js
    end
  end

  private

  def find_organization_by_name(name)
    Organization.find_by_name(params[:organization_name]) || not_found
  end

  def find_organization_repos_by_name(organization, name)
    organization.repos.find_by_name(name)
  end

  def find_benchmark_type_by_category(category)
    @repo.benchmark_types.find_by_category(category)
  end

  def fetch_benchmark_runs(initiator_type, form_result_type, benchmark_result_type, limit=nil)
    BenchmarkRun
      .joins(:benchmark_type)
      .where('benchmark_types.category = ?', form_result_type)
      .where(benchmark_result_type: benchmark_result_type)
      .includes(:initiator)
      .where(initiator_type: initiator_type)
      .limit(limit)
  end

  def fetch_categories
    @repo.benchmark_types.pluck(:category)
  end
end<commit_msg>Do not display benchmark_runs that are not valid.<commit_after>class ReposController < ApplicationController
  def show
    @organization = find_organization_by_name(params[:organization_name])
    @repo = find_organization_repos_by_name(@organization, params[:repo_name])

    display_count = params[:display_count].to_i

    @benchmark_run_display_count =
      if BenchmarkRun::PAGINATE_COUNT.include?(display_count)
        display_count
      else
        BenchmarkRun::DEFAULT_PAGINATE_COUNT
      end

    if @form_result_type = params[:result_type]
      @benchmark_type = find_benchmark_type_by_category(@form_result_type)

      benchmark_result_type_ids = @benchmark_type
        .benchmark_runs
        .pluck(:benchmark_result_type_id)
        .uniq
        .sort

      @charts = benchmark_result_type_ids.map do |benchmark_result_type_id|
        benchmark_result_type = BenchmarkResultType.find(benchmark_result_type_id)

        benchmark_runs = fetch_benchmark_runs(
          'Commit', @form_result_type, benchmark_result_type, @benchmark_run_display_count
        )

        next if benchmark_runs.empty?

        chart_builder = ChartBuilder.new(benchmark_runs.sort_by do |benchmark_run|
          benchmark_run.initiator.created_at
        end)

        columns = chart_builder.build_columns do |benchmark_run|
          environment = YAML.load(benchmark_run.environment)

          if environment.is_a?(Hash)
            temp = ""

            environment.each do |key, value|
              temp << "#{key}: #{value}<br>"
            end

            environment = temp
          end

          commit = benchmark_run.initiator

          "Commit: #{commit.sha1[0..6]}<br>" \
          "Commit Date: #{commit.created_at}<br>" \
          "Commit Message: #{commit.message.truncate(30)}<br>" \
          "#{environment}"
        end

        [columns, benchmark_result_type]
      end.compact
    end

    respond_to do |format|
      format.html do
        @result_types = fetch_categories
      end

      format.js
    end
  end

  def show_releases
    @organization = find_organization_by_name(params[:organization_name])
    @repo = find_organization_repos_by_name(@organization, params[:repo_name])

    if @form_result_type = params[:result_type]
      @benchmark_type = find_benchmark_type_by_category(@form_result_type)

      benchmark_result_type_ids = @benchmark_type
        .benchmark_runs
        .pluck(:benchmark_result_type_id)
        .uniq
        .sort

      @charts = benchmark_result_type_ids.map do |benchmark_result_type_id|
        benchmark_result_type = BenchmarkResultType.find(benchmark_result_type_id)

        benchmark_runs = fetch_benchmark_runs(
          'Release', @form_result_type, benchmark_result_type
        ).to_a

        next if benchmark_runs.empty?
        benchmark_runs = BenchmarkRun.sort_by_initiator_version(benchmark_runs)

        if latest_benchmark_run = BenchmarkRun.latest_commit_benchmark_run(
          @benchmark_type, benchmark_result_type)

          benchmark_runs << latest_benchmark_run
        end

        columns = ChartBuilder.new(benchmark_runs).build_columns do |benchmark_run|
          environment = YAML.load(benchmark_run.environment)

          if environment.is_a?(Hash)
            temp = ""

            environment.each do |key, value|
              temp << "#{key}: #{value}<br>"
            end

            environment = temp
          end

          "Version: #{benchmark_run.initiator.version}<br> #{environment}"
        end

        [columns, benchmark_result_type]
      end.compact
    end

    respond_to do |format|
      format.html do
        @result_types = fetch_categories
      end

      format.js
    end
  end

  private

  def find_organization_by_name(name)
    Organization.find_by_name(params[:organization_name]) || not_found
  end

  def find_organization_repos_by_name(organization, name)
    organization.repos.find_by_name(name)
  end

  def find_benchmark_type_by_category(category)
    @repo.benchmark_types.find_by_category(category)
  end

  def fetch_benchmark_runs(initiator_type, form_result_type, benchmark_result_type, limit=nil)
    BenchmarkRun
      .joins(:benchmark_type)
      .includes(:initiator)
      .where('benchmark_types.category = ?', form_result_type)
      .where(
        benchmark_result_type: benchmark_result_type,
        initiator_type: initiator_type,
        validity: true
      )
      .limit(limit)
  end

  def fetch_categories
    @repo.benchmark_types.pluck(:category)
  end
end
{'fraction_non_alphanumeric': 0.08046485888212507, 'fraction_numerical': 0.0011068068622025456, 'mean_word_length': 2.864841745081266, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 15, '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': '7468176', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2714, 'n_words': 441}