79b565cfb6
- Store daily BTC OHLC candles in SQLite to avoid hitting CoinGecko on every load - Seed with 30 days of daily candles on first boot (free tier gives daily granularity for days<=30) - Auto-detect and replace coarse legacy candle data on startup - Daily refresh adds new candles on each container restart - New GET /candles endpoint (supports ?days=365 and ?days=all) - Switch BTCHistoryChart to BTCCandlestickChart using lightweight-charts (TradingView) - Purchase markers with nearest-candle matching and multi-purchase merging - Fullscreen mode showing all stored candles - Fix frontend port to 3001 and pass REACT_APP_API_URL as build arg Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..database import get_db
|
|
from .. import models
|
|
from ..dependencies import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/candles")
|
|
def get_candles(
|
|
days: str = Query(default="365"),
|
|
db: Session = Depends(get_db),
|
|
current_user: models.User = Depends(get_current_user),
|
|
):
|
|
query = db.query(models.OHLCCandle).order_by(models.OHLCCandle.date.asc())
|
|
|
|
if days != "all":
|
|
try:
|
|
limit = int(days)
|
|
except ValueError:
|
|
limit = 365
|
|
# Fetch the most recent `limit` rows
|
|
total = query.count()
|
|
if total > limit:
|
|
query = query.offset(total - limit)
|
|
|
|
candles_db = query.all()
|
|
candles = [
|
|
{
|
|
"date": c.date,
|
|
"open": round(c.open, 2),
|
|
"high": round(c.high, 2),
|
|
"low": round(c.low, 2),
|
|
"close": round(c.close, 2),
|
|
}
|
|
for c in candles_db
|
|
]
|
|
|
|
purchases_db = db.query(models.Purchase).filter(
|
|
models.Purchase.user_id == current_user.id
|
|
).all()
|
|
purchases = [
|
|
{
|
|
"date": p.created_at.strftime("%Y-%m-%d"),
|
|
"amount_eur": round(p.amount_eur, 2),
|
|
"price_eur": round(p.price_eur, 2),
|
|
}
|
|
for p in purchases_db
|
|
]
|
|
|
|
return {"candles": candles, "purchases": purchases}
|