fix(analytics): нереалистичная годовая доходность не валит пересчёт метрик

XIRR за несколько дней истории даёт артефакт солвера за ±100 000 000 %, который переполнял NUMERIC(24,10) и ронял весь refresh. Такое значение теперь считается «нет ответа».
This commit is contained in:
Dmitry
2026-09-19 21:54:36 +03:00
parent 3b3ee4d682
commit 1b521be35e
2 changed files with 20 additions and 1 deletions
+8 -1
View File
@@ -58,6 +58,10 @@ ZERO = Decimal(0)
ONE = Decimal(1)
DAYS_IN_YEAR = Decimal(365)
RATE_PLACES = Decimal("0.000001")
MAX_ABS_RATE = Decimal(10**6)
"""An annualised rate beyond ±100 000 000 % is a solver artefact (a few days of history, or
flows with several sign changes), not a return. It is reported as «no answer»: stored, it would
also overflow the NUMERIC(24,10) column and take the whole metrics refresh down with it."""
PERIODS: tuple[str, ...] = ("1m", "3m", "6m", "ytd", "1y", "3y", "all")
@@ -205,7 +209,10 @@ def annualize(cumulative: Decimal | None, days: int) -> Decimal | None:
def _as_rate(value: float) -> Decimal | None:
try:
return _quantize(Decimal(repr(value)))
rate = Decimal(repr(value))
if not rate.is_finite() or abs(rate) > MAX_ABS_RATE:
return None
return _quantize(rate)
except (InvalidOperation, ValueError):
return None