<commit_before>class Position < ActiveRecord::Base
  belongs_to :fund

  def self.update_positions
    first_day_of_month = Date.today.beginning_of_month

    # First, get funds that don't yet have positions established for this month.
    active_funds = Fund.where(active: true)
    funds_to_update = active_funds.select do |fund|
      fund.positions.where(date: first_day_of_month)
    end

    # Get prices and compute this month's positions.
    puts "#{funds_to_update.count} #{funds_to_update.count == 1 ? 'position needs' : 'positions need'} updating for this month."
    unless funds_to_update.empty?
      prices = get_month_end_prices(10)
      funds_to_update.each do |fund|
        last = prices.last[:funds]["#{fund.name} Fund"]
        avg = prices.inject(0) { |sum, data| sum + data[:funds]["#{fund.name} Fund"] } / 10
        date = prices.last[:date]
        invested = last >= avg
        position = Position.create(date: first_day_of_month, fund: fund, invested: invested, ten_month_average: avg, tenth_month_price: last, tenth_month_price_date: date)
        puts "Updated position for #{position.date}: #{position.fund.name} Fund, Invested: #{position.invested}"
      end
    end
  end

  def self.get_month_end_prices(num_months)
    now = Date.today<commit_msg>correctly look up funds that need updating<commit_after>class Position < ActiveRecord::Base
  belongs_to :fund

  def self.update_positions
    first_day_of_month = Date.today.beginning_of_month

    # First, get funds that don't yet have positions established for this month.
    active_funds = Fund.where(active: true)
    funds_to_update = active_funds.select do |fund|
      fund if fund.positions.where(date: first_day_of_month).empty?
    end

    # Get prices and compute this month's positions.
    puts "#{funds_to_update.count} #{funds_to_update.count == 1 ? 'position needs' : 'positions need'} updating for this month."
    unless funds_to_update.empty?
      prices = get_month_end_prices(10)
      funds_to_update.each do |fund|
        last = prices.last[:funds]["#{fund.name} Fund"]
        avg = prices.inject(0) { |sum, data| sum + data[:funds]["#{fund.name} Fund"] } / 10
        date = prices.last[:date]
        invested = last >= avg
        position = Position.create(date: first_day_of_month, fund: fund, invested: invested, ten_month_average: avg, tenth_month_price: last, tenth_month_price_date: date)
        puts "Updated position for #{position.date}: #{position.fund.name} Fund, Invested: #{position.invested}"
      end
    end
  end

  def self.get_month_end_prices(num_months)
    now = Date.today
{'fraction_non_alphanumeric': 0.10779816513761468, 'fraction_numerical': 0.0045871559633027525, 'mean_word_length': 3.7068345323741005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '865112', 'n_tokens_mistral': 856, 'n_tokens_neox': 831, 'n_words': 211}