CategoryStats (category_stats.rb)

maybe · analytics

Computes descriptive statistics (count, mean, etc.) for categories in financial model.

class CategoryStats
  attr_reader :family

  def initialize(family)
    @family = family
  end

  def avg_monthly_total_for(category)
    statistics_data[category.id]&.avg || 0
  end

  def median_monthly_total_for(category)
    statistics_data[category.id]&.median || 0
  end

  def month_total_for(category, date: Date.current)
    monthly_totals = totals_data[category.id]

    category_total = monthly_totals&.find { |mt| mt.month == date.month && mt.year == date.year }

    category_total&.amount || 0
  end

  def month_category_totals(date: Date.current)
    by_classification = Hash.new { |h, k| h[k] = {} }

    totals_data.each_with_object(by_classification) do |(category_id, totals), result|
      totals.each do |t|
        next unless t.month == date.month && t.year == date.year
     

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key