<commit_before>class UserYearGrowthPresenter
  def self.for_user(user)
    years.map { |year| self.new(user, year) }
  end

  def self.for_year(year)
    User.all.map { |user| self.new(user, year) }
  end

  def self.years
    # FIXME
    [2013, 2014]
  end

  attr_reader :year, :user

  def initialize(user, year)
    @user = user
    @year = year
    @portfolio = PortfolioPresenter.all
    @ownership = OwnershipPresenter.all
  end

  def contributions(at: end_date)
    @contributions ||= @user.contributions.year(@year).to_a
    @contributions.select{|c| c.date <= at}
  end

  def gross_gains(at: end_date)
    gross_value(at: at) - contributions(at: at).sum(&:amount)
  end

  def gross_value(at: end_date)
    @ownership.user_percent(@user, at) * @portfolio.value_at(at) + booked_fee(at: at)
  end

  def benchmark_value(at: end_date)
    shares = benchmark_shares_matcher[at]
    return 0 if shares == 0
    shares * benchmark_price_matcher[at]
  end

  def unbooked_fee(at: end_date)
    (gross_value(at: at) - benchmark_value(at: at)) / 2
  end

  def booked_fee(at: end_date)
    Transfer.fees.where(date: at, from_user: @user).sum(:amount)
  end

  def value(at: end_date)
    gross_value(at: at) - unbooked_fee(at: at)
  end

  def starting_value<commit_msg>Adjust gain for starting value<commit_after>class UserYearGrowthPresenter
  def self.for_user(user)
    years.map { |year| self.new(user, year) }
  end

  def self.for_year(year)
    User.all.map { |user| self.new(user, year) }
  end

  def self.years
    # FIXME
    [2013, 2014]
  end

  attr_reader :year, :user

  def initialize(user, year)
    @user = user
    @year = year
    @portfolio = PortfolioPresenter.all
    @ownership = OwnershipPresenter.all
  end

  def contributions(at: end_date)
    @contributions ||= @user.contributions.year(@year).to_a
    @contributions.select{|c| c.date <= at}
  end

  def gross_gains(at: end_date)
    gross_value(at: at) - contributions(at: at).sum(&:amount) - starting_value
  end

  def gross_value(at: end_date)
    @ownership.user_percent(@user, at) * @portfolio.value_at(at) + booked_fee(at: at)
  end

  def benchmark_value(at: end_date)
    shares = benchmark_shares_matcher[at]
    return 0 if shares == 0
    shares * benchmark_price_matcher[at]
  end

  def unbooked_fee(at: end_date)
    (gross_value(at: at) - benchmark_value(at: at)) / 2
  end

  def booked_fee(at: end_date)
    Transfer.fees.where(date: at, from_user: @user).sum(:amount)
  end

  def value(at: end_date)
    gross_value(at: at) - unbooked_fee(at: at)
  end

  def starting_value
{'fraction_non_alphanumeric': 0.11976744186046512, 'fraction_numerical': 0.008527131782945736, 'mean_word_length': 3.560070671378092, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3215558', 'n_tokens_mistral': 1019, 'n_tokens_neox': 960, 'n_words': 210}