LogicGoInfotechSpaces commited on
Commit
c41d228
·
verified ·
1 Parent(s): ae96959

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -224,28 +224,45 @@ async def generate(
224
  # --------------------------------------------------
225
  doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
226
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
227
-
228
- is_first_time = len(daily_entries) == 0
229
- existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
230
  daily_updates = []
231
-
232
- if is_first_time:
233
  # First-ever usage → only today
234
  daily_updates.append({"date": today_date, "count": 1})
235
  else:
236
- # Backfill yesterday if missing
237
- if yesterday_date.date() not in existing_dates:
238
- daily_updates.append({"date": yesterday_date, "count": 0})
239
- # Insert today if missing
240
- if today_date.date() not in existing_dates:
 
 
 
 
 
 
 
241
  daily_updates.append({"date": today_date, "count": 1})
242
-
 
243
  if daily_updates:
244
  media_clicks_col.update_one(
245
  {"userId": user_oid},
246
- {"$push": {"ai_edit_daily_count": {"$each": daily_updates, "$sort": {"date": 1}}}}
247
  )
248
-
 
 
 
 
 
 
 
 
 
 
 
249
  # --------------------------------------------------
250
  # CATEGORY CLICK LOGIC
251
  # --------------------------------------------------
 
224
  # --------------------------------------------------
225
  doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
226
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
227
+
 
 
228
  daily_updates = []
229
+
230
+ if not daily_entries:
231
  # First-ever usage → only today
232
  daily_updates.append({"date": today_date, "count": 1})
233
  else:
234
+ existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
235
+ last_date_in_db = max(entry["date"].date() for entry in daily_entries)
236
+
237
+ # Fill all missing days between last_date_in_db and today-1 with 0
238
+ next_day = last_date_in_db + timedelta(days=1)
239
+ while next_day < today_date:
240
+ if next_day not in existing_dates:
241
+ daily_updates.append({"date": next_day, "count": 0})
242
+ next_day += timedelta(days=1)
243
+
244
+ # Add today if not already present
245
+ if today_date not in existing_dates:
246
  daily_updates.append({"date": today_date, "count": 1})
247
+
248
+ # Push updates if any
249
  if daily_updates:
250
  media_clicks_col.update_one(
251
  {"userId": user_oid},
252
+ {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
253
  )
254
+
255
+ # Sort oldest → newest and trim to last 32 entries
256
+ doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
257
+ daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
258
+ daily_entries.sort(key=lambda x: x["date"])
259
+ if len(daily_entries) > 32:
260
+ daily_entries = daily_entries[-32:]
261
+ media_clicks_col.update_one(
262
+ {"userId": user_oid},
263
+ {"$set": {"ai_edit_daily_count": daily_entries}}
264
+ )
265
+
266
  # --------------------------------------------------
267
  # CATEGORY CLICK LOGIC
268
  # --------------------------------------------------