LogicGoInfotechSpaces commited on
Commit
3e726a0
Β·
verified Β·
1 Parent(s): 744da5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -90
app.py CHANGED
@@ -344,10 +344,6 @@ async def face_swap_api(
344
  },
345
  upsert=True
346
  )
347
-
348
- # -------------------------------------------------
349
- # STEP 2: Handle DAILY USAGE (binary per day)
350
- # -------------------------------------------------
351
  # -------------------------------------------------
352
  # STEP 2: Handle DAILY USAGE (BINARY, NO DUPLICATES)
353
  # -------------------------------------------------
@@ -361,28 +357,27 @@ async def face_swap_api(
361
  # Normalize today to UTC midnight
362
  today_date = datetime(now.year, now.month, now.day)
363
 
364
- # Convert existing entries to map for fast lookup
365
- daily_map = {
366
- entry["date"]: entry["count"]
367
- for entry in daily_entries
368
- }
 
 
369
 
370
- if daily_map:
371
- last_date = max(daily_map.keys())
372
- else:
373
- last_date = today_date
374
 
375
- # Fill missing dates with 0 (from last_date+1 to yesterday)
376
  next_day = last_date + timedelta(days=1)
377
  while next_day < today_date:
378
- if next_day not in daily_map:
379
- daily_map[next_day] = 0
380
  next_day += timedelta(days=1)
381
 
382
- # Mark today as used (binary = 1)
383
  daily_map[today_date] = 1
384
 
385
- # Rebuild sorted list (oldest β†’ newest)
386
  final_daily_entries = [
387
  {"date": d, "count": daily_map[d]}
388
  for d in sorted(daily_map.keys())
@@ -391,7 +386,7 @@ async def face_swap_api(
391
  # Keep only last 32 days
392
  final_daily_entries = final_daily_entries[-32:]
393
 
394
- # Replace array atomically
395
  await media_clicks_col.update_one(
396
  {"userId": user_oid},
397
  {
@@ -402,77 +397,6 @@ async def face_swap_api(
402
  }
403
  )
404
 
405
-
406
-
407
- # doc = await media_clicks_col.find_one(
408
- # {"userId": user_oid},
409
- # {"ai_edit_daily_count": 1}
410
- # )
411
-
412
- # daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
413
- # is_first_time = len(daily_entries) == 0
414
-
415
- # existing_dates = {
416
- # entry["date"].date(): entry["count"]
417
- # for entry in daily_entries
418
- # }
419
-
420
- # daily_updates = []
421
-
422
- # if is_first_time:
423
- # # βœ… FIRST EVER USAGE β†’ only today = 1
424
- # daily_updates.append({
425
- # "date": today_date,
426
- # "count": 1
427
- # })
428
- # else:
429
- # # Insert yesterday if missing
430
- # if yesterday_date.date() not in existing_dates:
431
- # daily_updates.append({
432
- # "date": yesterday_date,
433
- # "count": 0
434
- # })
435
-
436
- # # Insert today if missing
437
- # if today_date.date() not in existing_dates:
438
- # daily_updates.append({
439
- # "date": today_date,
440
- # "count": 1
441
- # })
442
-
443
- # if daily_updates:
444
- # await media_clicks_col.update_one(
445
- # {"userId": user_oid},
446
- # {
447
- # "$push": {
448
- # "ai_edit_daily_count": {
449
- # "$each": daily_updates
450
- # }
451
- # }
452
- # }
453
- # )
454
-
455
- # -------------------------------------------------
456
- # STEP 2.5: Keep ONLY last 32 days of ai_edit_daily_count
457
- # -------------------------------------------------
458
- # doc = await media_clicks_col.find_one(
459
- # {"userId": user_oid},
460
- # {"ai_edit_daily_count": 1}
461
- # )
462
-
463
- # if daily_entries:
464
- # # Sort the array by date ASC (oldest β†’ newest)
465
- # daily_entries.sort(key=lambda x: x["date"])
466
-
467
- # # Keep only last 32 days
468
- # if len(daily_entries) > 32:
469
- # daily_entries = daily_entries[-32:]
470
-
471
- # # Update the DB with sorted & trimmed array
472
- # await media_clicks_col.update_one(
473
- # {"userId": user_oid},
474
- # {"$set": {"ai_edit_daily_count": daily_entries}}
475
- # )
476
  # -------------------------------------------------
477
  # STEP 3: Try updating existing subCategory
478
  # -------------------------------------------------
 
344
  },
345
  upsert=True
346
  )
 
 
 
 
347
  # -------------------------------------------------
348
  # STEP 2: Handle DAILY USAGE (BINARY, NO DUPLICATES)
349
  # -------------------------------------------------
 
357
  # Normalize today to UTC midnight
358
  today_date = datetime(now.year, now.month, now.day)
359
 
360
+ # Build normalized date β†’ count map (THIS ENFORCES UNIQUENESS)
361
+ daily_map = {}
362
+ for entry in daily_entries:
363
+ d = entry["date"]
364
+ if isinstance(d, datetime):
365
+ d = datetime(d.year, d.month, d.day)
366
+ daily_map[d] = entry["count"] # overwrite = no duplicates
367
 
368
+ # Determine last recorded date
369
+ last_date = max(daily_map.keys()) if daily_map else today_date
 
 
370
 
371
+ # Fill ALL missing days with count = 0
372
  next_day = last_date + timedelta(days=1)
373
  while next_day < today_date:
374
+ daily_map.setdefault(next_day, 0)
 
375
  next_day += timedelta(days=1)
376
 
377
+ # Mark today as used (binary)
378
  daily_map[today_date] = 1
379
 
380
+ # Rebuild list: OLDEST β†’ NEWEST
381
  final_daily_entries = [
382
  {"date": d, "count": daily_map[d]}
383
  for d in sorted(daily_map.keys())
 
386
  # Keep only last 32 days
387
  final_daily_entries = final_daily_entries[-32:]
388
 
389
+ # Atomic replace
390
  await media_clicks_col.update_one(
391
  {"userId": user_oid},
392
  {
 
397
  }
398
  )
399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
  # -------------------------------------------------
401
  # STEP 3: Try updating existing subCategory
402
  # -------------------------------------------------