forked from Varsha-1605/SocioSell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_setup.py
214 lines (200 loc) · 11.6 KB
/
database_setup.py
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
from pymongo import MongoClient, ASCENDING
from pymongo.server_api import ServerApi
import logging
import os
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def setup_product_database():
"""Setup product reference database with sample data"""
uri = os.getenv("MONGODB_URL")
try:
client = MongoClient(uri, server_api=ServerApi('1'))
db = client.social_media_products
# Create Image Collections
product_collection = db["products"]
listing_collection = db["listings"]
analytics_collection = db["analytics"]
review_collection = db["reviews"]
# Video Collections
video_collection = db["videos"]
video_listings_collection = db["video_listings"]
video_analytics_collection = db["video_analytics"]
# Create indexes for products
product_collection.create_index([("id", ASCENDING)], unique=True)
product_collection.create_index([("title", ASCENDING)])
product_collection.create_index([("category", ASCENDING)])
product_collection.create_index([("subcategory", ASCENDING)])
product_collection.create_index([("features", ASCENDING)])
product_collection.create_index([("price_range", ASCENDING)])
product_collection.create_index([("created_at", ASCENDING)])
product_collection.create_index([("updated_at", ASCENDING)])
# Create indexes for listings
listing_collection.create_index([("id", ASCENDING), ("product_id", ASCENDING)])
listing_collection.create_index([("product_id", ASCENDING), ("created_at", ASCENDING)])
listing_collection.create_index([("price", ASCENDING), ("updated_at", ASCENDING)])
listing_collection.create_index([("features", ASCENDING), ("title", ASCENDING)])
listing_collection.create_index([("id", ASCENDING)], unique=True)
listing_collection.create_index([("title", ASCENDING)])
listing_collection.create_index([("price", ASCENDING)])
listing_collection.create_index([("features", ASCENDING)], name="features_index")
# Create indexes for analytics
analytics_collection.create_index([("id", ASCENDING), ("product_id", ASCENDING)])
analytics_collection.create_index([("product_id", ASCENDING), ("created_at", ASCENDING)])
analytics_collection.create_index([("id", ASCENDING)], unique=True)
analytics_collection.create_index([("product_id", ASCENDING)], unique=True)
analytics_collection.create_index([("created_at", ASCENDING)])
analytics_collection.create_index([("updated_at", ASCENDING)])
analytics_collection.create_index([("sales_performance.total_sales", ASCENDING)])
analytics_collection.create_index([("sales_performance.revenue", ASCENDING)])
analytics_collection.create_index([("sales_performance.average_price", ASCENDING)])
analytics_collection.create_index([("customer_behavior.view_to_purchase_rate", ASCENDING)])
analytics_collection.create_index([("customer_behavior.repeat_purchase_rate", ASCENDING)])
analytics_collection.create_index([("customer_behavior.average_rating", ASCENDING)])
analytics_collection.create_index([("marketing_metrics.click_through_rate", ASCENDING)])
analytics_collection.create_index([("marketing_metrics.social_media_engagement", ASCENDING)])
# Create indexes for review
review_collection.create_index([("product_id", ASCENDING), ("rating", ASCENDING)])
review_collection.create_index([("user_id", ASCENDING), ("product_id", ASCENDING)])
review_collection.create_index([("id", ASCENDING)], unique=True)
review_collection.create_index([("product_id", ASCENDING)])
review_collection.create_index([("user_id", ASCENDING)])
review_collection.create_index([("rating", ASCENDING)])
review_collection.create_index([("title", ASCENDING)])
review_collection.create_index([("verified_purchase", ASCENDING)])
review_collection.create_index([("created_at", ASCENDING)])
review_collection.create_index([("updated_at", ASCENDING)])
# Create indexes for video
video_collection.create_index([("title", ASCENDING), ("views", ASCENDING)])
video_collection.create_index([("views", ASCENDING), ("rating", ASCENDING)])
video_collection.create_index([("id", ASCENDING)], unique=True)
video_collection.create_index([("title", ASCENDING)])
video_collection.create_index([("category", ASCENDING)])
video_collection.create_index([("subcategory", ASCENDING)])
video_collection.create_index([("duration", ASCENDING)])
video_collection.create_index([("views", ASCENDING)])
video_collection.create_index([("transcript_summary", "text")])
video_collection.create_index([("price_range", ASCENDING)])
video_collection.create_index([("created_at", ASCENDING)])
video_collection.create_index([("updated_at", ASCENDING)])
video_collection.create_index([("key_features", ASCENDING)])
video_collection.create_index([("highlights", ASCENDING)])
# Create indexes for video listing
video_listings_collection.create_index([("video_id", ASCENDING), ("id", ASCENDING)])
video_listings_collection.create_index([("id", ASCENDING)], unique=True)
video_listings_collection.create_index([("video_id", ASCENDING)])
video_listings_collection.create_index([("platform", ASCENDING)])
video_listings_collection.create_index([("title", ASCENDING)])
video_listings_collection.create_index([("views", ASCENDING)])
video_listings_collection.create_index([("rating", ASCENDING)])
video_listings_collection.create_index([("created_at", ASCENDING)])
video_listings_collection.create_index([("updated_at", ASCENDING)])
video_listings_collection.create_index([("product_links.price", ASCENDING)])
# Create indexes for video analytics
video_analytics_collection.create_index([("id", ASCENDING), ("video_id", ASCENDING)])
video_analytics_collection.create_index([("engagement.views", ASCENDING), ("engagement.likes", ASCENDING)])
video_analytics_collection.create_index([("performance.retention_rate", ASCENDING), ("performance.click_through_rate", ASCENDING)])
video_analytics_collection.create_index([("id", ASCENDING)], unique=True)
video_analytics_collection.create_index([("video_id", ASCENDING)])
video_analytics_collection.create_index([("created_at", ASCENDING)])
video_analytics_collection.create_index([("updated_at", ASCENDING)])
video_analytics_collection.create_index([("engagement.views", ASCENDING)])
video_analytics_collection.create_index([("engagement.likes", ASCENDING)])
video_analytics_collection.create_index([("engagement.comments", ASCENDING)])
video_analytics_collection.create_index([("engagement.average_watch_time", ASCENDING)])
video_analytics_collection.create_index([("audience.demographics", ASCENDING)])
video_analytics_collection.create_index([("audience.top_regions", ASCENDING)])
video_analytics_collection.create_index([("performance.retention_rate", ASCENDING)])
video_analytics_collection.create_index([("performance.click_through_rate", ASCENDING)])
# Sample product reference data
sample_products = [
{
"category": "Electronics",
"subcategory": "Smartphones",
"brand_options": ["Samsung Galaxy S24", "iPhone 15", "Google Pixel 8"],
"price_ranges": {
"budget": {"min": 299, "max": 499},
"mid_range": {"min": 500, "max": 799},
"premium": {"min": 800, "max": 1299}
},
"common_features": [
"5G Connectivity",
"AI-Enhanced Camera",
"AMOLED Display",
"Fast Charging",
"Wireless Charging"
],
"keywords": ["smartphone", "mobile phone", "cell phone", "android", "ios"]
},
{
"category": "Electronics",
"subcategory": "Wireless Earbuds",
"brand_options": ["Apple AirPods Pro", "Samsung Galaxy Buds", "Google Pixel Buds"],
"price_ranges": {
"budget": {"min": 49, "max": 99},
"mid_range": {"min": 100, "max": 199},
"premium": {"min": 200, "max": 299}
},
"common_features": [
"Active Noise Cancellation",
"Touch Controls",
"Wireless Charging Case",
"Water Resistance",
"Voice Assistant Support"
],
"keywords": ["earbuds", "wireless earphones", "tws", "headphones"]
},
{
"category": "Electronics",
"subcategory": "Smartwatches",
"brand_options": ["Apple Watch Series 9", "Samsung Galaxy Watch 6", "Google Pixel Watch"],
"price_ranges": {
"budget": {"min": 149, "max": 249},
"mid_range": {"min": 250, "max": 399},
"premium": {"min": 400, "max": 799}
},
"common_features": [
"Health Monitoring",
"Fitness Tracking",
"GPS",
"Always-On Display",
"Water Resistance"
],
"keywords": ["smartwatch", "fitness tracker", "smart watch", "wearable"]
}
]
# Clear existing data
# product_references.delete_many({})
# listings.delete_many({})
# Insert sample data
# product_references.insert_many(sample_products)
logger.info("File loaded successfully")
# Insert sample listings
# sample_listings = [
# {
# "product_id": str(product_references.find_one({"brand_options": "iPhone 15"})["_id"]),
# "title": "iPhone 15 Pro Max",
# "category": "Electronics",
# "subcategory": "Smartphones",
# "description": "Latest iPhone with A17 Pro chip and titanium design",
# "price": "$999",
# "features": [
# "48MP Main Camera",
# "Titanium Design",
# "Action Button"
# ],
# "keywords": ["iphone", "smartphone", "apple"],
# "original_caption": "Just got the new iPhone 15 Pro! Amazing camera system!",
# "created_at": datetime.utcnow(),
# "status": "active"
# }
# ]
# listings.insert_many(sample_listings)
# logger.info("Sample listings inserted successfully")
except Exception as e:
logger.error(f"Error setting up database: {e}")
finally:
client.close()
if __name__ == "__main__":
setup_product_database()