Merge feature/btc-history-chart: BTC candlestick chart with local OHLC storage
Resolves conflicts: combined admin + candles routers in main.py, fixed docker-compose port to 3001:3001, restored admin styles in Dashboard. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,8 @@ from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy import text
|
||||
|
||||
from .database import engine, Base
|
||||
from .routes import users, purchases, stats, history, admin
|
||||
from .database import engine, Base, SessionLocal
|
||||
from .routes import users, purchases, stats, history, admin, candles
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
@@ -22,16 +22,26 @@ app.include_router(purchases.router)
|
||||
app.include_router(stats.router)
|
||||
app.include_router(history.router)
|
||||
app.include_router(admin.router, prefix="/admin")
|
||||
app.include_router(candles.router)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def migrate():
|
||||
def startup():
|
||||
# Schema migration: add is_admin column if missing
|
||||
with engine.connect() as conn:
|
||||
cols = [r[1] for r in conn.execute(text("PRAGMA table_info(users)"))]
|
||||
if "is_admin" not in cols:
|
||||
conn.execute(text("ALTER TABLE users ADD COLUMN is_admin BOOLEAN DEFAULT 0"))
|
||||
conn.commit()
|
||||
|
||||
# Seed / refresh BTC candle data
|
||||
from .services.candles import refresh_latest_candles
|
||||
db = SessionLocal()
|
||||
try:
|
||||
refresh_latest_candles(db)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def root():
|
||||
|
||||
@@ -25,3 +25,14 @@ class Purchase(Base):
|
||||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
|
||||
owner = relationship("User", back_populates="purchases")
|
||||
|
||||
|
||||
class OHLCCandle(Base):
|
||||
__tablename__ = "ohlc_candles"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
date = Column(String, unique=True, index=True, nullable=False) # "YYYY-MM-DD" UTC
|
||||
open = Column(Float, nullable=False)
|
||||
high = Column(Float, nullable=False)
|
||||
low = Column(Float, nullable=False)
|
||||
close = Column(Float, nullable=False)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
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}
|
||||
@@ -1,4 +1,5 @@
|
||||
import requests
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def get_btc_history_eur() -> list:
|
||||
@@ -14,6 +15,40 @@ def get_btc_history_eur() -> list:
|
||||
return []
|
||||
|
||||
|
||||
def get_btc_ohlc_eur(days: int) -> list:
|
||||
"""Fetch OHLC candles from CoinGecko. Returns [[ts_ms, open, high, low, close], ...]."""
|
||||
try:
|
||||
resp = requests.get(
|
||||
"https://api.coingecko.com/api/v3/coins/bitcoin/ohlc",
|
||||
params={"vs_currency": "eur", "days": str(days)},
|
||||
timeout=20,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json() # [[timestamp_ms, open, high, low, close], ...]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
|
||||
def aggregate_to_daily(raw: list) -> dict:
|
||||
"""Collapse intraday OHLC rows into one candle per UTC calendar date.
|
||||
|
||||
Returns {date_str: {open, high, low, close}} using first open, max high,
|
||||
min low, last close per day.
|
||||
"""
|
||||
by_date: dict = {}
|
||||
for row in raw:
|
||||
ts_ms, o, h, l, c = row
|
||||
date = datetime.fromtimestamp(ts_ms / 1000, tz=timezone.utc).strftime("%Y-%m-%d")
|
||||
if date not in by_date:
|
||||
by_date[date] = {"open": o, "high": h, "low": l, "close": c}
|
||||
else:
|
||||
existing = by_date[date]
|
||||
existing["high"] = max(existing["high"], h)
|
||||
existing["low"] = min(existing["low"], l)
|
||||
existing["close"] = c # last close wins
|
||||
return by_date
|
||||
|
||||
|
||||
def get_btc_price_eur() -> float:
|
||||
try:
|
||||
resp = requests.get(
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import logging
|
||||
from datetime import datetime, timezone, date as dt_date
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import OHLCCandle
|
||||
from .btc import get_btc_ohlc_eur, aggregate_to_daily
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def seed_candles(db: Session) -> None:
|
||||
"""Fetch 30 days of daily OHLC candles from CoinGecko and store them.
|
||||
Free tier gives 4-hour bars for days<=30, which aggregate cleanly to daily candles.
|
||||
days>30 drops to 4-day granularity (unusable for a daily chart).
|
||||
"""
|
||||
raw = get_btc_ohlc_eur(days=30)
|
||||
if not raw:
|
||||
logger.warning("Candle seed: CoinGecko returned no data — will retry on next startup.")
|
||||
return
|
||||
|
||||
daily = aggregate_to_daily(raw)
|
||||
rows = [
|
||||
OHLCCandle(date=date, open=v["open"], high=v["high"], low=v["low"], close=v["close"])
|
||||
for date, v in sorted(daily.items())
|
||||
]
|
||||
for row in rows:
|
||||
db.merge(row)
|
||||
db.commit()
|
||||
logger.info("Candle seed: stored %d daily candles (%s → %s).", len(rows), min(daily.keys()), max(daily.keys()))
|
||||
|
||||
|
||||
def refresh_latest_candles(db: Session) -> None:
|
||||
"""Add any missing candles up to today. Seeds the DB if empty.
|
||||
Also detects and replaces coarse (>2-day gap) legacy data from a previous days=365 seed.
|
||||
"""
|
||||
# Sparse-data detection: if existing candles have >2-day gaps, wipe and re-seed
|
||||
first_two = db.query(OHLCCandle).order_by(OHLCCandle.date.asc()).limit(2).all()
|
||||
if len(first_two) == 2:
|
||||
d1 = dt_date.fromisoformat(first_two[0].date)
|
||||
d2 = dt_date.fromisoformat(first_two[1].date)
|
||||
if (d2 - d1).days > 2:
|
||||
logger.warning("Candle refresh: detected coarse candle data (gap=%d days). Wiping and re-seeding with daily candles.", (d2 - d1).days)
|
||||
db.query(OHLCCandle).delete()
|
||||
db.commit()
|
||||
seed_candles(db)
|
||||
return
|
||||
|
||||
latest = db.query(OHLCCandle).order_by(OHLCCandle.date.desc()).first()
|
||||
|
||||
if latest is None:
|
||||
seed_candles(db)
|
||||
return
|
||||
|
||||
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
||||
if latest.date >= today:
|
||||
return # Already up to date
|
||||
|
||||
raw = get_btc_ohlc_eur(days=7)
|
||||
if not raw:
|
||||
logger.warning("Candle refresh: CoinGecko returned no data.")
|
||||
return
|
||||
|
||||
daily = aggregate_to_daily(raw)
|
||||
new_dates = [d for d in daily if d > latest.date]
|
||||
if not new_dates:
|
||||
return
|
||||
|
||||
for date in new_dates:
|
||||
v = daily[date]
|
||||
db.merge(OHLCCandle(date=date, open=v["open"], high=v["high"], low=v["low"], close=v["close"]))
|
||||
db.commit()
|
||||
logger.info("Candle refresh: upserted %d candle(s) up to %s.", len(new_dates), max(new_dates))
|
||||
Reference in New Issue
Block a user