<commit_before>class Poll::Stats
  include Statisticable
  alias_method :poll, :resource

  def self.stats_methods
    super +
      %i[total_valid_votes total_white_votes total_null_votes
         total_participants_web total_web_valid total_web_white total_web_null
         total_participants_booth total_booth_valid total_booth_white total_booth_null
         total_participants_web_percentage total_participants_booth_percentage
         valid_percentage_web valid_percentage_booth total_valid_percentage
         white_percentage_web white_percentage_booth total_white_percentage
         null_percentage_web null_percentage_booth total_null_percentage]
  end

  def total_participants
    total_participants_web + total_participants_booth
  end

  %i[web booth].each do |channel|
    define_method :"total_participants_#{channel}" do
      send(:"total_#{channel}_valid") +
        send(:"total_#{channel}_white") +
        send(:"total_#{channel}_null")
    end

    define_method :"total_participants_#{channel}_percentage" do
      calculate_percentage(send(:"total_participants_#{channel}"), total_participants)
    end
  end

  def total_web_valid
    voters.where(origin: "web").count - total_web_white
  end

  def total_web_white
    0
  end

  def total_web_null
    0
  end

  def total_booth_valid
    recounts.sum(:total_amount)
  end

  def total_booth_white
    recounts.sum(:white_amount)
  end

  def total_booth_null
    recounts.sum(:null_amount)
  end

  def valid_percentage_web
    calculate_percentage(total_web_valid, total_valid_votes)
  end

  def white_percentage_web
    calculate_percentage(total_web_white, total_white_votes)
  end

  def null_percentage_web
    calculate_percentage(total_web_null, total_null_votes)
  end

  %i[valid white null].each do |type|
    define_method :"#{type}_percentage_booth" do
      calculate_percentage(send(:"total_booth_#{type}"), send(:"total_#{type}_votes"))
    end

    define_method :"total_#{type}_votes" do
      send(:"total_web_#{type}") + send(:"total_booth_#{type}")
    end

    define_method :"total_#{type}_percentage" do
      calculate_percentage(send(:"total_#{type}_votes"), total_participants)
    end
  end

  private

    def participants
      User.where(id: voters.pluck(:user_id))
    end

    def voters<commit_msg>Remove duplication in web percentage stats<commit_after>class Poll::Stats
  include Statisticable
  alias_method :poll, :resource

  def self.stats_methods
    super +
      %i[total_valid_votes total_white_votes total_null_votes
         total_participants_web total_web_valid total_web_white total_web_null
         total_participants_booth total_booth_valid total_booth_white total_booth_null
         total_participants_web_percentage total_participants_booth_percentage
         valid_percentage_web valid_percentage_booth total_valid_percentage
         white_percentage_web white_percentage_booth total_white_percentage
         null_percentage_web null_percentage_booth total_null_percentage]
  end

  def total_participants
    total_participants_web + total_participants_booth
  end

  %i[web booth].each do |channel|
    define_method :"total_participants_#{channel}" do
      send(:"total_#{channel}_valid") +
        send(:"total_#{channel}_white") +
        send(:"total_#{channel}_null")
    end

    define_method :"total_participants_#{channel}_percentage" do
      calculate_percentage(send(:"total_participants_#{channel}"), total_participants)
    end
  end

  def total_web_valid
    voters.where(origin: "web").count - total_web_white
  end

  def total_web_white
    0
  end

  def total_web_null
    0
  end

  def total_booth_valid
    recounts.sum(:total_amount)
  end

  def total_booth_white
    recounts.sum(:white_amount)
  end

  def total_booth_null
    recounts.sum(:null_amount)
  end

  %i[valid white null].each do |type|
    %i[web booth].each do |channel|
      define_method :"#{type}_percentage_#{channel}" do
        calculate_percentage(send(:"total_#{channel}_#{type}"), send(:"total_#{type}_votes"))
      end
    end

    define_method :"total_#{type}_votes" do
      send(:"total_web_#{type}") + send(:"total_booth_#{type}")
    end

    define_method :"total_#{type}_percentage" do
      calculate_percentage(send(:"total_#{type}_votes"), total_participants)
    end
  end

  private

    def participants
      User.where(id: voters.pluck(:user_id))
    end

    def voters
{'fraction_non_alphanumeric': 0.08049605411499436, 'fraction_numerical': 0.0009019165727170237, 'mean_word_length': 4.2559241706161135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2725083', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1480, 'n_words': 252}