LogicGoInfotechSpaces commited on
Commit
22f58a9
·
verified ·
1 Parent(s): 0fb9b06

Update app/database.py

Browse files
Files changed (1) hide show
  1. app/database.py +41 -23
app/database.py CHANGED
@@ -171,6 +171,8 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
171
  # Ensure today is at midnight
172
  today_start = today.replace(hour=0, minute=0, second=0, microsecond=0)
173
 
 
 
174
  # Check if today's date already exists
175
  user_doc = collection.find_one({"userId": user_object_id})
176
 
@@ -194,6 +196,7 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
194
 
195
  # Get existing daily counts
196
  existing_counts = user_doc.get("ai_edit_daily_count", [])
 
197
 
198
  # Check if today's date already exists
199
  today_exists = False
@@ -203,8 +206,10 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
203
  # Normalize to midnight for comparison
204
  if isinstance(entry_date, datetime):
205
  normalized_date = entry_date.replace(hour=0, minute=0, second=0, microsecond=0)
 
206
  if normalized_date == today_start:
207
  today_exists = True
 
208
  break
209
 
210
  # If today exists, do nothing (leave it as is)
@@ -213,6 +218,8 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
213
  return
214
 
215
  # Today doesn't exist - need to add it and fill missing dates
 
 
216
  # Find the latest date in existing counts
217
  last_date = None
218
  if existing_counts:
@@ -225,6 +232,11 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
225
  dates.append(entry_date.replace(hour=0, minute=0, second=0, microsecond=0))
226
  if dates:
227
  last_date = max(dates)
 
 
 
 
 
228
 
229
  # Generate missing dates between last_date and today
230
  dates_to_add = []
@@ -239,12 +251,14 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
239
  "count": 0
240
  })
241
  current_date += timedelta(days=1)
 
242
 
243
  # Add today's entry with count 1
244
  dates_to_add.append({
245
  "date": today_start,
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).
@@ -259,16 +273,20 @@ def _update_daily_count(collection, user_object_id: ObjectId, today: datetime) -
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,7 +586,6 @@ def close_connection():
568
 
569
 
570
 
571
-
572
  # """
573
  # MongoDB database connection and logging utilities, including admin media click logging.
574
  # """
@@ -817,28 +834,29 @@ def close_connection():
817
  # "count": 1
818
  # })
819
 
820
- # # Push all missing dates (including today)
 
821
  # if dates_to_add:
822
- # # Ensure ai_edit_daily_count field exists
823
- # if "ai_edit_daily_count" not in user_doc:
824
- # collection.update_one(
825
- # {"userId": user_object_id},
826
- # {"$set": {"ai_edit_daily_count": []}}
827
- # )
828
-
 
 
 
 
 
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
- # }
840
  # )
841
- # logger.debug("Added %d daily count entries (including today with count 1)", len(dates_to_add))
842
 
843
  # def log_api_call(
844
  # endpoint: str,
 
171
  # Ensure today is at midnight
172
  today_start = today.replace(hour=0, minute=0, second=0, microsecond=0)
173
 
174
+ logger.info("Updating daily count for user %s, today: %s", str(user_object_id), today_start.isoformat())
175
+
176
  # Check if today's date already exists
177
  user_doc = collection.find_one({"userId": user_object_id})
178
 
 
196
 
197
  # Get existing daily counts
198
  existing_counts = user_doc.get("ai_edit_daily_count", [])
199
+ logger.debug("Existing daily counts: %d entries", len(existing_counts))
200
 
201
  # Check if today's date already exists
202
  today_exists = False
 
206
  # Normalize to midnight for comparison
207
  if isinstance(entry_date, datetime):
208
  normalized_date = entry_date.replace(hour=0, minute=0, second=0, microsecond=0)
209
+ logger.debug("Comparing entry date %s with today %s", normalized_date.isoformat(), today_start.isoformat())
210
  if normalized_date == today_start:
211
  today_exists = True
212
+ logger.info("Today's date already exists in daily count, leaving unchanged: %s", today_start.isoformat())
213
  break
214
 
215
  # If today exists, do nothing (leave it as is)
 
218
  return
219
 
220
  # Today doesn't exist - need to add it and fill missing dates
221
+ logger.info("Today's date does not exist in daily count, adding new entry")
222
+
223
  # Find the latest date in existing counts
224
  last_date = None
225
  if existing_counts:
 
232
  dates.append(entry_date.replace(hour=0, minute=0, second=0, microsecond=0))
233
  if dates:
234
  last_date = max(dates)
235
+ logger.debug("Last date found: %s", last_date.isoformat())
236
+ else:
237
+ logger.debug("No valid dates found in existing counts")
238
+ else:
239
+ logger.debug("No existing counts, will add today as first entry")
240
 
241
  # Generate missing dates between last_date and today
242
  dates_to_add = []
 
251
  "count": 0
252
  })
253
  current_date += timedelta(days=1)
254
+ logger.debug("Filled %d gap dates between %s and %s", len(dates_to_add), last_date.isoformat(), today_start.isoformat())
255
 
256
  # Add today's entry with count 1
257
  dates_to_add.append({
258
  "date": today_start,
259
  "count": 1
260
  })
261
+ logger.info("Adding %d new date entries (including today with count 1)", len(dates_to_add))
262
 
263
  # Merge existing entries with the new ones, sort by date (oldest first),
264
  # and keep only the most recent 32 dates (drop the oldest beyond 32).
 
273
 
274
  all_entries.sort(key=_entry_sort_key)
275
  if len(all_entries) > 32:
276
+ removed = len(all_entries) - 32
277
  all_entries = all_entries[-32:]
278
+ logger.debug("Removed %d oldest entries to maintain 32-entry limit", removed)
279
 
280
+ result = collection.update_one(
281
  {"userId": user_object_id},
282
  {"$set": {"ai_edit_daily_count": all_entries}},
283
  )
284
+ logger.info(
285
+ "Updated ai_edit_daily_count with %d entries (oldest first, max 32). Matched: %d, Modified: %d",
286
+ len(all_entries), result.matched_count, result.modified_count,
287
  )
288
+ else:
289
+ logger.warning("No dates to add - this should not happen!")
290
 
291
  def log_api_call(
292
  endpoint: str,
 
586
 
587
 
588
 
 
589
  # """
590
  # MongoDB database connection and logging utilities, including admin media click logging.
591
  # """
 
834
  # "count": 1
835
  # })
836
 
837
+ # # Merge existing entries with the new ones, sort by date (oldest first),
838
+ # # and keep only the most recent 32 dates (drop the oldest beyond 32).
839
  # if dates_to_add:
840
+ # all_entries = list(existing_counts) + dates_to_add
841
+
842
+ # def _entry_sort_key(entry: Dict[str, Any]) -> datetime:
843
+ # dt = entry.get("date")
844
+ # if isinstance(dt, datetime):
845
+ # return dt.replace(hour=0, minute=0, second=0, microsecond=0)
846
+ # return datetime.min
847
+
848
+ # all_entries.sort(key=_entry_sort_key)
849
+ # if len(all_entries) > 32:
850
+ # all_entries = all_entries[-32:]
851
+
852
  # collection.update_one(
853
  # {"userId": user_object_id},
854
+ # {"$set": {"ai_edit_daily_count": all_entries}},
855
+ # )
856
+ # logger.debug(
857
+ # "Updated ai_edit_daily_count with %d entries (oldest first, max 32)",
858
+ # len(all_entries),
 
 
 
 
859
  # )
 
860
 
861
  # def log_api_call(
862
  # endpoint: str,