<commit_before>class User < Owner
  # TODO Add :omniauthable
  devise :trackable, :rememberable, :omniauthable, :omniauth_providers => [:github]
  has_and_belongs_to_many :organizations, join_table: :organizations_users
  has_many :alerts
  has_many :contributions
  has_many :scrapers_contributed_to, through: :contributions, source: :scraper

  def see_downloads
    get_feature_switch_value(:see_downloads, false)
  end

  def see_downloads=(value)
    set_feature_switch_value(:see_downloads, value)
  end

  # In most cases people have contributed to the scrapers that they own so we really don't
  # want to see these twice. This method just removes their own scrapers from the list
  def other_scrapers_contributed_to
    scrapers_contributed_to - scrapers
  end

  # A list of all owners thst this user can write to. Includes itself
  def all_owners
    [self] + organizations
  end

  def reset_authorization!
    update_attributes(access_token: Morph::Github.reset_authorization(access_token))
  end

  # Send all alerts. This method should be run from a daily cron job
  def self.process_alerts
    User.all.each do |user|
      user.process_alerts
    end
  end

  def successful_scrapers
    all_scrapers_watched.select {|s| s.last_run_finished_successfully?}
  end

  def broken_scrapers
    all_scrapers_watched.select {|s| s.last_run_finished_with_errors?}
  end

  def process_alerts
    broken_scrapers = broken_scrapers.sort do |a,b|
      if b.latest_successful_run_time.nil? && a.latest_successful_run_time.nil?
        0
      elsif b.latest_successful_run_time.nil?
        -1
      elsif a.latest_successful_run_time.nil?
        1
      else
        b.latest_successful_run_time <=> a.latest_successful_run_time
      end
    end

    unless broken_scrapers.empty?
      begin
        AlertMailer.alert_email(self, broken_scrapers, successful_scrapers.count).deliver
      rescue Net::SMTPSyntaxError
        puts "Warning: user #{nickname} has invalid email address #{email} (tried to send alert)"
      end
    end
  end

  def user?
    true
  end

  def organization?<commit_msg>Extract method<commit_after>class User < Owner
  # TODO Add :omniauthable
  devise :trackable, :rememberable, :omniauthable, :omniauth_providers => [:github]
  has_and_belongs_to_many :organizations, join_table: :organizations_users
  has_many :alerts
  has_many :contributions
  has_many :scrapers_contributed_to, through: :contributions, source: :scraper

  def see_downloads
    get_feature_switch_value(:see_downloads, false)
  end

  def see_downloads=(value)
    set_feature_switch_value(:see_downloads, value)
  end

  # In most cases people have contributed to the scrapers that they own so we really don't
  # want to see these twice. This method just removes their own scrapers from the list
  def other_scrapers_contributed_to
    scrapers_contributed_to - scrapers
  end

  # A list of all owners thst this user can write to. Includes itself
  def all_owners
    [self] + organizations
  end

  def reset_authorization!
    update_attributes(access_token: Morph::Github.reset_authorization(access_token))
  end

  # Send all alerts. This method should be run from a daily cron job
  def self.process_alerts
    User.all.each do |user|
      user.process_alerts
    end
  end

  def successful_scrapers
    all_scrapers_watched.select {|s| s.last_run_finished_successfully?}
  end

  def broken_scrapers
    all_scrapers_watched.select {|s| s.last_run_finished_with_errors?}
  end

  # Puts scrapers that have most recently failed first
  def broken_scrapers_ordered_by_urgency
    broken_scrapers.sort do |a,b|
      if b.latest_successful_run_time.nil? && a.latest_successful_run_time.nil?
        0
      elsif b.latest_successful_run_time.nil?
        -1
      elsif a.latest_successful_run_time.nil?
        1
      else
        b.latest_successful_run_time <=> a.latest_successful_run_time
      end
    end
  end

  def process_alerts
    unless broken_scrapers_ordered_by_urgency.empty?
      begin
        AlertMailer.alert_email(self, broken_scrapers_ordered_by_urgency, successful_scrapers.count).deliver
      rescue Net::SMTPSyntaxError
        puts "Warning: user #{nickname} has invalid email address #{email} (tried to send alert)"
      end
    end
  end

  def user?
    true
  end

  def organization?
{'fraction_non_alphanumeric': 0.060843512330029964, 'fraction_numerical': 0.0013828070984097719, 'mean_word_length': 3.717391304347826, 'pattern_counts': {'":': 0, '<': 7, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4966181', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1409, 'n_words': 368}