Cache last known BTC price and show stale warning in UI
When the CoinGecko API fails, fall back to the last successful price instead of 0.0, and surface a warning indicator on the price card. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -54,7 +54,12 @@ def aggregate_to_daily(raw: list) -> dict:
|
||||
return by_date
|
||||
|
||||
|
||||
def get_btc_price_eur() -> float:
|
||||
_last_known_price: float = 0.0
|
||||
|
||||
|
||||
def get_btc_price_eur() -> tuple[float, bool]:
|
||||
"""Returns (price, is_cached). is_cached=True when using a stale fallback."""
|
||||
global _last_known_price
|
||||
try:
|
||||
resp = requests.get(
|
||||
"https://api.coingecko.com/api/v3/simple/price",
|
||||
@@ -62,7 +67,9 @@ def get_btc_price_eur() -> float:
|
||||
timeout=10,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return float(resp.json()["bitcoin"]["eur"])
|
||||
price = float(resp.json()["bitcoin"]["eur"])
|
||||
_last_known_price = price
|
||||
return price, False
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to fetch BTC price: {e}")
|
||||
return 0.0
|
||||
return _last_known_price, True
|
||||
|
||||
Reference in New Issue
Block a user