File size: 25,140 Bytes
d6d843f |
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
#!/usr/bin/env python3
"""
Database module for Crypto Data Aggregator
Complete CRUD operations with the exact schema specified
"""
import sqlite3
import threading
import json
from datetime import datetime, timedelta
from typing import List, Dict, Optional, Any, Tuple
from contextlib import contextmanager
import logging
import config
# Setup logging
logging.basicConfig(
level=getattr(logging, config.LOG_LEVEL),
format=config.LOG_FORMAT,
handlers=[
logging.FileHandler(config.LOG_FILE),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class CryptoDatabase:
"""
Database manager for cryptocurrency data with full CRUD operations
Thread-safe implementation using context managers
"""
def __init__(self, db_path: str = None):
"""Initialize database with connection pooling"""
self.db_path = str(db_path or config.DATABASE_PATH)
self._local = threading.local()
self._init_database()
logger.info(f"Database initialized at {self.db_path}")
@contextmanager
def get_connection(self):
"""Get thread-safe database connection"""
if not hasattr(self._local, 'conn'):
self._local.conn = sqlite3.connect(
self.db_path,
check_same_thread=False,
timeout=30.0
)
self._local.conn.row_factory = sqlite3.Row
try:
yield self._local.conn
except Exception as e:
self._local.conn.rollback()
logger.error(f"Database error: {e}")
raise
def _init_database(self):
"""Initialize all database tables with exact schema"""
with self.get_connection() as conn:
cursor = conn.cursor()
# ==================== PRICES TABLE ====================
cursor.execute("""
CREATE TABLE IF NOT EXISTS prices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol TEXT NOT NULL,
name TEXT,
price_usd REAL NOT NULL,
volume_24h REAL,
market_cap REAL,
percent_change_1h REAL,
percent_change_24h REAL,
percent_change_7d REAL,
rank INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# ==================== NEWS TABLE ====================
cursor.execute("""
CREATE TABLE IF NOT EXISTS news (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
summary TEXT,
url TEXT UNIQUE,
source TEXT,
sentiment_score REAL,
sentiment_label TEXT,
related_coins TEXT,
published_date DATETIME,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# ==================== MARKET ANALYSIS TABLE ====================
cursor.execute("""
CREATE TABLE IF NOT EXISTS market_analysis (
id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol TEXT NOT NULL,
timeframe TEXT,
trend TEXT,
support_level REAL,
resistance_level REAL,
prediction TEXT,
confidence REAL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# ==================== USER QUERIES TABLE ====================
cursor.execute("""
CREATE TABLE IF NOT EXISTS user_queries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query TEXT,
result_count INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# ==================== CREATE INDEXES ====================
cursor.execute("CREATE INDEX IF NOT EXISTS idx_prices_symbol ON prices(symbol)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_prices_timestamp ON prices(timestamp)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_prices_rank ON prices(rank)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_news_url ON news(url)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_news_published ON news(published_date)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_news_sentiment ON news(sentiment_label)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_analysis_symbol ON market_analysis(symbol)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_analysis_timestamp ON market_analysis(timestamp)")
conn.commit()
logger.info("Database tables and indexes created successfully")
# ==================== PRICES CRUD OPERATIONS ====================
def save_price(self, price_data: Dict[str, Any]) -> bool:
"""
Save a single price record
Args:
price_data: Dictionary containing price information
Returns:
bool: True if successful, False otherwise
"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO prices
(symbol, name, price_usd, volume_24h, market_cap,
percent_change_1h, percent_change_24h, percent_change_7d, rank)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
price_data.get('symbol'),
price_data.get('name'),
price_data.get('price_usd', 0.0),
price_data.get('volume_24h'),
price_data.get('market_cap'),
price_data.get('percent_change_1h'),
price_data.get('percent_change_24h'),
price_data.get('percent_change_7d'),
price_data.get('rank')
))
conn.commit()
return True
except Exception as e:
logger.error(f"Error saving price: {e}")
return False
def save_prices_batch(self, prices: List[Dict[str, Any]]) -> int:
"""
Save multiple price records in batch (minimum 100 records for efficiency)
Args:
prices: List of price dictionaries
Returns:
int: Number of records saved
"""
saved_count = 0
try:
with self.get_connection() as conn:
cursor = conn.cursor()
for price_data in prices:
try:
cursor.execute("""
INSERT INTO prices
(symbol, name, price_usd, volume_24h, market_cap,
percent_change_1h, percent_change_24h, percent_change_7d, rank)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
price_data.get('symbol'),
price_data.get('name'),
price_data.get('price_usd', 0.0),
price_data.get('volume_24h'),
price_data.get('market_cap'),
price_data.get('percent_change_1h'),
price_data.get('percent_change_24h'),
price_data.get('percent_change_7d'),
price_data.get('rank')
))
saved_count += 1
except Exception as e:
logger.warning(f"Error saving individual price: {e}")
continue
conn.commit()
logger.info(f"Batch saved {saved_count} price records")
except Exception as e:
logger.error(f"Error in batch save: {e}")
return saved_count
def get_latest_prices(self, limit: int = 100) -> List[Dict[str, Any]]:
"""
Get latest prices for top cryptocurrencies
Args:
limit: Maximum number of records to return
Returns:
List of price dictionaries
"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT DISTINCT ON (symbol) *
FROM prices
WHERE timestamp >= datetime('now', '-1 hour')
ORDER BY symbol, timestamp DESC, rank ASC
LIMIT ?
""", (limit,))
# SQLite doesn't support DISTINCT ON, use subquery instead
cursor.execute("""
SELECT p1.*
FROM prices p1
INNER JOIN (
SELECT symbol, MAX(timestamp) as max_ts
FROM prices
WHERE timestamp >= datetime('now', '-1 hour')
GROUP BY symbol
) p2 ON p1.symbol = p2.symbol AND p1.timestamp = p2.max_ts
ORDER BY p1.rank ASC, p1.market_cap DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting latest prices: {e}")
return []
def get_price_history(self, symbol: str, hours: int = 24) -> List[Dict[str, Any]]:
"""
Get price history for a specific symbol
Args:
symbol: Cryptocurrency symbol
hours: Number of hours to look back
Returns:
List of price dictionaries
"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM prices
WHERE symbol = ?
AND timestamp >= datetime('now', '-' || ? || ' hours')
ORDER BY timestamp ASC
""", (symbol, hours))
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting price history: {e}")
return []
def get_top_gainers(self, limit: int = 10) -> List[Dict[str, Any]]:
"""Get top gaining cryptocurrencies in last 24h"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT p1.*
FROM prices p1
INNER JOIN (
SELECT symbol, MAX(timestamp) as max_ts
FROM prices
WHERE timestamp >= datetime('now', '-1 hour')
GROUP BY symbol
) p2 ON p1.symbol = p2.symbol AND p1.timestamp = p2.max_ts
WHERE p1.percent_change_24h IS NOT NULL
ORDER BY p1.percent_change_24h DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting top gainers: {e}")
return []
def delete_old_prices(self, days: int = 30) -> int:
"""
Delete price records older than specified days
Args:
days: Number of days to keep
Returns:
Number of deleted records
"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
DELETE FROM prices
WHERE timestamp < datetime('now', '-' || ? || ' days')
""", (days,))
conn.commit()
deleted = cursor.rowcount
logger.info(f"Deleted {deleted} old price records")
return deleted
except Exception as e:
logger.error(f"Error deleting old prices: {e}")
return 0
# ==================== NEWS CRUD OPERATIONS ====================
def save_news(self, news_data: Dict[str, Any]) -> bool:
"""
Save a single news record
Args:
news_data: Dictionary containing news information
Returns:
bool: True if successful, False otherwise
"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT OR IGNORE INTO news
(title, summary, url, source, sentiment_score,
sentiment_label, related_coins, published_date)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (
news_data.get('title'),
news_data.get('summary'),
news_data.get('url'),
news_data.get('source'),
news_data.get('sentiment_score'),
news_data.get('sentiment_label'),
json.dumps(news_data.get('related_coins', [])),
news_data.get('published_date')
))
conn.commit()
return True
except Exception as e:
logger.error(f"Error saving news: {e}")
return False
def get_latest_news(self, limit: int = 50, sentiment: Optional[str] = None) -> List[Dict[str, Any]]:
"""
Get latest news articles
Args:
limit: Maximum number of articles
sentiment: Filter by sentiment label (optional)
Returns:
List of news dictionaries
"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
if sentiment:
cursor.execute("""
SELECT * FROM news
WHERE sentiment_label = ?
ORDER BY published_date DESC, timestamp DESC
LIMIT ?
""", (sentiment, limit))
else:
cursor.execute("""
SELECT * FROM news
ORDER BY published_date DESC, timestamp DESC
LIMIT ?
""", (limit,))
results = []
for row in cursor.fetchall():
news_dict = dict(row)
if news_dict.get('related_coins'):
try:
news_dict['related_coins'] = json.loads(news_dict['related_coins'])
except:
news_dict['related_coins'] = []
results.append(news_dict)
return results
except Exception as e:
logger.error(f"Error getting latest news: {e}")
return []
def get_news_by_coin(self, coin: str, limit: int = 20) -> List[Dict[str, Any]]:
"""Get news related to a specific coin"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM news
WHERE related_coins LIKE ?
ORDER BY published_date DESC
LIMIT ?
""", (f'%{coin}%', limit))
results = []
for row in cursor.fetchall():
news_dict = dict(row)
if news_dict.get('related_coins'):
try:
news_dict['related_coins'] = json.loads(news_dict['related_coins'])
except:
news_dict['related_coins'] = []
results.append(news_dict)
return results
except Exception as e:
logger.error(f"Error getting news by coin: {e}")
return []
def update_news_sentiment(self, news_id: int, sentiment_score: float, sentiment_label: str) -> bool:
"""Update sentiment for a news article"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE news
SET sentiment_score = ?, sentiment_label = ?
WHERE id = ?
""", (sentiment_score, sentiment_label, news_id))
conn.commit()
return True
except Exception as e:
logger.error(f"Error updating news sentiment: {e}")
return False
def delete_old_news(self, days: int = 30) -> int:
"""Delete news older than specified days"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
DELETE FROM news
WHERE timestamp < datetime('now', '-' || ? || ' days')
""", (days,))
conn.commit()
deleted = cursor.rowcount
logger.info(f"Deleted {deleted} old news records")
return deleted
except Exception as e:
logger.error(f"Error deleting old news: {e}")
return 0
# ==================== MARKET ANALYSIS CRUD OPERATIONS ====================
def save_analysis(self, analysis_data: Dict[str, Any]) -> bool:
"""Save market analysis"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO market_analysis
(symbol, timeframe, trend, support_level, resistance_level,
prediction, confidence)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
analysis_data.get('symbol'),
analysis_data.get('timeframe'),
analysis_data.get('trend'),
analysis_data.get('support_level'),
analysis_data.get('resistance_level'),
analysis_data.get('prediction'),
analysis_data.get('confidence')
))
conn.commit()
return True
except Exception as e:
logger.error(f"Error saving analysis: {e}")
return False
def get_latest_analysis(self, symbol: str) -> Optional[Dict[str, Any]]:
"""Get latest analysis for a symbol"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM market_analysis
WHERE symbol = ?
ORDER BY timestamp DESC
LIMIT 1
""", (symbol,))
row = cursor.fetchone()
return dict(row) if row else None
except Exception as e:
logger.error(f"Error getting latest analysis: {e}")
return None
def get_all_analyses(self, limit: int = 100) -> List[Dict[str, Any]]:
"""Get all market analyses"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM market_analysis
ORDER BY timestamp DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting all analyses: {e}")
return []
# ==================== USER QUERIES CRUD OPERATIONS ====================
def log_user_query(self, query: str, result_count: int) -> bool:
"""Log a user query"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO user_queries (query, result_count)
VALUES (?, ?)
""", (query, result_count))
conn.commit()
return True
except Exception as e:
logger.error(f"Error logging user query: {e}")
return False
def get_recent_queries(self, limit: int = 50) -> List[Dict[str, Any]]:
"""Get recent user queries"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM user_queries
ORDER BY timestamp DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting recent queries: {e}")
return []
# ==================== UTILITY OPERATIONS ====================
def execute_safe_query(self, query: str, params: Tuple = ()) -> List[Dict[str, Any]]:
"""
Execute a safe read-only query
Args:
query: SQL query (must start with SELECT)
params: Query parameters
Returns:
List of result dictionaries
"""
try:
# Security: Only allow SELECT queries
if not query.strip().upper().startswith('SELECT'):
logger.warning(f"Attempted non-SELECT query: {query}")
return []
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute(query, params)
return [dict(row) for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error executing safe query: {e}")
return []
def get_database_stats(self) -> Dict[str, Any]:
"""Get database statistics"""
try:
with self.get_connection() as conn:
cursor = conn.cursor()
stats = {}
# Count records in each table
for table in ['prices', 'news', 'market_analysis', 'user_queries']:
cursor.execute(f"SELECT COUNT(*) as count FROM {table}")
stats[f'{table}_count'] = cursor.fetchone()['count']
# Get unique symbols
cursor.execute("SELECT COUNT(DISTINCT symbol) as count FROM prices")
stats['unique_symbols'] = cursor.fetchone()['count']
# Get latest price update
cursor.execute("SELECT MAX(timestamp) as latest FROM prices")
stats['latest_price_update'] = cursor.fetchone()['latest']
# Get latest news update
cursor.execute("SELECT MAX(timestamp) as latest FROM news")
stats['latest_news_update'] = cursor.fetchone()['latest']
# Database file size
import os
if os.path.exists(self.db_path):
stats['database_size_bytes'] = os.path.getsize(self.db_path)
stats['database_size_mb'] = stats['database_size_bytes'] / (1024 * 1024)
return stats
except Exception as e:
logger.error(f"Error getting database stats: {e}")
return {}
def vacuum_database(self) -> bool:
"""Vacuum database to reclaim space"""
try:
with self.get_connection() as conn:
conn.execute("VACUUM")
logger.info("Database vacuumed successfully")
return True
except Exception as e:
logger.error(f"Error vacuuming database: {e}")
return False
def backup_database(self, backup_path: Optional[str] = None) -> bool:
"""Create database backup"""
try:
import shutil
if backup_path is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_path = config.DATABASE_BACKUP_DIR / f"backup_{timestamp}.db"
shutil.copy2(self.db_path, backup_path)
logger.info(f"Database backed up to {backup_path}")
return True
except Exception as e:
logger.error(f"Error backing up database: {e}")
return False
def close(self):
"""Close database connection"""
if hasattr(self._local, 'conn'):
self._local.conn.close()
delattr(self._local, 'conn')
logger.info("Database connection closed")
# Singleton instance
_db_instance = None
def get_database() -> CryptoDatabase:
"""Get database singleton instance"""
global _db_instance
if _db_instance is None:
_db_instance = CryptoDatabase()
return _db_instance
|