Spaces:
Sleeping
Sleeping
Ali Hashhash commited on
Commit ·
e965068
1
Parent(s): 420464f
feat: add migration and verification scripts to initialize missing user analytics documents
Browse files- migrate_analytics.py +119 -0
- verify_analytics.py +16 -0
migrate_analytics.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Migration Script: Initialize analytics collection for all existing users.
|
| 3 |
+
Creates a default analytics document (matching AnalyticsModel) for every user
|
| 4 |
+
that doesn't already have one.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import firebase_admin
|
| 8 |
+
from firebase_admin import credentials, firestore
|
| 9 |
+
from google.cloud.firestore_v1 import SERVER_TIMESTAMP
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
# Initialize Firebase Admin SDK
|
| 13 |
+
cred = credentials.Certificate("firebase-service-account.json")
|
| 14 |
+
firebase_admin.initialize_app(cred)
|
| 15 |
+
db = firestore.client()
|
| 16 |
+
|
| 17 |
+
print("=" * 60)
|
| 18 |
+
print(" Analytics Collection Migration")
|
| 19 |
+
print("=" * 60)
|
| 20 |
+
|
| 21 |
+
# Step 1: Read all users
|
| 22 |
+
print("\n[1/3] Reading users collection...")
|
| 23 |
+
users_ref = db.collection("users")
|
| 24 |
+
user_docs = users_ref.stream()
|
| 25 |
+
|
| 26 |
+
user_ids = []
|
| 27 |
+
for doc in user_docs:
|
| 28 |
+
user_ids.append(doc.id)
|
| 29 |
+
print(f" Found user: {doc.id}")
|
| 30 |
+
|
| 31 |
+
print(f"\n Total users found: {len(user_ids)}")
|
| 32 |
+
|
| 33 |
+
if not user_ids:
|
| 34 |
+
print("\n No users found in the database. Nothing to migrate.")
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
# Step 2: Check which users already have analytics documents
|
| 38 |
+
print("\n[2/3] Checking existing analytics documents...")
|
| 39 |
+
analytics_ref = db.collection("analytics")
|
| 40 |
+
existing_analytics = set()
|
| 41 |
+
for doc in analytics_ref.stream():
|
| 42 |
+
existing_analytics.add(doc.id)
|
| 43 |
+
|
| 44 |
+
users_needing_analytics = [uid for uid in user_ids if uid not in existing_analytics]
|
| 45 |
+
already_have = [uid for uid in user_ids if uid in existing_analytics]
|
| 46 |
+
|
| 47 |
+
if already_have:
|
| 48 |
+
print(f" Users already with analytics: {len(already_have)}")
|
| 49 |
+
for uid in already_have:
|
| 50 |
+
print(f" - {uid} (skipping)")
|
| 51 |
+
|
| 52 |
+
print(f" Users needing analytics: {len(users_needing_analytics)}")
|
| 53 |
+
|
| 54 |
+
if not users_needing_analytics:
|
| 55 |
+
print("\n All users already have analytics documents. Nothing to do.")
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
# Step 3: Create default analytics documents using batched writes
|
| 59 |
+
print("\n[3/3] Creating default analytics documents...")
|
| 60 |
+
|
| 61 |
+
# Default schema matching AnalyticsModel / initializeUserAnalytics
|
| 62 |
+
default_analytics = {
|
| 63 |
+
"totalVideos": 0,
|
| 64 |
+
"totalMinutes": 0,
|
| 65 |
+
"totalSavedHours": 0.0,
|
| 66 |
+
"favoriteCategory": "None",
|
| 67 |
+
"currentStreak": 0,
|
| 68 |
+
"thisWeekVideos": 0,
|
| 69 |
+
"thisMonthSavedHours": 0.0,
|
| 70 |
+
"categoryCount": {},
|
| 71 |
+
"lastUpdated": SERVER_TIMESTAMP,
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# Firestore batch limit is 500 operations per batch
|
| 75 |
+
batch = db.batch()
|
| 76 |
+
batch_count = 0
|
| 77 |
+
total_created = 0
|
| 78 |
+
|
| 79 |
+
for uid in users_needing_analytics:
|
| 80 |
+
doc_ref = analytics_ref.document(uid)
|
| 81 |
+
batch.set(doc_ref, default_analytics)
|
| 82 |
+
batch_count += 1
|
| 83 |
+
total_created += 1
|
| 84 |
+
print(f" Queued: analytics/{uid}")
|
| 85 |
+
|
| 86 |
+
# Commit every 500 operations (Firestore batch limit)
|
| 87 |
+
if batch_count >= 500:
|
| 88 |
+
batch.commit()
|
| 89 |
+
print(f" Committed batch ({batch_count} documents)")
|
| 90 |
+
batch = db.batch()
|
| 91 |
+
batch_count = 0
|
| 92 |
+
|
| 93 |
+
# Commit remaining
|
| 94 |
+
if batch_count > 0:
|
| 95 |
+
batch.commit()
|
| 96 |
+
print(f" Committed final batch ({batch_count} documents)")
|
| 97 |
+
|
| 98 |
+
# Step 4: Verify
|
| 99 |
+
print("\n" + "=" * 60)
|
| 100 |
+
print(" Verification")
|
| 101 |
+
print("=" * 60)
|
| 102 |
+
|
| 103 |
+
verify_count = 0
|
| 104 |
+
for doc in db.collection("analytics").stream():
|
| 105 |
+
verify_count += 1
|
| 106 |
+
data = doc.to_dict()
|
| 107 |
+
print(f" ✓ analytics/{doc.id}")
|
| 108 |
+
print(f" totalVideos={data.get('totalVideos')}, "
|
| 109 |
+
f"totalMinutes={data.get('totalMinutes')}, "
|
| 110 |
+
f"favoriteCategory={data.get('favoriteCategory')}, "
|
| 111 |
+
f"lastUpdated={data.get('lastUpdated')}")
|
| 112 |
+
|
| 113 |
+
print(f"\n Analytics collection now has {verify_count} document(s)")
|
| 114 |
+
print(f" New documents created: {total_created}")
|
| 115 |
+
print("\n Migration complete!")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|
verify_analytics.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quick verification of analytics collection."""
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials, firestore
|
| 4 |
+
|
| 5 |
+
cred = credentials.Certificate("firebase-service-account.json")
|
| 6 |
+
firebase_admin.initialize_app(cred)
|
| 7 |
+
db = firestore.client()
|
| 8 |
+
|
| 9 |
+
docs = list(db.collection("analytics").stream())
|
| 10 |
+
print(f"Analytics collection: {len(docs)} documents")
|
| 11 |
+
for d in docs:
|
| 12 |
+
data = d.to_dict()
|
| 13 |
+
print(f" OK analytics/{d.id}")
|
| 14 |
+
print(f" totalVideos={data.get('totalVideos')}, totalMinutes={data.get('totalMinutes')}, "
|
| 15 |
+
f"favoriteCategory={data.get('favoriteCategory')}, lastUpdated={data.get('lastUpdated')}")
|
| 16 |
+
print("\nDone.")
|