Show net worth velocity and momentum beside the net worth (#129, 10.2)

Merged
JA
2mo ago
+457-0

Summary

Part of #129 (10.2). The dashboard's net worth section shows the current value and the change over the period. It does not show how fast net worth is moving, or whether that pace is speeding up or slowing down. This adds both, beside the figure: velocity (change per month over the period) and momentum (that pace against the period immediately before it).

Plan and decisions are in the #129 body (triage plan, 10.2).

Scope and dependencies

  • Issue: #129 (10.2). Does not close it; 10.1 is #287, 10.3 is #289.
  • Parent PR/branch, if stacked: none. Off main at c783118. Independent of 10.1 and 10.3.
  • Intentionally out of scope: the Reports page, the API, an all-time period (no prior window exists), and any new colour semantics (the figures are neutral text with an explicit sign).

What changed

  • BalanceSheet::NetWorthVelocity (balance_sheet, period: both injected):
    • reads BalanceSheet::NetWorthBreakdownSeriesBuilder#breakdown_series, as the issue specifies. Its first and last points are always the window's own start and end dates whatever the interval (the series always includes both), so the figure does not depend on the monthly interval;
    • velocity = (last value minus first value) / days between them, scaled to a month of 365.2425 / 12 = 30.436875 days (DAYS_PER_MONTH);
    • momentum = velocity minus the prior window's velocity; the prior window is Period.custom of the same length ending the day before period.start_date;
    • both are nil unless every account the series is built from (BalanceSheet::HistoricalAccountScope, so a viewer who sees only some accounts is judged on those) has history covering the whole window. The guard takes the latest of the accounts' first entries, not the earliest: one account with a long history must not vouch for another that opens inside the window, whose balance arrives from nothing and would read as growth. Accounts with no entries add nothing to the series and do not hold it back. Pending transactions are left out of the entry dates (Entry.excluding_pending), as the series is not drawn from them. Without the guard, the zeros before the first entry would read as a flat stretch and momentum would simply equal velocity.
  • UI::NetWorthPace ViewComponent, rendered in the net worth header's free right-hand side so the widget's height does not change. It renders nothing when velocity is nil, and leaves momentum out when it is nil. The sign follows the rounded amount, so nothing prints as -$0.00.
  • Locale: UI.net_worth_pace.* in en and de (other locales fall back to en, as with rate_change_table). A component test renders in German.

History of the source: the first version read BalanceSheet#net_worth_series (the series the dashboard chart uses) on the grounds that the breakdown also builds per-group series the card does not need. It was reworked to follow the issue table, accepting the cost below.

Blast radius

Who else hits these lines: _net_worth_chart is rendered only from the dashboard (grepped). The component renders nothing without a full period of history, so such a family sees the section exactly as before.

Cost of using the breakdown series: each call builds the per-group series too, which velocity does not use, and the dashboard asks for two windows (this period and the one before). Both are cached per period and user, and the Reports page already builds the same series, so a user who has opened Reports for the same period shares the cache for the current window. Also one grouped entries query for the guard. The prior window's key is not warmed by the Reports page, and every sync empties both, so a cold dashboard load pays for two builds; the guard runs first, so a family whose history does not cover the window pays one grouped query only. Not measured on a large family.

Siblings: history_covers? is the only reader of the entry dates here. NetWorthSeriesBuilder and NetWorthBreakdownSeriesBuilder are untouched; the Reports page keeps its own trend. The breakdown hands values over already rounded to the currency's display precision, so there is a sub-cent rounding in the inputs; Money#format is the only rounding applied here.

Consequence worth knowing: a family that adds an account sees no velocity until that account is older than the period (older than twice it, for momentum). That is the guard working, and a deliberate trade against showing a figure that is mostly the new account's opening balance. "All time" starts at the family's oldest entry, so it shows a figure only when every account opened on that same day; in practice it is usually blank.

Evidence checklist

  • Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
  • Contract rows touched by this change name existing tests (see Validation).
  • Any gate test has an observed failing mutation transcript attached. Mutations below.
  • Fixtures used as evidence assert internal consistency: the model tests build one balance per day on an otherwise empty family, so every expected figure is derivable from the test's own data.
  • Financial calculations use BigDecimal and documented rounding points. BigDecimal throughout; the breakdown's display rounding is documented above, and for_display decides the sign shown.
  • De-identification: no personal data in the diff.
  • Files changed, decisions, tests/runtime, risks and next unblocked issue documented below.

Validation

Fail-first: the 14 model tests errored on the missing class before it existed; the two tests added for the first review (another account opening inside the period; a pending transaction before the history) failed against the first guard; the source test added for the rework (stubbed breakdown windows, one per window) failed against the net_worth_series version. Then each behaviour broken deliberately, one at a time, and the named test went red:

MutationTest that failed
momentum subtraction reversedmomentum positive when growth speeds up; flips sign when growth slows; equals the difference
drop the history guardwithheld when history starts inside the period; inside the prior period; no history
guard <= to < (boundary)momentum available when history starts exactly where the prior period does
earliest instead of latest account startvelocity withheld when any account's history starts inside the period
drop excluding_pendinga pending transaction dated before the history does not count
prior window ends a day lateprior period is the same length, ending the day before; momentum tests
drop the per-month scalingvelocity per month; momentum difference
span from period length instead of the pointsvelocity per month; momentum difference
read a series other than the breakdownvelocity and momentum are read from the breakdown series, one window each
sign from the unrounded amountan amount that rounds to zero carries no sign
momentum always shownmomentum left out when it cannot be read

Exit criteria: velocity for a flat series is 0 (a flat series has zero velocity); momentum flips sign when growth slows (momentum flips sign when growth slows, which also asserts velocity stays positive). The dashboard renders the figures (PagesControllerTest, model stubbed), and the real fixture family exercises the unstubbed path in the existing dashboard test, where the figures are withheld. The figure being shown from real balances is covered at the model level only; there is no un-stubbed dashboard test that shows one.

Commands (Ruby 3.3.6 with the Gemfile's ruby line removed, local Postgres and Redis; see Risks):

  • bin/rails test, full suite, run before the review and rework commits: 11373 runs, 49222 assertions, 4 failures, 1 error, 33 skips. All 5 also fail on an untouched main in this environment: 3 in Settings::HostingsControllerTest and 1 in Provider::RegistryTest read ANTHROPIC_BASE_URL (set in this sandbox), and Provider::OpenaiTest expects Ruby 3.4's Hash#inspect. After the rework the velocity model, UI::NetWorthPace, PagesControllerTest, the other balance-sheet tests and ReportsControllerTest were rerun: 125 runs, 0 failures. After the German strings: UI::NetWorthPace and I18nTest rerun, 13 runs, 0 failures, 4 skips.
  • CI on 854946f passed on Ruby 3.4.9: test_unit, test_system, lint, lint_js, scan_ruby, scan_js, CodeQL and Codacy. The latest push (2921d34, German strings and one test only) is re-running and is checked before this is marked ready.
  • bin/rubocop on the changed files: no offenses. bin/brakeman --no-pager: no warnings. erb_lint on the two changed ERB files (earlier commits): no errors.

Not run: DISABLE_PARALLELIZATION=true bin/rails test:system locally (no Selenium here; CI ran it), npm run lint (no JavaScript changed). No screenshots: the layout, which relies on the header's free right-hand side, was not opened in a browser and should be looked at before this leaves draft.

Risks, blockers, and follow-up

  • Verified locally under Ruby 3.3.6, not the 3.4.9 in .ruby-version (3.4.9's source host is blocked in this environment). CI runs 3.4.9 and passed on 854946f.
  • Gatekeeper review: a comment from the Gatekeeper account on this draft (head 854946f) approves it. Its identifiers were checked against the code (HistoricalAccountScope#account_ids, Entry.excluding_pending, NetWorthBreakdownSeriesBuilder#breakdown_series all exist). It is not the owner's approval, and a full Gatekeeper review is still due when the PR is marked ready. Its prose note (the month length written as 30.4375 where the constant is 30.436875) is corrected above.
  • Upstream we-promise/sure's AGENTS.md was not read (outside this session's repository scope); per rule 3 it must be before this is marked ready.
  • A family younger than the selected period (or than twice it, for momentum), or one that recently added an account, sees no figures yet.
  • Dashboard cost: two cached breakdown builds per load on a cold cache (see Blast radius); worth watching, not changed here.
  • Next unblocked: 10.1 (#287) and 10.3 (#289).