<commit_before># == Schema Information
#
# Table name: icu_ratings
#
#  id              :integer(4)  not null, primary key
#  icu_id          :integer(4)
#  rating          :integer(2)
#  full            :boolean     default(FALSE)
#  last_rating     :integer(2)
#  last_full       :boolean     default(FALSE)
#  games           :integer(2)
#

class LiveRating < ActiveRecord::Base
  extend ICU::Util::Pagination
  include Hightide

  belongs_to :icu_player, foreign_key: "icu_id"

  validates :icu_id, numericality: { only_integer: true, greater_than_or_equal: 0 }, uniqueness: true
  validates :rating, numericality: { only_integer: true }
  validates :last_rating, numericality: { only_integer: true }, allow_nil: true
  validates :games, numericality: { only_integer: true, greater_than_or_equal: 0 }
  validates :full, inclusion: { in: [true, false] }
  validates :last_full, inclusion: { in: [true, false] }, allow_nil: true

  default_scope -> { includes(:icu_player).joins(:icu_player).order("rating DESC, icu_players.last_name") }

  # Adapted from IcuRating#search.
  def self.search(params, path, paginated=true)
    matches = all
    matches = matches.where("first_name LIKE ?", "%#{params[:first_name]}%") if params[:first_name].present?
    matches = matches.where("last_name LIKE ?", "%#{params[:last_name]}%") if params[:last_name].present?
    matches = matches.where("club IS NULL") if params[:club] == "None"
    matches = matches.where("club = ?", params[:club]) if params[:club].present? && params[:club] != "None"
    matches = matches.where("gender = 'M' OR gender IS NULL") if params[:gender] == "M"
    matches = matches.where("gender = 'F'") if params[:gender] == "F"
    matches = matches.where(icu_id: params[:icu_id].to_i) if params[:icu_id].to_i > 0
    matches = matches.where(full: params[:type] == "full") if params[:type].present?
    matches = IcuPlayer.search_fed(matches, params[:fed])
    return matches unless paginated
    paginate(matches, path, params)
  end

  def self.recalculate
    icu_ids = get_subscriptions
    last_list = get_last_list
    tournament_ratings = get_tournament_ratings(icu_ids)
    published_ratings = get_published_ratings(last_list)
    legacy_ratings = get_legacy_ratings(icu_ids.reject{ |id| tournament_ratings[id] })
    recent_games = get_recent_games(tournament_ratings.keys, last_list)
    current = unscoped.to_a.each_with_object({}) { |live_rating, hash| hash[live_rating.icu_id] = live_rating }<commit_msg>Removed deceased players from live ratings<commit_after># == Schema Information
#
# Table name: icu_ratings
#
#  id              :integer(4)  not null, primary key
#  icu_id          :integer(4)
#  rating          :integer(2)
#  full            :boolean     default(FALSE)
#  last_rating     :integer(2)
#  last_full       :boolean     default(FALSE)
#  games           :integer(2)
#

class LiveRating < ActiveRecord::Base
  extend ICU::Util::Pagination
  include Hightide

  belongs_to :icu_player, foreign_key: "icu_id"

  validates :icu_id, numericality: { only_integer: true, greater_than_or_equal: 0 }, uniqueness: true
  validates :rating, numericality: { only_integer: true }
  validates :last_rating, numericality: { only_integer: true }, allow_nil: true
  validates :games, numericality: { only_integer: true, greater_than_or_equal: 0 }
  validates :full, inclusion: { in: [true, false] }
  validates :last_full, inclusion: { in: [true, false] }, allow_nil: true

  default_scope -> { includes(:icu_player).joins(:icu_player).order("rating DESC, icu_players.last_name") }

  # Adapted from IcuRating#search.
  def self.search(params, path, paginated=true)
    matches = all
    matches = matches.where("deceased = ?", false)
    matches = matches.where("first_name LIKE ?", "%#{params[:first_name]}%") if params[:first_name].present?
    matches = matches.where("last_name LIKE ?", "%#{params[:last_name]}%") if params[:last_name].present?
    matches = matches.where("club IS NULL") if params[:club] == "None"
    matches = matches.where("club = ?", params[:club]) if params[:club].present? && params[:club] != "None"
    matches = matches.where("gender = 'M' OR gender IS NULL") if params[:gender] == "M"
    matches = matches.where("gender = 'F'") if params[:gender] == "F"
    matches = matches.where(icu_id: params[:icu_id].to_i) if params[:icu_id].to_i > 0
    matches = matches.where(full: params[:type] == "full") if params[:type].present?
    matches = IcuPlayer.search_fed(matches, params[:fed])
    return matches unless paginated
    paginate(matches, path, params)
  end

  def self.recalculate
    icu_ids = get_subscriptions
    last_list = get_last_list
    tournament_ratings = get_tournament_ratings(icu_ids)
    published_ratings = get_published_ratings(last_list)
    legacy_ratings = get_legacy_ratings(icu_ids.reject{ |id| tournament_ratings[id] })
    recent_games = get_recent_games(tournament_ratings.keys, last_list)
    current = unscoped.to_a.each_with_object({}) { |live_rating, hash| hash[live_rating.icu_id] = live_rating }
{'fraction_non_alphanumeric': 0.13337296868806975, 'fraction_numerical': 0.0031708283789139914, 'mean_word_length': 4.421052631578948, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4978379', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1664, 'n_words': 368}