108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Database initialization script for email tracking system.
|
|
|
|
This script creates the necessary MongoDB collections and indexes
|
|
for tracking email opens and link clicks in the newsletter system.
|
|
|
|
Collections created:
|
|
- newsletter_sends: Tracks each newsletter sent to each subscriber
|
|
- link_clicks: Tracks individual link clicks
|
|
- subscriber_activity: Aggregated activity status for each subscriber
|
|
|
|
Usage:
|
|
python init_tracking_db.py
|
|
"""
|
|
|
|
from pymongo import MongoClient, ASCENDING
|
|
from config import Config
|
|
from datetime import datetime
|
|
|
|
|
|
def init_tracking_database():
|
|
"""Initialize tracking collections with proper indexes"""
|
|
|
|
print("Connecting to MongoDB...")
|
|
client = MongoClient(Config.MONGODB_URI)
|
|
db = client[Config.DB_NAME]
|
|
|
|
print(f"Connected to database: {Config.DB_NAME}")
|
|
|
|
# Get collection references
|
|
newsletter_sends = db['newsletter_sends']
|
|
link_clicks = db['link_clicks']
|
|
subscriber_activity = db['subscriber_activity']
|
|
|
|
print("\n=== Setting up Newsletter Sends Collection ===")
|
|
# Newsletter Sends Collection Indexes
|
|
newsletter_sends.create_index('tracking_id', unique=True)
|
|
print("✓ Created unique index on 'tracking_id'")
|
|
|
|
newsletter_sends.create_index('newsletter_id')
|
|
print("✓ Created index on 'newsletter_id'")
|
|
|
|
newsletter_sends.create_index('subscriber_email')
|
|
print("✓ Created index on 'subscriber_email'")
|
|
|
|
newsletter_sends.create_index('sent_at')
|
|
print("✓ Created index on 'sent_at'")
|
|
|
|
print("\n=== Setting up Link Clicks Collection ===")
|
|
# Link Clicks Collection Indexes
|
|
link_clicks.create_index('tracking_id', unique=True)
|
|
print("✓ Created unique index on 'tracking_id'")
|
|
|
|
link_clicks.create_index('newsletter_id')
|
|
print("✓ Created index on 'newsletter_id'")
|
|
|
|
link_clicks.create_index('article_url')
|
|
print("✓ Created index on 'article_url'")
|
|
|
|
link_clicks.create_index('subscriber_email')
|
|
print("✓ Created index on 'subscriber_email'")
|
|
|
|
print("\n=== Setting up Subscriber Activity Collection ===")
|
|
# Subscriber Activity Collection Indexes
|
|
subscriber_activity.create_index('email', unique=True)
|
|
print("✓ Created unique index on 'email'")
|
|
|
|
subscriber_activity.create_index('status')
|
|
print("✓ Created index on 'status'")
|
|
|
|
subscriber_activity.create_index('last_opened_at')
|
|
print("✓ Created index on 'last_opened_at'")
|
|
|
|
# Display collection statistics
|
|
print("\n=== Collection Statistics ===")
|
|
print(f"newsletter_sends: {newsletter_sends.count_documents({})} documents")
|
|
print(f"link_clicks: {link_clicks.count_documents({})} documents")
|
|
print(f"subscriber_activity: {subscriber_activity.count_documents({})} documents")
|
|
|
|
# List all indexes for verification
|
|
print("\n=== Index Verification ===")
|
|
print("\nNewsletter Sends Indexes:")
|
|
for index in newsletter_sends.list_indexes():
|
|
print(f" - {index['name']}: {index.get('key', {})}")
|
|
|
|
print("\nLink Clicks Indexes:")
|
|
for index in link_clicks.list_indexes():
|
|
print(f" - {index['name']}: {index.get('key', {})}")
|
|
|
|
print("\nSubscriber Activity Indexes:")
|
|
for index in subscriber_activity.list_indexes():
|
|
print(f" - {index['name']}: {index.get('key', {})}")
|
|
|
|
print("\n✅ Tracking database initialization complete!")
|
|
|
|
client.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
init_tracking_database()
|
|
except Exception as e:
|
|
print(f"\n❌ Error initializing tracking database: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
exit(1)
|