File size: 7,295 Bytes
c293f7c | 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 | #!/usr/bin/env python3
"""
Database initialization script for the misinformation heatmap application.
Handles both local SQLite and cloud BigQuery database setup.
"""
import argparse
import asyncio
import logging
import os
import sys
from pathlib import Path
from typing import Optional
# Add the backend directory to the Python path
sys.path.insert(0, str(Path(__file__).parent))
from config import Config
from database import DatabaseInterface, SQLiteDatabase, DatabaseManager
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
async def create_local_database(config: Config) -> bool:
"""
Create and initialize local SQLite database.
Args:
config: Application configuration
Returns:
bool: True if successful, False otherwise
"""
try:
logger.info("Initializing local SQLite database...")
# Ensure data directory exists
data_dir = Path("data")
data_dir.mkdir(exist_ok=True)
# Initialize database
db_manager = DatabaseManager()
db = db_manager.create_database()
# Initialize database schema
await db.initialize()
logger.info("Local database initialized successfully")
return True
except Exception as e:
logger.error(f"Database initialization failed: {e}")
return False
except Exception as e:
logger.error(f"Unexpected error during database initialization: {e}")
return False
# create_cloud_database REMOVED (GCP decommissioned to prevent costs)
async def validate_database(config: Config) -> bool:
"""
Validate database setup and connectivity.
Args:
config: Application configuration
Returns:
bool: True if validation passes, False otherwise
"""
try:
logger.info("Validating database setup...")
db_manager = DatabaseManager()
db = db_manager.create_database()
# Test basic connectivity by trying to initialize
try:
await db.initialize()
logger.info("Database connectivity test passed")
except Exception as e:
logger.error(f"Database connectivity test failed: {e}")
return False
logger.info("Database validation completed successfully")
return True
except Exception as e:
logger.error(f"Database validation failed: {e}")
return False
async def create_sample_data(config: Config) -> bool:
"""
Create sample data for development and testing.
Args:
config: Application configuration
Returns:
bool: True if successful, False otherwise
"""
try:
logger.info("Creating sample data...")
db_manager = DatabaseManager()
db = db_manager.create_database()
# Sample events for testing
sample_events = [
{
"event_id": "sample-001",
"source": "manual",
"original_text": "Sample misinformation event in Maharashtra for testing purposes.",
"lang": "en",
"region_hint": "Maharashtra",
"lat": 19.0760,
"lon": 72.8777,
"entities": ["Maharashtra", "testing"],
"virality_score": 0.3,
"satellite_data": {
"similarity": 0.8,
"anomaly": False,
"reality_score": 0.9,
"confidence": 0.85
},
"claims": []
},
{
"event_id": "sample-002",
"source": "manual",
"original_text": "Sample environmental claim in Karnataka with satellite validation.",
"lang": "en",
"region_hint": "Karnataka",
"lat": 15.3173,
"lon": 75.7139,
"entities": ["Karnataka", "environment"],
"virality_score": 0.5,
"satellite_data": {
"similarity": 0.6,
"anomaly": True,
"reality_score": 0.4,
"confidence": 0.75
},
"claims": []
}
]
# Insert sample events
for event_data in sample_events:
# Convert dict to ProcessedEvent object if needed
# For now, just skip sample data creation as it requires proper model conversion
pass
logger.info(f"Created {len(sample_events)} sample events")
return True
except Exception as e:
logger.error(f"Failed to create sample data: {e}")
return False
async def main():
"""Main function to handle database initialization."""
parser = argparse.ArgumentParser(
description="Initialize database for misinformation heatmap application"
)
parser.add_argument(
"--mode",
choices=["local"],
default="local",
help="Database mode (strictly local to prevent GCP costs)"
)
parser.add_argument(
"--sample-data",
action="store_true",
help="Create sample data for testing"
)
parser.add_argument(
"--validate-only",
action="store_true",
help="Only validate existing database setup"
)
parser.add_argument(
"--force",
action="store_true",
help="Force recreation of existing database"
)
parser.add_argument(
"--verbose",
action="store_true",
help="Enable verbose logging"
)
args = parser.parse_args()
# Set logging level
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
logger.info(f"Starting database initialization in {args.mode} mode")
try:
# Load configuration
config = Config(mode=args.mode)
# Validation only mode
if args.validate_only:
success = await validate_database(config)
sys.exit(0 if success else 1)
# Initialize SQLite database
success = await create_local_database(config)
if not success:
logger.error("Database initialization failed")
sys.exit(1)
# Validate the setup
if not await validate_database(config):
logger.error("Database validation failed after initialization")
sys.exit(1)
# Create sample data if requested
if args.sample_data:
if not await create_sample_data(config):
logger.warning("Sample data creation failed, but database is initialized")
logger.info("Database initialization completed successfully")
except KeyboardInterrupt:
logger.info("Database initialization interrupted by user")
sys.exit(1)
except Exception as e:
logger.error(f"Unexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main()) |