LogicGoInfotechSpaces commited on
Commit
28443f0
Β·
verified Β·
1 Parent(s): 8bc82db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -51
app.py CHANGED
@@ -345,6 +345,9 @@ async def face_swap_api(
345
  upsert=True
346
  )
347
 
 
 
 
348
  # -------------------------------------------------
349
  # STEP 2: Handle DAILY USAGE (binary per day)
350
  # -------------------------------------------------
@@ -352,71 +355,112 @@ async def face_swap_api(
352
  {"userId": user_oid},
353
  {"ai_edit_daily_count": 1}
354
  )
355
-
356
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
357
- is_first_time = len(daily_entries) == 0
358
-
359
- existing_dates = {
360
- entry["date"].date(): entry["count"]
361
- for entry in daily_entries
362
- }
363
-
364
  daily_updates = []
365
-
366
- if is_first_time:
367
- # βœ… FIRST EVER USAGE β†’ only today = 1
368
- daily_updates.append({
369
- "date": today_date,
370
- "count": 1
371
- })
372
  else:
373
- # Insert yesterday if missing
374
- if yesterday_date.date() not in existing_dates:
375
- daily_updates.append({
376
- "date": yesterday_date,
377
- "count": 0
378
- })
379
-
380
- # Insert today if missing
381
- if today_date.date() not in existing_dates:
382
- daily_updates.append({
383
- "date": today_date,
384
- "count": 1
385
- })
386
-
387
  if daily_updates:
388
  await media_clicks_col.update_one(
389
  {"userId": user_oid},
390
- {
391
- "$push": {
392
- "ai_edit_daily_count": {
393
- "$each": daily_updates
394
- }
395
- }
396
- }
397
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
 
399
  # -------------------------------------------------
400
  # STEP 2.5: Keep ONLY last 32 days of ai_edit_daily_count
401
  # -------------------------------------------------
402
- doc = await media_clicks_col.find_one(
403
- {"userId": user_oid},
404
- {"ai_edit_daily_count": 1}
405
- )
406
 
407
- if daily_entries:
408
- # Sort the array by date ASC (oldest β†’ newest)
409
- daily_entries.sort(key=lambda x: x["date"])
410
 
411
- # Keep only last 32 days
412
- if len(daily_entries) > 32:
413
- daily_entries = daily_entries[-32:]
414
 
415
- # Update the DB with sorted & trimmed array
416
- await media_clicks_col.update_one(
417
- {"userId": user_oid},
418
- {"$set": {"ai_edit_daily_count": daily_entries}}
419
- )
420
  # -------------------------------------------------
421
  # STEP 3: Try updating existing subCategory
422
  # -------------------------------------------------
 
345
  upsert=True
346
  )
347
 
348
+ # -------------------------------------------------
349
+ # STEP 2: Handle DAILY USAGE (binary per day)
350
+ # -------------------------------------------------
351
  # -------------------------------------------------
352
  # STEP 2: Handle DAILY USAGE (binary per day)
353
  # -------------------------------------------------
 
355
  {"userId": user_oid},
356
  {"ai_edit_daily_count": 1}
357
  )
 
358
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
359
+
 
 
 
 
 
 
360
  daily_updates = []
361
+
362
+ if not daily_entries:
363
+ # FIRST EVER USAGE β†’ today = 1
364
+ daily_updates.append({"date": today_date, "count": 1})
 
 
 
365
  else:
366
+ # Find the latest date in the array
367
+ last_date_in_db = max(entry["date"] for entry in daily_entries)
368
+ next_day = last_date_in_db + timedelta(days=1)
369
+
370
+ # Fill missing days with count = 0 up to yesterday
371
+ while next_day < today_date:
372
+ daily_updates.append({"date": next_day, "count": 0})
373
+ next_day += timedelta(days=1)
374
+
375
+ # Today = 1
376
+ daily_updates.append({"date": today_date, "count": 1})
377
+
 
 
378
  if daily_updates:
379
  await media_clicks_col.update_one(
380
  {"userId": user_oid},
381
+ {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
 
 
 
 
 
 
382
  )
383
+
384
+ # After this, optionally sort & trim to last 32 days
385
+ doc = await media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
386
+ daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
387
+ daily_entries.sort(key=lambda x: x["date"])
388
+ if len(daily_entries) > 32:
389
+ daily_entries = daily_entries[-32:]
390
+ await media_clicks_col.update_one(
391
+ {"userId": user_oid},
392
+ {"$set": {"ai_edit_daily_count": daily_entries}}
393
+ )
394
+
395
+ # doc = await media_clicks_col.find_one(
396
+ # {"userId": user_oid},
397
+ # {"ai_edit_daily_count": 1}
398
+ # )
399
+
400
+ # daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
401
+ # is_first_time = len(daily_entries) == 0
402
+
403
+ # existing_dates = {
404
+ # entry["date"].date(): entry["count"]
405
+ # for entry in daily_entries
406
+ # }
407
+
408
+ # daily_updates = []
409
+
410
+ # if is_first_time:
411
+ # # βœ… FIRST EVER USAGE β†’ only today = 1
412
+ # daily_updates.append({
413
+ # "date": today_date,
414
+ # "count": 1
415
+ # })
416
+ # else:
417
+ # # Insert yesterday if missing
418
+ # if yesterday_date.date() not in existing_dates:
419
+ # daily_updates.append({
420
+ # "date": yesterday_date,
421
+ # "count": 0
422
+ # })
423
+
424
+ # # Insert today if missing
425
+ # if today_date.date() not in existing_dates:
426
+ # daily_updates.append({
427
+ # "date": today_date,
428
+ # "count": 1
429
+ # })
430
+
431
+ # if daily_updates:
432
+ # await media_clicks_col.update_one(
433
+ # {"userId": user_oid},
434
+ # {
435
+ # "$push": {
436
+ # "ai_edit_daily_count": {
437
+ # "$each": daily_updates
438
+ # }
439
+ # }
440
+ # }
441
+ # )
442
 
443
  # -------------------------------------------------
444
  # STEP 2.5: Keep ONLY last 32 days of ai_edit_daily_count
445
  # -------------------------------------------------
446
+ # doc = await media_clicks_col.find_one(
447
+ # {"userId": user_oid},
448
+ # {"ai_edit_daily_count": 1}
449
+ # )
450
 
451
+ # if daily_entries:
452
+ # # Sort the array by date ASC (oldest β†’ newest)
453
+ # daily_entries.sort(key=lambda x: x["date"])
454
 
455
+ # # Keep only last 32 days
456
+ # if len(daily_entries) > 32:
457
+ # daily_entries = daily_entries[-32:]
458
 
459
+ # # Update the DB with sorted & trimmed array
460
+ # await media_clicks_col.update_one(
461
+ # {"userId": user_oid},
462
+ # {"$set": {"ai_edit_daily_count": daily_entries}}
463
+ # )
464
  # -------------------------------------------------
465
  # STEP 3: Try updating existing subCategory
466
  # -------------------------------------------------