<commit_before># Best All-around Rider Competition. Assigns points for each top-15 placing in major Disciplines: road, track, etc.
# Calculates a BAR for each Discipline, and an Overall BAR that combines all the discipline BARs.
# Also calculates a team BAR.
# Recalculating the BAR at years-end can take nearly 30 minutes even on a fast server. Recalculation must be manually triggered.
# This class implements a number of OBRA-specific rules and should be generalized and simplified.
# The BAR categories and disciplines are all configured in the databsase. Race categories need to have a bar_category_id to 
# show up in the BAR; disciplines must exist in the disciplines table and discipline_bar_categories.
# FIXME This documentation is out of date
# TODO Consider using parent BAR again with child Competitions. Should that just be Overall BAR?
class Bar < Competition

  # TODO Add add_child(...) to Race
  # check placings for ties
  # remove one-day licensees
  
  validate :valid_dates

  def Bar.calculate!(year = Date.today.year)
    benchmark(name, Logger::INFO, false) {
      transaction do
        year = year.to_i if year.is_a?(String)
        date = Date.new(year, 1, 1)

        # Age Graded BAR and Overall BAR do their own calculations
        Discipline.find_all_bar.reject {|discipline| discipline == Discipline[:age_graded] || discipline == Discipline[:overall]}.each do |discipline|
          bar = Bar.find(:first, :conditions => { :date => date, :discipline => discipline.name })
          unless bar
            bar = Bar.create!(
              :name => "#{year} #{discipline.name} BAR",
              :date => date,
              :discipline => discipline.name
            )
          end
        end

        Bar.find(:all, :conditions => { :date => date }).each do |bar|
          bar.destroy_races
          bar.create_races
          # Could bulk load all Event and Races at this point, but hardly seems to matter
          bar.calculate_members_only_places
          bar.calculate!
        end
      end
    }
    # Don't return the entire populated instance!
    true
  end
  
  # Expire BAR web pages from cache. Expires *all* BAR pages. Shouldn't be in the model, either
  # BarSweeper seems to fire, but does not expire pages?
  def Bar.expire_cache
    FileUtils::rm_rf("#{RAILS_ROOT}/public/bar.html")
    FileUtils::rm_rf("#{RAILS_ROOT}/public/bar")
  end
  
  def Bar.find_by_year_and_discipline(year, discipline_name)<commit_msg>Remove unneeded call to calculate_member_places in BAR<commit_after># Best All-around Rider Competition. Assigns points for each top-15 placing in major Disciplines: road, track, etc.
# Calculates a BAR for each Discipline, and an Overall BAR that combines all the discipline BARs.
# Also calculates a team BAR.
# Recalculating the BAR at years-end can take nearly 30 minutes even on a fast server. Recalculation must be manually triggered.
# This class implements a number of OBRA-specific rules and should be generalized and simplified.
# The BAR categories and disciplines are all configured in the databsase. Race categories need to have a bar_category_id to 
# show up in the BAR; disciplines must exist in the disciplines table and discipline_bar_categories.
# FIXME This documentation is out of date
# TODO Consider using parent BAR again with child Competitions. Should that just be Overall BAR?
class Bar < Competition

  # TODO Add add_child(...) to Race
  # check placings for ties
  # remove one-day licensees
  
  validate :valid_dates

  def Bar.calculate!(year = Date.today.year)
    benchmark(name, Logger::INFO, false) {
      transaction do
        year = year.to_i if year.is_a?(String)
        date = Date.new(year, 1, 1)

        # Age Graded BAR and Overall BAR do their own calculations
        Discipline.find_all_bar.reject {|discipline| discipline == Discipline[:age_graded] || discipline == Discipline[:overall]}.each do |discipline|
          bar = Bar.find(:first, :conditions => { :date => date, :discipline => discipline.name })
          unless bar
            bar = Bar.create!(
              :name => "#{year} #{discipline.name} BAR",
              :date => date,
              :discipline => discipline.name
            )
          end
        end

        Bar.find(:all, :conditions => { :date => date }).each do |bar|
          bar.destroy_races
          bar.create_races
          # Could bulk load all Event and Races at this point, but hardly seems to matter
          bar.calculate!
        end
      end
    }
    # Don't return the entire populated instance!
    true
  end
  
  # Expire BAR web pages from cache. Expires *all* BAR pages. Shouldn't be in the model, either
  # BarSweeper seems to fire, but does not expire pages?
  def Bar.expire_cache
    FileUtils::rm_rf("#{RAILS_ROOT}/public/bar.html")
    FileUtils::rm_rf("#{RAILS_ROOT}/public/bar")
  end
  
  def Bar.find_by_year_and_discipline(year, discipline_name)
{'fraction_non_alphanumeric': 0.08115766039263307, 'fraction_numerical': 0.0024286581663630845, 'mean_word_length': 2.956765412329864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '7461741', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1352, 'n_words': 540}