File size: 12,030 Bytes
d115c85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""
Data Hub API Router
Serves collected data from the database
"""

from fastapi import APIRouter, HTTPException, Query
from typing import List, Dict, Any, Optional
from datetime import datetime, timedelta, timezone
from database.db_manager import DatabaseManager
from database.models import MarketPrice, OHLC, SentimentMetric

router = APIRouter(prefix="/api/hub", tags=["Data Hub"])
db_manager = DatabaseManager()


# ============================================================================
# MARKET PRICES
# ============================================================================

@router.get("/prices/latest")
async def get_latest_prices(
    symbols: Optional[str] = Query(None, description="Comma-separated symbols (e.g., BTC,ETH)"),
    source: Optional[str] = Query(None, description="Filter by source (CoinGecko, Binance)"),
    limit: int = Query(100, ge=1, le=1000)
) -> Dict[str, Any]:
    """
    Get latest market prices from database

    Returns the most recent price for each symbol
    """
    try:
        with db_manager.get_session() as session:
            # Get latest price for each symbol
            from sqlalchemy import func

            # Subquery to get max timestamp per symbol
            subq = (
                session.query(
                    MarketPrice.symbol,
                    func.max(MarketPrice.timestamp).label('max_ts')
                )
                .group_by(MarketPrice.symbol)
                .subquery()
            )

            # Join to get full records
            query = session.query(MarketPrice).join(
                subq,
                (MarketPrice.symbol == subq.c.symbol) &
                (MarketPrice.timestamp == subq.c.max_ts)
            )

            # Apply filters
            if symbols:
                symbol_list = [s.strip().upper() for s in symbols.split(',')]
                query = query.filter(MarketPrice.symbol.in_(symbol_list))

            if source:
                query = query.filter(MarketPrice.source == source)

            prices = query.limit(limit).all()

            return {
                "success": True,
                "count": len(prices),
                "data": [
                    {
                        "symbol": p.symbol,
                        "price_usd": p.price_usd,
                        "market_cap": p.market_cap,
                        "volume_24h": p.volume_24h,
                        "price_change_24h": p.price_change_24h,
                        "source": p.source,
                        "timestamp": p.timestamp.isoformat()
                    }
                    for p in prices
                ]
            }

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching prices: {str(e)}")


@router.get("/prices/{symbol}")
async def get_symbol_price(
    symbol: str,
    hours: int = Query(24, ge=1, le=168, description="Hours of history")
) -> Dict[str, Any]:
    """
    Get price history for a specific symbol
    """
    try:
        with db_manager.get_session() as session:
            cutoff = datetime.now(timezone.utc) - timedelta(hours=hours)

            prices = (
                session.query(MarketPrice)
                .filter(
                    MarketPrice.symbol == symbol.upper(),
                    MarketPrice.timestamp >= cutoff
                )
                .order_by(MarketPrice.timestamp.desc())
                .all()
            )

            if not prices:
                raise HTTPException(status_code=404, detail=f"No data found for {symbol}")

            return {
                "success": True,
                "symbol": symbol.upper(),
                "count": len(prices),
                "data": [
                    {
                        "price_usd": p.price_usd,
                        "market_cap": p.market_cap,
                        "volume_24h": p.volume_24h,
                        "price_change_24h": p.price_change_24h,
                        "source": p.source,
                        "timestamp": p.timestamp.isoformat()
                    }
                    for p in prices
                ]
            }

    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching price history: {str(e)}")


# ============================================================================
# OHLC CANDLESTICK DATA
# ============================================================================

@router.get("/ohlc/{symbol}")
async def get_ohlc_data(
    symbol: str,
    interval: str = Query("1h", description="Timeframe (1m, 5m, 15m, 1h, 4h, 1d)"),
    limit: int = Query(100, ge=1, le=1000)
) -> Dict[str, Any]:
    """
    Get OHLC candlestick data for charts

    Returns data in format ready for TradingView/Lightweight Charts
    """
    try:
        with db_manager.get_session() as session:
            candles = (
                session.query(OHLC)
                .filter(
                    OHLC.symbol == symbol.upper(),
                    OHLC.interval == interval
                )
                .order_by(OHLC.ts.desc())
                .limit(limit)
                .all()
            )

            if not candles:
                raise HTTPException(
                    status_code=404,
                    detail=f"No OHLC data found for {symbol} with interval {interval}"
                )

            # Reverse to get chronological order
            candles = list(reversed(candles))

            return {
                "success": True,
                "symbol": symbol.upper(),
                "interval": interval,
                "count": len(candles),
                "data": [
                    {
                        "time": int(c.ts.timestamp()),  # Unix timestamp for charts
                        "open": c.open,
                        "high": c.high,
                        "low": c.low,
                        "close": c.close,
                        "volume": c.volume
                    }
                    for c in candles
                ]
            }

    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching OHLC data: {str(e)}")


# ============================================================================
# SENTIMENT DATA
# ============================================================================

@router.get("/sentiment/fear-greed")
async def get_fear_greed_index(
    hours: int = Query(24, ge=1, le=168)
) -> Dict[str, Any]:
    """
    Get Fear & Greed Index data
    """
    try:
        with db_manager.get_session() as session:
            cutoff = datetime.now(timezone.utc) - timedelta(hours=hours)

            metrics = (
                session.query(SentimentMetric)
                .filter(
                    SentimentMetric.metric_name == "fear_greed_index",
                    SentimentMetric.timestamp >= cutoff
                )
                .order_by(SentimentMetric.timestamp.desc())
                .all()
            )

            if not metrics:
                raise HTTPException(status_code=404, detail="No Fear & Greed data found")

            latest = metrics[0]

            return {
                "success": True,
                "latest": {
                    "value": latest.value,
                    "classification": latest.classification,
                    "timestamp": latest.timestamp.isoformat()
                },
                "history": [
                    {
                        "value": m.value,
                        "classification": m.classification,
                        "timestamp": m.timestamp.isoformat()
                    }
                    for m in metrics
                ]
            }

    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching sentiment data: {str(e)}")


# ============================================================================
# HEALTH & STATUS
# ============================================================================

@router.get("/status")
async def get_hub_status() -> Dict[str, Any]:
    """
    Get data hub status and statistics
    """
    try:
        db_stats = db_manager.get_database_stats()

        # Get latest timestamps
        with db_manager.get_session() as session:
            latest_price = (
                session.query(MarketPrice)
                .order_by(MarketPrice.timestamp.desc())
                .first()
            )

            latest_ohlc = (
                session.query(OHLC)
                .order_by(OHLC.ts.desc())
                .first()
            )

            latest_sentiment = (
                session.query(SentimentMetric)
                .order_by(SentimentMetric.timestamp.desc())
                .first()
            )

            return {
                "success": True,
                "status": "operational",
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "database": {
                    "size_mb": db_stats.get("database_size_mb", 0),
                    "providers": db_stats.get("providers", 0)
                },
                "data_counts": {
                    "market_prices": db_stats.get("market_prices", 0),
                    "ohlc_candles": db_stats.get("ohlc", 0),
                    "sentiment_metrics": db_stats.get("sentiment_metrics", 0)
                },
                "latest_updates": {
                    "prices": latest_price.timestamp.isoformat() if latest_price else None,
                    "ohlc": latest_ohlc.ts.isoformat() if latest_ohlc else None,
                    "sentiment": latest_sentiment.timestamp.isoformat() if latest_sentiment else None
                }
            }

    except Exception as e:
        return {
            "success": False,
            "status": "error",
            "error": str(e),
            "timestamp": datetime.now(timezone.utc).isoformat()
        }


# ============================================================================
# STATISTICS
# ============================================================================

@router.get("/stats")
async def get_hub_stats() -> Dict[str, Any]:
    """
    Get comprehensive hub statistics
    """
    try:
        with db_manager.get_session() as session:
            from sqlalchemy import func, distinct

            # Count unique symbols
            unique_symbols = session.query(func.count(distinct(MarketPrice.symbol))).scalar()

            # Count records per source
            price_sources = (
                session.query(MarketPrice.source, func.count(MarketPrice.id))
                .group_by(MarketPrice.source)
                .all()
            )

            # Get data freshness
            latest_price = (
                session.query(MarketPrice)
                .order_by(MarketPrice.timestamp.desc())
                .first()
            )

            if latest_price:
                age_seconds = (datetime.now(timezone.utc) - latest_price.timestamp).total_seconds()
                freshness = "fresh" if age_seconds < 120 else "stale"
            else:
                age_seconds = None
                freshness = "no_data"

            return {
                "success": True,
                "symbols_tracked": unique_symbols,
                "data_sources": {
                    source: count for source, count in price_sources
                },
                "data_freshness": {
                    "status": freshness,
                    "age_seconds": age_seconds,
                    "last_update": latest_price.timestamp.isoformat() if latest_price else None
                },
                "database": db_manager.get_database_stats()
            }

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching stats: {str(e)}")