LogicGoInfotechSpaces commited on
Commit
0fb9b06
·
verified ·
1 Parent(s): 571cdaa

Update app/database.py

Browse files
Files changed (1) hide show
  1. app/database.py +24 -21
app/database.py CHANGED
@@ -246,28 +246,29 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
246
  "count": 1
247
  })
248
 
249
- # Push all missing dates (including today)
 
250
  if dates_to_add:
251
- # Ensure ai_edit_daily_count field exists
252
- if "ai_edit_daily_count" not in user_doc:
253
- collection.update_one(
254
- {"userId": user_object_id},
255
- {"$set": {"ai_edit_daily_count": []}}
256
- )
257
-
 
 
 
 
 
258
  collection.update_one(
259
  {"userId": user_object_id},
260
- {
261
- # Append new dates and keep only the most recent 32 entries
262
- "$push": {
263
- "ai_edit_daily_count": {
264
- "$each": dates_to_add,
265
- "$slice": -32,
266
- }
267
- }
268
- }
269
  )
270
- logger.debug("Added %d daily count entries (including today with count 1)", len(dates_to_add))
271
 
272
  def log_api_call(
273
  endpoint: str,
@@ -567,6 +568,7 @@ def close_connection():
567
 
568
 
569
 
 
570
  # """
571
  # MongoDB database connection and logging utilities, including admin media click logging.
572
  # """
@@ -827,9 +829,11 @@ def close_connection():
827
  # collection.update_one(
828
  # {"userId": user_object_id},
829
  # {
 
830
  # "$push": {
831
  # "ai_edit_daily_count": {
832
- # "$each": dates_to_add
 
833
  # }
834
  # }
835
  # }
@@ -1130,5 +1134,4 @@ def close_connection():
1130
  # _admin_client.close()
1131
  # _admin_client = None
1132
  # _admin_db = None
1133
- # logger.info("Admin MongoDB connection closed")
1134
-
 
246
  "count": 1
247
  })
248
 
249
+ # Merge existing entries with the new ones, sort by date (oldest first),
250
+ # and keep only the most recent 32 dates (drop the oldest beyond 32).
251
  if dates_to_add:
252
+ all_entries = list(existing_counts) + dates_to_add
253
+
254
+ def _entry_sort_key(entry: Dict[str, Any]) -> datetime:
255
+ dt = entry.get("date")
256
+ if isinstance(dt, datetime):
257
+ return dt.replace(hour=0, minute=0, second=0, microsecond=0)
258
+ return datetime.min
259
+
260
+ all_entries.sort(key=_entry_sort_key)
261
+ if len(all_entries) > 32:
262
+ all_entries = all_entries[-32:]
263
+
264
  collection.update_one(
265
  {"userId": user_object_id},
266
+ {"$set": {"ai_edit_daily_count": all_entries}},
267
+ )
268
+ logger.debug(
269
+ "Updated ai_edit_daily_count with %d entries (oldest first, max 32)",
270
+ len(all_entries),
 
 
 
 
271
  )
 
272
 
273
  def log_api_call(
274
  endpoint: str,
 
568
 
569
 
570
 
571
+
572
  # """
573
  # MongoDB database connection and logging utilities, including admin media click logging.
574
  # """
 
829
  # collection.update_one(
830
  # {"userId": user_object_id},
831
  # {
832
+ # # Append new dates and keep only the most recent 32 entries
833
  # "$push": {
834
  # "ai_edit_daily_count": {
835
+ # "$each": dates_to_add,
836
+ # "$slice": -32,
837
  # }
838
  # }
839
  # }
 
1134
  # _admin_client.close()
1135
  # _admin_client = None
1136
  # _admin_db = None
1137
+ # logger.info("Admin MongoDB connection closed")