LogicGoInfotechSpaces commited on
Commit
4016953
·
verified ·
1 Parent(s): 6107421

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -207,29 +207,46 @@ async def log_media_click(user_id: str, category_oid_str: str):
207
  {"userId": user_oid},
208
  {"ai_edit_daily_count": 1}
209
  )
210
-
211
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
212
- is_first_time = len(daily_entries) == 0
213
-
214
- existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
215
  daily_updates = []
216
-
217
- if is_first_time:
218
- # First ever usage → only today
219
  daily_updates.append({"date": today_date, "count": 1})
220
  else:
221
- # Backfill yesterday if missing
222
- if yesterday_date.date() not in existing_dates:
223
- daily_updates.append({"date": yesterday_date, "count": 0})
224
- # Insert today if missing
225
- if today_date.date() not in existing_dates:
 
 
 
 
 
 
 
 
226
  daily_updates.append({"date": today_date, "count": 1})
227
-
 
228
  if daily_updates:
229
  await media_clicks_col.update_one(
230
  {"userId": user_oid},
231
- {"$push": {"ai_edit_daily_count": {"$each": daily_updates, "$sort": {"date": 1}}}}
232
  )
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
  # -------------------------------------------------
235
  # STEP 2: Try updating existing category
 
207
  {"userId": user_oid},
208
  {"ai_edit_daily_count": 1}
209
  )
 
210
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
 
 
 
211
  daily_updates = []
212
+
213
+ if not daily_entries:
214
+ # First-ever usage → only today
215
  daily_updates.append({"date": today_date, "count": 1})
216
  else:
217
+ # Build existing date map for lookup
218
+ existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
219
+ last_date_in_db = max(entry["date"].date() for entry in daily_entries)
220
+
221
+ # Fill missing days between last recorded day and today-1
222
+ next_day = last_date_in_db + timedelta(days=1)
223
+ while next_day < today_date:
224
+ if next_day not in existing_dates:
225
+ daily_updates.append({"date": next_day, "count": 0})
226
+ next_day += timedelta(days=1)
227
+
228
+ # Add today if not already present
229
+ if today_date not in existing_dates:
230
  daily_updates.append({"date": today_date, "count": 1})
231
+
232
+ # Push updates if any
233
  if daily_updates:
234
  await media_clicks_col.update_one(
235
  {"userId": user_oid},
236
+ {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
237
  )
238
+
239
+ # Sort oldest → newest and trim to last 32 entries
240
+ doc = await media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
241
+ daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
242
+ daily_entries.sort(key=lambda x: x["date"])
243
+ if len(daily_entries) > 32:
244
+ daily_entries = daily_entries[-32:]
245
+ await media_clicks_col.update_one(
246
+ {"userId": user_oid},
247
+ {"$set": {"ai_edit_daily_count": daily_entries}}
248
+ )
249
+
250
 
251
  # -------------------------------------------------
252
  # STEP 2: Try updating existing category