<commit_before>require 'csv'

class GrantingEngineController < ApplicationController

  def approve_projects
    @projects = Project.where(:status => 'P')
  end

  # Expected Data format:
  # [
  # 	{
  # 		'projectId' : NUMBER,
  # 		'projectStatus': STATUS
  # 	}
  # ]
  def execute_approve_projects
    params[:approved_projects].each do |project|
      project_id = project[:project_id]
      approval_status = project[:project_status]

      project_entity = Project.find(project_id)
      project_entity.approval_status = ApprovalStatus.find(approval_status)
      project_entity.save
    end

    render json: {'answer': 'success'}
  end

  def adjustment_grants
    approved_projects = Project.where(:status => 'A1')
                            .where('updated_at >= ?', Date.today)

    @adjustment_grants = generate_adjustment_grants(approved_projects)

    respond_to do |format|
      format.html
      format.csv do
        @adjustment_grants.each { |grant| grant.save }
        file_name = "\"#{Date.today}-grants\""
        headers['Content-Type'] = 'text/csv'
        headers['Content-Disposition'] = "attachment; filename=#{file_name}"
      end
    end
  end

  def periodic_grants
    @grants = generate_periodic_grants(Project.all).compact

    respond_to do |format|
      format.html
      format.csv do
        @grants.each { |grant| grant.save }
        file_name = "\"#{Date.today}-periodic-grants\""
        headers['Content-Type'] = 'text/csv'
        headers['Content-Disposition'] = "attachment; filename=#{file_name}"
      end
    end
  end

  private
  def generate_adjustment_grants(approved_projects)
    approved_projects.map do |project|
      Grant.new(:project => project,
                :receiver_wallet => project.wallet_address,
                :type_tag => 'AGRT',
                :grant_date => adjust_date(project.install_date),
                :amount => calculate_grant_amount(project, grant.grant_date))
    end
  end

  def generate_periodic_grants(projects)
    projects.map do |project|
      grant_date = Date.today
      six_months = project.install_date.advance(:months => 6)
      six_months = Date.new(grant_date.year, six_months.month, six_months.day)

      unless six_months > grant_date
        Grant.new(:GUID => generate_guid('PGRT', project),
                  :project => project,
                  :receiver_wallet => project.wallet_address,
                  :amount => 180 * project.nameplate * 0.15, # 6 months = 180 days
                  :grant_date => grant_date,<commit_msg>clean up and refactoring to use the adjusted flag<commit_after>require 'csv'

class GrantingEngineController < ApplicationController

  def approve_projects
    @projects = Project.where(:status => 'P')
  end

  # Expected Data format:
  # [
  # 	{
  # 		'projectId' : NUMBER,
  # 		'projectStatus': STATUS
  # 	}
  # ]
  def execute_approve_projects
    params[:approved_projects].each do |project|
      project_id = project[:project_id]
      approval_status = project[:project_status]

      project_entity = Project.find(project_id)
      project_entity.approval_status = ApprovalStatus.find(approval_status)
      project_entity.save
    end

    render json: {'answer': 'success'}
  end

  def adjustment_grants
    approved_projects = Project.where(:adjusted => false)

    @adjustment_grants = generate_adjustment_grants(approved_projects)

    respond_to do |format|
      format.html
      format.csv do
        @adjustment_grants.each { |grant| grant.save }
        file_name = "\"#{Date.today}-grants\""
        headers['Content-Type'] = 'text/csv'
        headers['Content-Disposition'] = "attachment; filename=#{file_name}"
      end
    end
  end

  def periodic_grants
    @grants = generate_periodic_grants(Project.all).compact

    respond_to do |format|
      format.html
      format.csv do
        @grants.each { |grant| grant.save }
        file_name = "\"#{Date.today}-periodic-grants\""
        headers['Content-Type'] = 'text/csv'
        headers['Content-Disposition'] = "attachment; filename=#{file_name}"
      end
    end
  end

  private
  def generate_adjustment_grants(approved_projects)
    approved_projects.map do |project|
      Grant.new(:GUID => generate_guid('AGRT', project),
                :project => project,
                :receiver_wallet => project.wallet_address,
                :type_tag => 'AGRT',
                :grant_date => adjust_date(project.install_date),
                :amount => calculate_grant_amount(project, grant.grant_date))
    end
  end

  def generate_periodic_grants(projects)
    projects.map do |project|
      grant_date = Date.today
      six_months = project.install_date.advance(:months => 6)
      six_months = Date.new(grant_date.year, six_months.month, six_months.day)

      unless six_months > grant_date
        Grant.new(:GUID => generate_guid('PGRT', project),
                  :project => project,
                  :receiver_wallet => project.wallet_address,
                  :amount => 180 * project.nameplate * 0.15, # 6 months = 180 days
                  :grant_date => grant_date,
{'fraction_non_alphanumeric': 0.10987509758001561, 'fraction_numerical': 0.004488680718188915, 'mean_word_length': 2.721859114015977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '2264370', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1517, 'n_words': 292}