<commit_before>class Admin::UnattachedPromoRegistrationsController < AdminController
  include PromosHelper

  def index
    filter = params[:filter]
    case filter
    when "All codes", nil, ""
      @promo_registrations = PromoRegistration.unattached_only.order("created_at DESC")
    when "Not assigned"
      @promo_registrations = PromoRegistration.unattached_only.where(promo_campaign_id: nil).order("created_at DESC")
    else
      @promo_registrations = PromoRegistration.joins(:promo_campaign).
                                               unattached.
                                               where(promo_campaigns: {name: filter}).
                                               order("created_at DESC")
    end
    @current_campaign = params[:filter] || "All codes"
    @campaigns = PromoCampaign.all.map {|campaign| campaign.name}
  end

  def create
    number = create_params.to_i
    if number > 50
      redirect_to admin_unattached_promo_registrations_path, alert: "Can't create more than 50 codes at a time."
    else
      PromoRegistrarUnattached.new(number: number).perform
      redirect_to admin_unattached_promo_registrations_path, notice: "#{number} codes created."
    end
  end

  def report
    referral_codes = params[:referral_codes]
    @reporting_interval = params[:reporting_interval]
    @event_types = params[:event_types]
    if @event_types.nil?
      return redirect_to admin_unattached_promo_registrations_path(filter: params[:filter]),
                        alert: "Please check at least one of downloads, installs, or confirmations."
    end
    
    report_start_and_end_date = parse_report_dates(params[:referral_code_report_period], @reporting_interval)
    report_info = PromoReportGenerator.new(referral_codes: referral_codes,
                                                  start_date: report_start_and_end_date[:start_date],
                                                  end_date: report_start_and_end_date[:end_date],
                                                  reporting_interval: @reporting_interval).perform

    @start_date = report_info["start_date"]
    @end_date = report_info["end_date"]
    @report_contents = report_info["contents"]

    report_string = render_to_string :layout => false
    send_data report_string, filename: "BraveReferralPromoReport.html", type: "application/html"
  end

  def update_statuses
    referral_codes = params[:referral_codes]
    referral_code_status = params[:referral_code_status]
    promo_registrations = PromoRegistration.where(referral_code: referral_codes)
    PromoUnattachedStatusUpdater.new(promo_registrations: promo_registrations, status: referral_code_status).perform
    redirect_to admin_unattached_promo_registrations_path(filter: params[:filter]),
                notice: "#{referral_codes.count} codes updated to '#{referral_code_status}' status."
  end

  def assign
    referral_codes = params[:referral_codes]
    promo_campaign_target = PromoCampaign.where(name: params[:promo_campaign_target]).first
    promo_registrations = PromoRegistration.where(referral_code: referral_codes)
    promo_registrations.update_all(promo_campaign_id: promo_campaign_target.id)
    redirect_to admin_unattached_promo_registrations_path(filter: params[:promo_campaign_target]),
                notice: "Assigned #{referral_codes.count} codes to campaign '#{params[:promo_campaign_target]}'."
  end

  private

  def parse_report_dates(report_period, reporting_interval)
    start_date = Date.new(report_period["start(1i)"].to_i,
                          report_period["start(2i)"].to_i,
                          report_period["start(3i)"].to_i)
    end_date = Date.new(report_period["end(1i)"].to_i,
                        report_period["end(2i)"].to_i,
                        report_period["end(3i)"].to_i)
    {
      start_date: start_date,
      end_date: end_date
    }
  end

  def create_params
    params.require(:number_of_codes_to_create)
  end
end<commit_msg>Report first runs as downloads instead of retrievals<commit_after>class Admin::UnattachedPromoRegistrationsController < AdminController
  include PromosHelper

  def index
    filter = params[:filter]
    case filter
    when "All codes", nil, ""
      @promo_registrations = PromoRegistration.unattached_only.order("created_at DESC")
    when "Not assigned"
      @promo_registrations = PromoRegistration.unattached_only.where(promo_campaign_id: nil).order("created_at DESC")
    else
      @promo_registrations = PromoRegistration.joins(:promo_campaign).
                                               unattached_only.
                                               where(promo_campaigns: {name: filter}).
                                               order("created_at DESC")
    end
    @current_campaign = params[:filter] || "All codes"
    @campaigns = PromoCampaign.all.map {|campaign| campaign.name}
  end

  def create
    number = create_params.to_i
    if number > 50
      redirect_to admin_unattached_promo_registrations_path, alert: "Can't create more than 50 codes at a time."
    else
      PromoRegistrarUnattached.new(number: number).perform
      redirect_to admin_unattached_promo_registrations_path, notice: "#{number} codes created."
    end
  end

  def report
    referral_codes = params[:referral_codes]
    @reporting_interval = params[:reporting_interval]
    @event_types = params[:event_types]
    if @event_types.nil?
      return redirect_to admin_unattached_promo_registrations_path(filter: params[:filter]),
                        alert: "Please check at least one of downloads, installs, or confirmations."
    end
    
    report_start_and_end_date = parse_report_dates(params[:referral_code_report_period], @reporting_interval)
    report_info = PromoReportGenerator.new(referral_codes: referral_codes,
                                                  start_date: report_start_and_end_date[:start_date],
                                                  end_date: report_start_and_end_date[:end_date],
                                                  reporting_interval: @reporting_interval).perform

    @start_date = report_info["start_date"]
    @end_date = report_info["end_date"]
    @report_contents = report_info["contents"]

    report_string = render_to_string :layout => false
    send_data report_string, filename: "BraveReferralPromoReport.html", type: "application/html"
  end

  def update_statuses
    referral_codes = params[:referral_codes]
    referral_code_status = params[:referral_code_status]
    promo_registrations = PromoRegistration.where(referral_code: referral_codes)
    PromoUnattachedStatusUpdater.new(promo_registrations: promo_registrations, status: referral_code_status).perform
    redirect_to admin_unattached_promo_registrations_path(filter: params[:filter]),
                notice: "#{referral_codes.count} codes updated to '#{referral_code_status}' status."
  end

  def assign
    referral_codes = params[:referral_codes]
    promo_campaign_target = PromoCampaign.where(name: params[:promo_campaign_target]).first
    promo_registrations = PromoRegistration.where(referral_code: referral_codes)
    promo_registrations.update_all(promo_campaign_id: promo_campaign_target.id)
    redirect_to admin_unattached_promo_registrations_path(filter: params[:promo_campaign_target]),
                notice: "Assigned #{referral_codes.count} codes to campaign '#{params[:promo_campaign_target]}'."
  end

  private

  def parse_report_dates(report_period, reporting_interval)
    start_date = Date.new(report_period["start(1i)"].to_i,
                          report_period["start(2i)"].to_i,
                          report_period["start(3i)"].to_i)
    end_date = Date.new(report_period["end(1i)"].to_i,
                        report_period["end(2i)"].to_i,
                        report_period["end(3i)"].to_i)
    {
      start_date: start_date,
      end_date: end_date
    }
  end

  def create_params
    params.require(:number_of_codes_to_create)
  end
end
{'fraction_non_alphanumeric': 0.08414239482200647, 'fraction_numerical': 0.0024894199651481204, 'mean_word_length': 3.0994897959183674, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4044440', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2302, 'n_words': 402}