<commit_before>require_dependency "hubstats/application_controller"

module Hubstats
  class DeploysController < ApplicationController

    def index
      # sets to include user and repo, and sorts data
      @deploys = Hubstats::Deploy.includes(:repo).includes(:pull_requests)
        .belonging_to_users(params[:users]).belonging_to_repos(params[:repos])
        .group_by(params[:group])
        .order_with_timespan(@timespan, params[:order])
        .paginate(:page => params[:page], :per_page => 15)

      # enables grouping by user or repo
      if params[:group] == "user"
        @groups = @deploys.to_a.group_by(&:user_name)
      elsif params[:group] == "repo"
        @groups = @deploys.to_a.group_by(&:repo_name)
      end
    end

    # show basic stats and pull requests from a single deploy
    def show
      @deploy = Hubstats::Deploy.includes(:repo).includes(:pull_requests).find(params[:id])
      repo = @deploy.repo
      @pull_requests = @deploy.pull_requests
      pull_request_count = @pull_requests.length
      net_additions = find_net_additions(@deploy.id)
      comment_count = find_comment_count(@deploy.id)
      @stats = {
        pull_count: pull_request_count,
        net_additions: net_additions,
        comment_count: comment_count
      }
    end

    # finds all of the additions and deletions in all pull requests and then makes the net additions
    def find_net_additions(deploy_id)
      deploy = Hubstats::Deploy.find(deploy_id)
      pull_requests = deploy.pull_requests
      total_additions = 0
      total_deletions = 0
      pull_requests.each do |pull|
        total_additions += pull.additions.to_i
        total_deletions += pull.deletions.to_i
      end
      return total_additions - total_deletions
    end

    # returns the total amount of comments from all pull requests in a deploy
    def find_comment_count(deploy_id)
      deploy = Hubstats::Deploy.find(deploy_id)
      pull_requests = deploy.pull_requests
      total_comments = 0
      pull_requests.each do |pull|
        if pull.comments
          total_comments += pull.comments
        end
      end
      return total_comments
    end

    # make a new deploy with all of the correct attributes
    def create
      if params[:deployed_by].nil? || params[:git_revision].nil? || params[:repo_name].nil?
        render text: "Missing a necessary parameter: deployer, git revision, or repository name.", :status => 400 and return
      else
        @deploy = Deploy.new
        @deploy.deployed_at = params[:deployed_at]<commit_msg>edited so that we're using correct comment count<commit_after>require_dependency "hubstats/application_controller"

module Hubstats
  class DeploysController < ApplicationController

    def index
      # sets to include user and repo, and sorts data
      @deploys = Hubstats::Deploy.includes(:repo).includes(:pull_requests)
        .belonging_to_users(params[:users]).belonging_to_repos(params[:repos])
        .group_by(params[:group])
        .order_with_timespan(@timespan, params[:order])
        .paginate(:page => params[:page], :per_page => 15)

      # enables grouping by user or repo
      if params[:group] == "user"
        @groups = @deploys.to_a.group_by(&:user_name)
      elsif params[:group] == "repo"
        @groups = @deploys.to_a.group_by(&:repo_name)
      end
    end

    # show basic stats and pull requests from a single deploy
    def show
      @deploy = Hubstats::Deploy.includes(:repo).includes(:pull_requests).find(params[:id])
      repo = @deploy.repo
      @pull_requests = @deploy.pull_requests
      pull_request_count = @pull_requests.length
      net_additions = find_net_additions(@deploy.id)
      comment_count = find_comment_count(@deploy.id)
      @stats = {
        pull_count: pull_request_count,
        net_additions: net_additions,
        comment_count: comment_count
      }
    end

    # finds all of the additions and deletions in all pull requests and then makes the net additions
    def find_net_additions(deploy_id)
      deploy = Hubstats::Deploy.find(deploy_id)
      pull_requests = deploy.pull_requests
      total_additions = 0
      total_deletions = 0
      pull_requests.each do |pull|
        total_additions += pull.additions.to_i
        total_deletions += pull.deletions.to_i
      end
      return total_additions - total_deletions
    end

    # returns the total amount of comments from all pull requests in a deploy
    def find_comment_count(deploy_id)
      deploy = Hubstats::Deploy.find(deploy_id)
      pull_requests = deploy.pull_requests
      total_comments = 0
      pull_requests.each do |pull|
        if pull.comments != nil
          total_comments += Hubstats::Comment.belonging_to_pull_request(pull.id).includes(:user).created_since(@timespan).count(:all)
          #total_comments += pull.comments
        end
      end
      return total_comments
    end

    # make a new deploy with all of the correct attributes
    def create
      if params[:deployed_by].nil? || params[:git_revision].nil? || params[:repo_name].nil?
        render text: "Missing a necessary parameter: deployer, git revision, or repository name.", :status => 400 and return
      else
        @deploy = Deploy.new
        @deploy.deployed_at = params[:deployed_at]
{'fraction_non_alphanumeric': 0.08975572808180268, 'fraction_numerical': 0.0030297292179511455, 'mean_word_length': 3.056835637480799, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5891782', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1526, 'n_words': 383}