The personal finance app for everyone (by everyone)
Ruby
fix(insights): stop celebrating a lower net worth milestone on the way down (#5123)
David Gil7c8faf277db0
diff --git a/app/models/insight/generators/net_worth_milestone_generator.rb b/app/models/insight/generators/net_worth_milestone_generator.rbindex 5ba4bf363d..579c38f790 100644--- a/app/models/insight/generators/net_worth_milestone_generator.rb+++ b/app/models/insight/generators/net_worth_milestone_generator.rb@@ -1,7 +1,13 @@# Celebrates the family's net worth crossing a round-number milestone within-# the last 30 days. The dedup key is the milestone amount itself, so a-# milestone row is reused for life: a dismissal is permanent, and only a-# dip-below-and-recross within a 30-day window can resurface an expired one.+# the last 30 days. A crossing counts only when net worth had stayed below the+# milestone for the 30 days before it and has held at or above it since, and+# only for the highest milestone net worth reached in the window. So a drop+# never surfaces a lower milestone passed earlier, and a dip-and-recovery does+# not announce the same milestone twice.+#+# The dedup key is the milestone amount itself, so a milestone row is reused+# for life: a dismissal is permanent, and an expired one resurfaces only after+# net worth spent a full window below the milestone and crossed it again.class Insight::Generators::NetWorthMilestoneGenerator < Insight::Generatorproduces "net_worth_milestone"@@ -13,17 +19,22 @@ class Insight::Generators::NetWorthMilestoneGenerator < Insight::Generator500_000, 1_000_000, 2_500_000, 5_000_000, 10_000_000].freeze- def generate- series = balance_sheet.net_worth_series(period: Period.last_30_days)- values = series.values- return [] if values.size < 2+ # How long net worth must have stayed below a milestone before crossing it+ # for the crossing to count. Matches the look-back window.+ QUIET_PERIOD = 30.days++ Point = Data.define(:date, :amount)- current = money_amount(values.last.value)- previous = money_amount(values.first.value)- return [] if current <= previous+ def generate+ window = Period.last_30_days+ history = net_worth_history(window)+ return [] if history.size < 2- milestone = MILESTONES.select { |m| previous < m && current >= m }.max- return [] unless milestone+ current = history.last.amount+ peak = history.select { |point| point.date >= window.start_date }.map(&:amount).max+ milestone = MILESTONES.select { |m| m <= peak }.max+ return [] unless milestone && current >= milestone+ return [] unless freshly_crossed?(history, milestone, window)[build_insight(@@ -41,13 +52,36 @@ class Insight::Generators::NetWorthMilestoneGenerator < Insight::Generatormetadata: {milestone: milestone},- period: Period.last_30_days,+ period: window,dedup_key: "net_worth_milestone:#{milestone}")]endprivate+ # The window plus the quiet period before it, so a crossing near the start+ # of the window can still be checked against the days that preceded it.+ def net_worth_history(window)+ period = Period.custom(start_date: window.start_date - QUIET_PERIOD, end_date: window.end_date)+ balance_sheet.net_worth_series(period: period).values.map do |value|+ Point.new(date: value.date, amount: money_amount(value.value))+ end+ end++ # The last upward crossing must fall inside the window, and net worth must+ # have stayed below the milestone for the quiet period before it. Callers+ # guarantee current >= milestone, so it has held since that crossing.+ def freshly_crossed?(history, milestone, window)+ last_below = history.rindex { |point| point.amount < milestone }+ return false unless last_below++ crossed_on = history[last_below + 1].date+ quiet_since = crossed_on - QUIET_PERIOD++ crossed_on > window.start_date &&+ history.none? { |point| point.date >= quiet_since && point.date < crossed_on && point.amount >= milestone }+ end+def money_amount(value)(value.respond_to?(:amount) ? value.amount : value).to_denddiff --git a/test/models/insight/generators/net_worth_milestone_generator_test.rb b/test/models/insight/generators/net_worth_milestone_generator_test.rbnew file mode 100644index 0000000000..39fb2b4d27--- /dev/null+++ b/test/models/insight/generators/net_worth_milestone_generator_test.rb@@ -0,0 +1,78 @@+require "test_helper"++class Insight::Generators::NetWorthMilestoneGeneratorTest < ActiveSupport::TestCase+ include BalanceTestHelper++ setup do+ @family = families(:dylan_family)+ @account = accounts(:depository)+ Balance.where(account: @family.accounts).delete_all+ end++ test "celebrates a milestone crossed upwards within the last 30 days" do+ net_worth 60 => 240_000, 10 => 255_000++ assert_equal [ "net_worth_milestone:250000" ], dedup_keys+ end++ test "celebrates only the highest milestone when several are crossed at once" do+ net_worth 60 => 90_000, 5 => 263_000++ assert_equal [ "net_worth_milestone:250000" ], dedup_keys+ end++ test "stays quiet when net worth went down" do+ net_worth 60 => 255_000, 10 => 240_000++ assert_empty dedup_keys+ end++ test "a drop below a milestone does not celebrate a lower one passed earlier" do+ net_worth 60 => 90_000, 20 => 263_000, 2 => 244_000++ assert_empty dedup_keys+ end++ test "a recovery above a milestone shortly after a dip does not celebrate it again" do+ net_worth 60 => 240_000, 20 => 255_000, 10 => 248_000, 3 => 252_000++ assert_empty dedup_keys+ end++ test "a recovery after a dip that started before the window does not celebrate it again" do+ net_worth 60 => 255_000, 33 => 245_000, 5 => 252_000++ assert_empty dedup_keys+ end++ test "celebrates a milestone regained after a full window below it" do+ net_worth 60 => 255_000, 45 => 245_000, 5 => 252_000++ assert_equal [ "net_worth_milestone:250000" ], dedup_keys+ end++ test "a higher milestone reached before the window does not hide one regained inside it" do+ net_worth 60 => 510_000, 40 => 245_000, 2 => 252_000++ assert_equal [ "net_worth_milestone:250000" ], dedup_keys+ end++ test "a milestone crossed before the window is no longer news" do+ net_worth 60 => 240_000, 35 => 255_000++ assert_empty dedup_keys+ end++ private+ # Sets the family's net worth from N days ago onwards; each value holds+ # until the next one.+ def net_worth(values_by_days_ago)+ values_by_days_ago.each do |days_ago, amount|+ create_balance(account: @account, date: days_ago.days.ago.to_date, balance: amount)+ end+ end++ def dedup_keys+ Insight::Generators::NetWorthMilestoneGenerator.new(@family).generate.map(&:dedup_key)+ end+end