Skip to content

fix: compute portfolio dayChange from analytics snapshots instead of Math.random - #76

Open
devgodfreytem wants to merge 1 commit into
grantFoxin:mainfrom
devgodfreytem:fix/real-day-change-57
Open

devgodfreytem wants to merge 1 commit into
grantFoxin:mainfrom
devgodfreytem:fix/real-day-change-57

Conversation

@devgodfreytem

Copy link
Copy Markdown
Contributor

Closes #57

What changed

  • StellarService.getPortfolio(): dayChange (what /portfolio/:id and the dashboard show) no longer comes from Math.random(). calculateDayChange now calls analyticsService.getChangeForWindow(portfolioId, 24h, 2h). That compares the analytics_snapshots row closest to 24h ago, within a 2h window, with the latest row. It's the same calculation calculatePerformanceMetrics already uses for dailyChange.
  • No history: with no recorded history, dayChange is null, not a made-up number. Dashboard.tsx and PerformanceChart.tsx already render null as "—".
  • New test: backend/src/test/dayChange.test.ts checks that the snapshot change is used with the 24h/2h window, that Math.random is never called, that the value is null without history, and that it's stable across calls.

Why

  • The 24h change shown on the dashboard was a fresh random number on every request.

How to test

  • cd backend && npm test -- dayChange

Notes:

  • Not run locally; I'm relying on CI.
  • The rest of the issue was already on main: dbGetSnapshotAt/dbGetClosestSnapshot, getChangeForWindow, the 7d/30d/all-time changes, and the frontend's null handling. This PR removes the last random value on the portfolio path.
  • Untouched: frontend/src/components/AssetCard.tsx and frontend/src/services/browserPriceService.ts still use Math.random() for per-asset demo price changes. They aren't part of this issue's portfolio path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: calculateDayChange returns random numbers instead of real price history

1 participant