File size: 13,216 Bytes
b190b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Database Query Functions for Cached Market Data

Provides REAL data access from cached_market_data and cached_ohlc tables



CRITICAL RULES:

- ONLY read from database - NEVER generate fake data

- Return empty list if no data found

- All queries must be REAL database operations

"""

import logging
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
from sqlalchemy import desc, and_, func
from sqlalchemy.orm import Session

from database.models import CachedMarketData, CachedOHLC
from database.db_manager import DatabaseManager
from utils.logger import setup_logger

logger = setup_logger("cache_queries")


class CacheQueries:
    """

    Database query operations for cached market data

    

    CRITICAL: All methods return REAL data from database ONLY

    """
    
    def __init__(self, db_manager: DatabaseManager):
        self.db = db_manager
    
    def get_cached_market_data(

        self,

        symbols: Optional[List[str]] = None,

        limit: int = 100

    ) -> List[Dict[str, Any]]:
        """

        Get cached market data from database

        

        CRITICAL RULES:

        - ONLY read from cached_market_data table

        - NEVER generate or fake data

        - Return empty list if no data found

        - Use DISTINCT ON to get latest data per symbol

        

        Args:

            symbols: List of symbols to filter (e.g., ['BTC', 'ETH'])

            limit: Maximum number of records

            

        Returns:

            List of dictionaries with REAL market data from database

        """
        try:
            with self.db.get_session() as session:
                # Subquery to get latest fetched_at for each symbol
                subq = session.query(
                    CachedMarketData.symbol,
                    func.max(CachedMarketData.fetched_at).label('max_fetched_at')
                ).group_by(CachedMarketData.symbol)
                
                if symbols:
                    subq = subq.filter(CachedMarketData.symbol.in_(symbols))
                
                subq = subq.subquery()
                
                # Join to get full records for latest entries
                query = session.query(CachedMarketData).join(
                    subq,
                    and_(
                        CachedMarketData.symbol == subq.c.symbol,
                        CachedMarketData.fetched_at == subq.c.max_fetched_at
                    )
                ).order_by(desc(CachedMarketData.fetched_at)).limit(limit)
                
                results = query.all()
                
                if not results:
                    logger.info(f"No cached market data found for symbols={symbols}")
                    return []
                
                # Convert to dictionaries - REAL data from database
                data = []
                for row in results:
                    data.append({
                        "symbol": row.symbol,
                        "price": float(row.price),
                        "market_cap": float(row.market_cap) if row.market_cap else None,
                        "volume_24h": float(row.volume_24h) if row.volume_24h else None,
                        "change_24h": float(row.change_24h) if row.change_24h else None,
                        "high_24h": float(row.high_24h) if row.high_24h else None,
                        "low_24h": float(row.low_24h) if row.low_24h else None,
                        "provider": row.provider,
                        "fetched_at": row.fetched_at
                    })
                
                logger.info(f"Retrieved {len(data)} cached market records")
                return data
                
        except Exception as e:
            logger.error(f"Database error in get_cached_market_data: {e}", exc_info=True)
            # Return empty list on error - NEVER fake data
            return []
    
    def get_cached_ohlc(

        self,

        symbol: str,

        interval: str = "1h",

        limit: int = 1000

    ) -> List[Dict[str, Any]]:
        """

        Get cached OHLC data from database

        

        CRITICAL RULES:

        - ONLY read from cached_ohlc table

        - NEVER generate fake candles

        - Return empty list if no data found

        - Order by timestamp ASC for chart display

        

        Args:

            symbol: Trading pair symbol (e.g., 'BTCUSDT')

            interval: Candle interval (e.g., '1h', '4h', '1d')

            limit: Maximum number of candles

            

        Returns:

            List of dictionaries with REAL OHLC data from database

        """
        try:
            with self.db.get_session() as session:
                # Query for OHLC data
                query = session.query(CachedOHLC).filter(
                    and_(
                        CachedOHLC.symbol == symbol,
                        CachedOHLC.interval == interval
                    )
                ).order_by(desc(CachedOHLC.timestamp)).limit(limit)
                
                results = query.all()
                
                if not results:
                    logger.info(f"No cached OHLC data found for {symbol} {interval}")
                    return []
                
                # Convert to dictionaries - REAL candle data from database
                # Reverse order for chronological display
                data = []
                for row in reversed(results):
                    data.append({
                        "timestamp": row.timestamp,
                        "open": float(row.open),
                        "high": float(row.high),
                        "low": float(row.low),
                        "close": float(row.close),
                        "volume": float(row.volume),
                        "provider": row.provider,
                        "fetched_at": row.fetched_at
                    })
                
                logger.info(f"Retrieved {len(data)} OHLC candles for {symbol} {interval}")
                return data
                
        except Exception as e:
            logger.error(f"Database error in get_cached_ohlc: {e}", exc_info=True)
            # Return empty list on error - NEVER fake data
            return []
    
    def save_market_data(

        self,

        symbol: str,

        price: float,

        market_cap: Optional[float] = None,

        volume_24h: Optional[float] = None,

        change_24h: Optional[float] = None,

        high_24h: Optional[float] = None,

        low_24h: Optional[float] = None,

        provider: str = "unknown"

    ) -> bool:
        """

        Save market data to cache

        

        CRITICAL: Only used by background workers to store REAL API data

        

        Args:

            symbol: Crypto symbol

            price: Current price (REAL from API)

            market_cap: Market cap (REAL from API)

            volume_24h: 24h volume (REAL from API)

            change_24h: 24h change (REAL from API)

            high_24h: 24h high (REAL from API)

            low_24h: 24h low (REAL from API)

            provider: Data provider name

            

        Returns:

            bool: True if saved successfully

        """
        try:
            with self.db.get_session() as session:
                # Create new record with REAL data
                record = CachedMarketData(
                    symbol=symbol,
                    price=price,
                    market_cap=market_cap,
                    volume_24h=volume_24h,
                    change_24h=change_24h,
                    high_24h=high_24h,
                    low_24h=low_24h,
                    provider=provider,
                    fetched_at=datetime.utcnow()
                )
                
                session.add(record)
                session.commit()
                
                logger.info(f"Saved market data for {symbol} from {provider}")
                return True
                
        except Exception as e:
            logger.error(f"Error saving market data for {symbol}: {e}", exc_info=True)
            return False
    
    def save_ohlc_candle(

        self,

        symbol: str,

        interval: str,

        timestamp: datetime,

        open_price: float,

        high: float,

        low: float,

        close: float,

        volume: float,

        provider: str = "unknown"

    ) -> bool:
        """

        Save OHLC candle to cache

        

        CRITICAL: Only used by background workers to store REAL candle data

        

        Args:

            symbol: Trading pair symbol

            interval: Candle interval

            timestamp: Candle open time (REAL from API)

            open_price: Open price (REAL from API)

            high: High price (REAL from API)

            low: Low price (REAL from API)

            close: Close price (REAL from API)

            volume: Volume (REAL from API)

            provider: Data provider name

            

        Returns:

            bool: True if saved successfully

        """
        try:
            with self.db.get_session() as session:
                # Check if candle already exists
                existing = session.query(CachedOHLC).filter(
                    and_(
                        CachedOHLC.symbol == symbol,
                        CachedOHLC.interval == interval,
                        CachedOHLC.timestamp == timestamp
                    )
                ).first()
                
                if existing:
                    # Update existing candle
                    existing.open = open_price
                    existing.high = high
                    existing.low = low
                    existing.close = close
                    existing.volume = volume
                    existing.provider = provider
                    existing.fetched_at = datetime.utcnow()
                else:
                    # Create new candle with REAL data
                    record = CachedOHLC(
                        symbol=symbol,
                        interval=interval,
                        timestamp=timestamp,
                        open=open_price,
                        high=high,
                        low=low,
                        close=close,
                        volume=volume,
                        provider=provider,
                        fetched_at=datetime.utcnow()
                    )
                    session.add(record)
                
                session.commit()
                
                logger.debug(f"Saved OHLC candle for {symbol} {interval} at {timestamp}")
                return True
                
        except Exception as e:
            logger.error(f"Error saving OHLC candle for {symbol}: {e}", exc_info=True)
            return False
    
    def cleanup_old_data(self, days: int = 7) -> Dict[str, int]:
        """

        Remove old cached data to manage storage

        

        Args:

            days: Remove data older than N days

            

        Returns:

            Dictionary with counts of deleted records

        """
        try:
            with self.db.get_session() as session:
                cutoff_time = datetime.utcnow() - timedelta(days=days)
                deleted_counts = {}
                
                # Clean old market data
                deleted = session.query(CachedMarketData).filter(
                    CachedMarketData.fetched_at < cutoff_time
                ).delete()
                deleted_counts['market_data'] = deleted
                
                # Clean old OHLC data
                deleted = session.query(CachedOHLC).filter(
                    CachedOHLC.fetched_at < cutoff_time
                ).delete()
                deleted_counts['ohlc'] = deleted
                
                session.commit()
                
                total_deleted = sum(deleted_counts.values())
                logger.info(f"Cleaned up {total_deleted} old cache records (older than {days} days)")
                
                return deleted_counts
                
        except Exception as e:
            logger.error(f"Error cleaning up old data: {e}", exc_info=True)
            return {}


# Global instance
_cache_queries = None

def get_cache_queries(db_manager: Optional[DatabaseManager] = None) -> CacheQueries:
    """

    Get global CacheQueries instance

    

    Args:

        db_manager: DatabaseManager instance (optional, will use global if not provided)

        

    Returns:

        CacheQueries instance

    """
    global _cache_queries
    
    if _cache_queries is None:
        if db_manager is None:
            from database.db_manager import db_manager as global_db
            db_manager = global_db
        _cache_queries = CacheQueries(db_manager)
    
    return _cache_queries