ABAO77 commited on
Commit
cfe5245
·
verified ·
1 Parent(s): 58d41ff

Upload 164 files

Browse files
src/apis/controllers/__pycache__/auth_controller.cpython-311.pyc CHANGED
Binary files a/src/apis/controllers/__pycache__/auth_controller.cpython-311.pyc and b/src/apis/controllers/__pycache__/auth_controller.cpython-311.pyc differ
 
src/apis/controllers/__pycache__/destination_controller.cpython-311.pyc CHANGED
Binary files a/src/apis/controllers/__pycache__/destination_controller.cpython-311.pyc and b/src/apis/controllers/__pycache__/destination_controller.cpython-311.pyc differ
 
src/apis/controllers/__pycache__/post_controller.cpython-311.pyc CHANGED
Binary files a/src/apis/controllers/__pycache__/post_controller.cpython-311.pyc and b/src/apis/controllers/__pycache__/post_controller.cpython-311.pyc differ
 
src/apis/controllers/post_controller.py CHANGED
@@ -62,13 +62,18 @@ async def get_a_post_controller(post_id: str) -> Dict:
62
  return {"status": "error", "message": str(e)}
63
 
64
 
 
 
 
65
  async def list_all_posts_controller_priority(
66
  user_id: str,
67
  page: int = 1,
68
  top_5_destinations: List[str] = [],
69
  view_post_ids: List[str] = [],
70
  ):
 
71
  try:
 
72
  PAGE_SIZE = 10
73
  TOP_LIMIT = int(PAGE_SIZE * 0.7)
74
  OTHER_LIMIT = PAGE_SIZE - TOP_LIMIT
@@ -87,13 +92,11 @@ async def list_all_posts_controller_priority(
87
  "_id": {"$nin": excluded_ids},
88
  }
89
 
90
- # Đếm tổng số bài viết mỗi nhóm để tính total_items/pages
91
  total_top = await PostCRUD.count(top_filter)
92
  total_other = await PostCRUD.count(other_filter)
93
  total_items = total_top + total_other
94
  total_pages = math.ceil(total_items / PAGE_SIZE)
95
 
