Spaces:
Sleeping
Sleeping
| """ | |
| Optimized projection fields for different use cases to reduce data transfer and improve performance. | |
| """ | |
| from datetime import datetime | |
| CURRENT_DATE = datetime.now() | |
| # Minimal fields for list views and search results | |
| MINIMAL_FIELDS = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "average_rating": "$average_rating.value", | |
| "city": 1, | |
| "merchant_category": 1, | |
| "profile_picture": {"$arrayElemAt": ["$display_picture", 0]}, | |
| } | |
| # Essential fields for card views | |
| CARD_FIELDS = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "business_url": 1, | |
| "description": 1, | |
| "profile_picture": {"$arrayElemAt": ["$display_picture", 0]}, | |
| "average_rating": "$average_rating.value", | |
| "city": 1, | |
| "merchant_category": 1, | |
| "merchant_subcategory": 1, | |
| # Include entire address object (formatted_address fallback handled in pipeline) | |
| "address": 1, | |
| "promotions": 1, | |
| } | |
| # Optimized years_in_business calculation using $dateDiff (MongoDB 5.0+) | |
| YEARS_IN_BUSINESS_OPTIMIZED = { | |
| "$let": { | |
| "vars": { | |
| "years": {"$dateDiff": {"startDate": "$available_from", "endDate": "$$NOW", "unit": "year"}}, | |
| "months": {"$dateDiff": {"startDate": "$available_from", "endDate": "$$NOW", "unit": "month"}}, | |
| "days": {"$dateDiff": {"startDate": "$available_from", "endDate": "$$NOW", "unit": "day"}} | |
| }, | |
| "in": { | |
| "$switch": { | |
| "branches": [ | |
| { | |
| "case": {"$gte": ["$$years", 1]}, | |
| "then": {"$concat": [{"$toString": "$$years"}, " years in business"]} | |
| }, | |
| { | |
| "case": {"$gte": ["$$months", 1]}, | |
| "then": {"$concat": [{"$toString": "$$months"}, " months in business"]} | |
| } | |
| ], | |
| "default": {"$concat": [{"$toString": "$$days"}, " days in business"]} | |
| } | |
| } | |
| } | |
| } | |
| # Optimized common fields with improved years_in_business calculation | |
| COMMON_FIELDS_OPTIMIZED = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "business_url": 1, | |
| "description": 1, | |
| "display_picture": 1, | |
| "profile_picture": {"$arrayElemAt": ["$display_picture", 0]}, | |
| "average_rating": 1, | |
| "city": 1, | |
| "country": 1, | |
| "merchant_category": 1, | |
| "merchant_subcategory": 1, | |
| "address": 1, | |
| "business_hour": 1, | |
| "promotions": 1, | |
| "trending": 1, | |
| "amenities": 1, | |
| "cancellation_policy": 1, | |
| "share_description": 1, | |
| "years_in_business": YEARS_IN_BUSINESS_OPTIMIZED | |
| } | |
| # Fields for detailed merchant view | |
| DETAILED_FIELDS = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "business_url": 1, | |
| "description": 1, | |
| "display_picture": 1, | |
| "profile_picture": {"$arrayElemAt": ["$display_picture", 0]}, | |
| "average_rating": 1, | |
| "city": 1, | |
| "country": 1, | |
| "merchant_category": 1, | |
| "merchant_subcategory": 1, | |
| "address": 1, | |
| "business_hour": 1, | |
| "promotions": 1, | |
| "trending": 1, | |
| "amenities": 1, | |
| "cancellation_policy": 1, | |
| "share_description": 1, | |
| "payment_modes": 1, | |
| "contact_info": 1, | |
| "social_media": 1, | |
| "years_in_business": YEARS_IN_BUSINESS_OPTIMIZED | |
| } | |
| # Fields for recommended merchants (lightweight) | |
| RECOMMENDED_FIELDS_OPTIMIZED = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "address.formatted_address": 1, | |
| "location_id": 1, | |
| "business_name": 1, | |
| "business_url": 1, | |
| "description": 1, | |
| "business_hour.weekdays": 1, # Only weekdays for quick availability check | |
| "promotions": 1, | |
| "cancellation_policy": 1, | |
| "amenities": 1, | |
| "profile_picture": {"$arrayElemAt": ["$display_picture", 0]}, | |
| "average_rating": "$average_rating.value", | |
| "share_description": 1, | |
| "city": 1, | |
| "country": 1, | |
| "merchant_subcategory": 1, | |
| "merchant_category": 1, | |
| "payment_modes": 1 | |
| } | |
| # Fields for search results with geospatial data | |
| SEARCH_FIELDS_WITH_GEO = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "business_url": 1, | |
| "description": 1, | |
| "profile_picture": {"$arrayElemAt": ["$display_picture", 0]}, | |
| "average_rating": 1, | |
| "city": 1, | |
| "merchant_category": 1, | |
| "merchant_subcategory": 1, | |
| "address": 1, | |
| "promotions": 1, | |
| "distance": { | |
| "$round": [ | |
| { | |
| "$multiply": [ | |
| { | |
| "$acos": { | |
| "$add": [ | |
| { | |
| "$multiply": [ | |
| {"$sin": {"$degreesToRadians": {"$arrayElemAt": ["$address.location.coordinates", 1]}}}, | |
| {"$sin": {"$degreesToRadians": "$$userLat"}} | |
| ] | |
| }, | |
| { | |
| "$multiply": [ | |
| {"$cos": {"$degreesToRadians": {"$arrayElemAt": ["$address.location.coordinates", 1]}}}, | |
| {"$cos": {"$degreesToRadians": "$$userLat"}}, | |
| {"$cos": {"$degreesToRadians": {"$subtract": [{"$arrayElemAt": ["$address.location.coordinates", 0]}, "$$userLng"]}}} | |
| ] | |
| } | |
| ] | |
| } | |
| }, | |
| 6371000 # Earth's radius in meters | |
| ] | |
| }, | |
| 0 | |
| ] | |
| } | |
| } | |
| # Fields for catalogue and associate data (minimal for performance) | |
| CATALOGUE_MINIMAL_FIELDS = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "location_id": 1, | |
| "catalogue": { | |
| "$map": { | |
| "input": {"$objectToArray": "$catalogue"}, | |
| "as": "cat", | |
| "in": { | |
| "category": "$$cat.k", | |
| "services": { | |
| "$map": { | |
| "input": "$$cat.v", | |
| "as": "service", | |
| "in": { | |
| "service_id": "$$service.service_id", | |
| "service_name": "$$service.service_name", | |
| "price": {"$round": ["$$service.price", 2]}, | |
| "currency": "$$service.currency", | |
| "duration": "$$service.duration", | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| ASSOCIATE_MINIMAL_FIELDS = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "business_name": 1, | |
| "location_id": 1, | |
| "associates": { | |
| "$map": { | |
| "input": "$associate", | |
| "as": "s", | |
| "in": { | |
| "associate_id": "$$s.associate_id", | |
| "name": "$$s.name", | |
| "role": "$$s.role", | |
| "rating": "$$s.rating", | |
| } | |
| } | |
| } | |
| } | |
| # Performance monitoring fields | |
| PERFORMANCE_FIELDS = { | |
| "_id": 0, | |
| "merchant_id": 1, | |
| "stats.total_bookings": 1, | |
| "stats.response_time": 1, | |
| "average_rating.total_reviews": 1, | |
| "go_live_from": 1, | |
| "last_updated": 1 | |
| } |