96
- # Lấy bài viết đã tính điểm
97
  top_posts = await PostCRUD.find_many_with_score(
98
  filter=top_filter,
99
  top_destinations=top_5_destinations,
@@ -108,10 +111,12 @@ async def list_all_posts_controller_priority(
108
  skip=skip_other,
109
  )
110
 
111
- # Xen kẽ
112
  combined_posts = []
113
  i = j = 0
114
- while len(combined_posts) < PAGE_SIZE and (i < len(top_posts) or j < len(other_posts)):
 
 
115
  if i < len(top_posts):
116
  combined_posts.append(top_posts[i])
117
  i += 1
@@ -119,26 +124,25 @@ async def list_all_posts_controller_priority(
119
  combined_posts.append(other_posts[j])
120
  j += 1
121
 
122
- # Enrich thông tin
123
  user_ids = list({post.get("user_id") for post in combined_posts})
124
  destination_ids = list({post.get("destination_id") for post in combined_posts})
125
- post_ids = [post["_id"] for post in combined_posts]
126
-
127
- user_infos, destination_infos, reactions = await gather(
128
- *[
129
- gather(*[UserCRUD.find_by_id(uid) for uid in user_ids]),
130
- gather(*[DestinationCRUD.find_by_id(did) for did in destination_ids]),
131
- gather(
132
- *[
133
- ReactionCRUD.read_one(
134
- {"user_id": user_id, "post_id": serialize_datetime(pid)}
135
- )
136
- for pid in post_ids
137
- ]
138
- ),
139
- ]
140
  )
141
 
 
 
 
 
 
 
 
 
 
 
142
  user_info_map = {
143
  info["_id"]: {
144
  "user_id": info["_id"],
@@ -188,7 +192,8 @@ async def list_all_posts_controller_priority(
188
  "user_info": user_info_map.get(uid),
189
  }
190
  )
191
-
 
192
  return {
193
  "status": "success",
194
  "message": {
@@ -204,8 +209,10 @@ async def list_all_posts_controller_priority(
204
  logger.error(f"Error listing personalized posts: {str(e)}")
205
  return {"status": "error", "message": str(e)}
206
 
 
207
  async def list_all_posts_controller(user_id: str, page: int = 1):
208
  try:
 
209
  PAGE_SIZE = 5
210
  # Calculate skip value for pagination
211
  skip = (page - 1) * PAGE_SIZE
@@ -222,8 +229,15 @@ async def list_all_posts_controller(user_id: str, page: int = 1):
222
  sort=[("created_at", -1)], # Sort by created_at descending
223
  )
224
 
 
225
  user_ids = list({post.get("user_id") for post in posts})
226
- user_infos = await gather(*[UserCRUD.find_by_id(uid) for uid in user_ids])
 
 
 
 
 
 
227
  user_info_map = {
228
  info.get("_id"): {
229
  "user_id": info.get("_id"),
@@ -233,22 +247,20 @@ async def list_all_posts_controller(user_id: str, page: int = 1):
233
  for info in user_infos
234
  if info
235
  }
236
- destination_ids = list({post.get("destination_id") for post in posts})
237
- destination_infos = await gather(
238
- *[DestinationCRUD.find_by_id(did) for did in destination_ids]
239
- )
240
  destination_info_map = {
241
  info.get("_id"): info.get("name") for info in destination_infos if info
242
  }
 
243
  formatted_user_reactions_map = {}
244
  if user_id:
245
  all_post_ids = [serialize_datetime(post.get("_id")) for post in posts]
246
- reactions = await gather(
247
- *[
248
- ReactionCRUD.read_one({"user_id": user_id, "post_id": post_id})
249
- for post_id in all_post_ids
250
- ]
251
- )
252
  for reaction in reactions:
253
  if reaction:
254
  post_id = reaction.get("post_id")
@@ -298,6 +310,7 @@ async def list_posts_by_destination_controller(
298
  destination_id: str, user_id: str, page: int = 1
299
  ):
300
  try:
 
301
  PAGE_SIZE = 5 # Changed from 1 to 5 posts per page
302
  # Calculate skip value for pagination
303
  skip = (page - 1) * PAGE_SIZE
@@ -328,7 +341,9 @@ async def list_posts_by_destination_controller(
328
 
329
  # Get unique user_ids
330
  user_ids = list({post.get("user_id") for post in posts})
331
- user_infos = await gather(*[UserCRUD.find_by_id(uid) for uid in user_ids])
 
 
332
  user_info_map = {
333
  info.get("_id"): {
334
  "user_id": info.get("_id"),
@@ -347,12 +362,11 @@ async def list_posts_by_destination_controller(
347
  formatted_user_reactions_map = {}
348
  if user_id:
349
  all_post_ids = [serialize_datetime(post.get("_id")) for post in posts]
350
- reactions = await gather(
351
- *[
352
- ReactionCRUD.read_one({"user_id": user_id, "post_id": post_id})
353
- for post_id in all_post_ids
354
- ]
355
  )
 
356
  for reaction in reactions:
357
  if reaction:
358
  post_id = reaction.get("post_id")
 
62
  return {"status": "error", "message": str(e)}
63
 
64
 
65
+ import time
66
+
67
+
68
  async def list_all_posts_controller_priority(
69
  user_id: str,
70
  page: int = 1,
71
  top_5_destinations: List[str] = [],
72
  view_post_ids: List[str] = [],
73
  ):
74
+ print("view_post_ids", view_post_ids)
75
  try:
76
+ start_time = time.time()
77
  PAGE_SIZE = 10
78
  TOP_LIMIT = int(PAGE_SIZE * 0.7)
79
  OTHER_LIMIT = PAGE_SIZE - TOP_LIMIT
 
92
  "_id": {"$nin": excluded_ids},
93
  }
94
 
 
95
  total_top = await PostCRUD.count(top_filter)
96
  total_other = await PostCRUD.count(other_filter)
97
  total_items = total_top + total_other
98
  total_pages = math.ceil(total_items / PAGE_SIZE)
99
 
 
100
  top_posts = await PostCRUD.find_many_with_score(
101
  filter=top_filter,
102
  top_destinations=top_5_destinations,
 
111
  skip=skip_other,
112
  )
113
 
114
+ # Xen kẽ
115
  combined_posts = []
116
  i = j = 0
117
+ while len(combined_posts) < PAGE_SIZE and (
118
+ i < len(top_posts) or j < len(other_posts)
119
+ ):
120
  if i < len(top_posts):
121
  combined_posts.append(top_posts[i])
122
  i += 1
 
124
  combined_posts.append(other_posts[j])
125
  j += 1
126
 
 
127
  user_ids = list({post.get("user_id") for post in combined_posts})
128
  destination_ids = list({post.get("destination_id") for post in combined_posts})
129
+ post_ids = [serialize_datetime(post["_id"]) for post in combined_posts]
130
+
131
+ user_infos_result = await UserCRUD.read({"_id": {"$in": user_ids}})
132
+ destination_infos_result = await DestinationCRUD.read(
133
+ {"_id": {"$in": destination_ids}}
 
 
 
 
 
 
 
 
 
 
134
  )
135
 
136
+ reactions_result = (
137
+ await ReactionCRUD.read({"user_id": user_id, "post_id": {"$in": post_ids}})
138
+ if user_id
139
+ else []
140
+ )
141
+
142
+ user_infos = user_infos_result
143
+ destination_infos = destination_infos_result
144
+ reactions = reactions_result
145
+
146
  user_info_map = {
147
  info["_id"]: {
148
  "user_id": info["_id"],
 
192
  "user_info": user_info_map.get(uid),
193
  }
194
  )
195
+ end_time = time.time()
196
+ print(f"Time taken: {end_time - start_time} seconds")
197
  return {
198
  "status": "success",
199
  "message": {
 
209
  logger.error(f"Error listing personalized posts: {str(e)}")
210
  return {"status": "error", "message": str(e)}
211
 
212
+
213
  async def list_all_posts_controller(user_id: str, page: int = 1):
214
  try:
215
+ start_time = time.time()
216
  PAGE_SIZE = 5
217
  # Calculate skip value for pagination
218
  skip = (page - 1) * PAGE_SIZE
 
229
  sort=[("created_at", -1)], # Sort by created_at descending
230
  )
231
 
232
+ # Get unique user and destination IDs
233
  user_ids = list({post.get("user_id") for post in posts})
234
+ destination_ids = list({post.get("destination_id") for post in posts})
235
+
236
+ # Use $in operator for batch lookups
237
+ user_infos = await UserCRUD.read({"_id": {"$in": user_ids}})
238
+ destination_infos = await DestinationCRUD.read({"_id": {"$in": destination_ids}})
239
+
240
+ # Create user info map
241
  user_info_map = {
242
  info.get("_id"): {
243
  "user_id": info.get("_id"),
 
247
  for info in user_infos
248
  if info
249
  }
250
+
251
+ # Create destination info map
 
 
252
  destination_info_map = {
253
  info.get("_id"): info.get("name") for info in destination_infos if info
254
  }
255
+
256
  formatted_user_reactions_map = {}
257
  if user_id:
258
  all_post_ids = [serialize_datetime(post.get("_id")) for post in posts]
259
+ # Use $in operator for batch lookup of reactions
260
+ reactions = await ReactionCRUD.read({
261
+ "user_id": user_id,
262
+ "post_id": {"$in": all_post_ids}
263
+ })
 
264
  for reaction in reactions:
265
  if reaction:
266
  post_id = reaction.get("post_id")
 
310
  destination_id: str, user_id: str, page: int = 1
311
  ):
312
  try:
313
+ start_time = time.time()
314
  PAGE_SIZE = 5 # Changed from 1 to 5 posts per page
315
  # Calculate skip value for pagination
316
  skip = (page - 1) * PAGE_SIZE
 
341
 
342
  # Get unique user_ids
343
  user_ids = list({post.get("user_id") for post in posts})
344
+
345
+ # Use $in operator for batch user lookup
346
+ user_infos = await UserCRUD.read({"_id": {"$in": user_ids}})
347
  user_info_map = {
348
  info.get("_id"): {
349
  "user_id": info.get("_id"),
 
362
  formatted_user_reactions_map = {}
363
  if user_id:
364
  all_post_ids = [serialize_datetime(post.get("_id")) for post in posts]
365
+ # Use $in operator for batch lookup of reactions
366
+ reactions = await ReactionCRUD.read(
367
+ {"user_id": user_id, "post_id": {"$in": all_post_ids}}
 
 
368
  )
369
+
370
  for reaction in reactions:
371
  if reaction:
372
  post_id = reaction.get("post_id")
src/apis/models/__pycache__/destination_models.cpython-311.pyc CHANGED
Binary files a/src/apis/models/__pycache__/destination_models.cpython-311.pyc and b/src/apis/models/__pycache__/destination_models.cpython-311.pyc differ
 
src/apis/models/destination_models.py CHANGED
@@ -7,7 +7,12 @@ from bson import ObjectId
7
  class Destination(BaseDocument):
8
  name: str = Field("", description="Destination's name")
9
  description: str = Field("", description="Destination's description")
 
 
10
  image: str = Field("", description="Destination's picture")
 
 
 
11
  created_user_id: str = Field("", description="Destination's created user")
12
  updated_user_id: str = Field("", description="Destination's updated user")
13
  model_config = {
@@ -15,7 +20,12 @@ class Destination(BaseDocument):
15
  "example": {
16
  "name": "Hanoi",
17
  "description": "Hanoi is the capital of Vietnam",
 
 
18
  "image": "https://www.google.com.vn",
 
 
 
19
  "created_user_id": "1234567890",
20
  "updated_user_id": "1234567890",
21
  }
 
7
  class Destination(BaseDocument):
8
  name: str = Field("", description="Destination's name")
9
  description: str = Field("", description="Destination's description")
10
+ type: str = Field("", description="Destination's type")
11
+ tags: str = Field("", description="Destination's tags")
12
  image: str = Field("", description="Destination's picture")
13
+ lat: float = Field(0, description="Destination's latitude")
14
+ long: float = Field(0, description="Destination's longitude")
15
+ exclude_weather: list[str] = Field([], description="Destination's exclude weather")
16
  created_user_id: str = Field("", description="Destination's created user")
17
  updated_user_id: str = Field("", description="Destination's updated user")
18
  model_config = {
 
20
  "example": {
21
  "name": "Hanoi",
22
  "description": "Hanoi is the capital of Vietnam",
23
+ "type": "city",
24
+ "tags": "Vietnam, Hanoi, Capital",
25
  "image": "https://www.google.com.vn",
26
+ "lat": 0,
27
+ "long": 0,
28
+ "exclude_weather": ["rainy", "snowy"],
29
  "created_user_id": "1234567890",
30
  "updated_user_id": "1234567890",
31
  }
src/langgraph/langchain/__pycache__/llm.cpython-311.pyc CHANGED
Binary files a/src/langgraph/langchain/__pycache__/llm.cpython-311.pyc and b/src/langgraph/langchain/__pycache__/llm.cpython-311.pyc differ