LogicGoInfotechSpaces commited on
Commit
744da5a
·
verified ·
1 Parent(s): ed59e54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -31
app.py CHANGED
@@ -349,56 +349,61 @@ async def face_swap_api(
349
  # STEP 2: Handle DAILY USAGE (binary per day)
350
  # -------------------------------------------------
351
  # -------------------------------------------------
352
- # STEP 2: Handle DAILY USAGE (binary per day)
353
  # -------------------------------------------------
354
  doc = await media_clicks_col.find_one(
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 → only today = 1 (no previous 0s)
364
- daily_updates.append({"date": today_date, "count": 1})
365
  else:
366
- # Build a set of existing dates to avoid duplicates
367
- existing_dates = {entry["date"] for entry in daily_entries}
368
 
369
- # Find the last recorded date
370
- last_date_in_db = max(entry["date"] for entry in daily_entries)
 
 
 
 
371
 
372
- # Fill missing days between last date and yesterday with count=0
373
- next_day = last_date_in_db + timedelta(days=1)
374
- while next_day < today_date:
375
- if next_day not in existing_dates:
376
- daily_updates.append({"date": next_day, "count": 0})
377
- next_day += timedelta(days=1)
378
 
379
- # Add today if not already present
380
- if today_date not in existing_dates:
381
- daily_updates.append({"date": today_date, "count": 1})
 
 
382
 
383
- # Push updates if any
384
- if daily_updates:
385
- await media_clicks_col.update_one(
386
- {"userId": user_oid},
387
- {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
388
- )
389
 
390
- # Sort and trim to last 32 days
391
- doc = await media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
392
- daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
393
- daily_entries.sort(key=lambda x: x["date"])
394
- if len(daily_entries) > 32:
395
- daily_entries = daily_entries[-32:]
396
  await media_clicks_col.update_one(
397
  {"userId": user_oid},
398
- {"$set": {"ai_edit_daily_count": daily_entries}}
 
 
 
 
 
399
  )
400
 
401
 
 
402
  # doc = await media_clicks_col.find_one(
403
  # {"userId": user_oid},
404
  # {"ai_edit_daily_count": 1}
 
349
  # STEP 2: Handle DAILY USAGE (binary per day)
350
  # -------------------------------------------------
351
  # -------------------------------------------------
352
+ # STEP 2: Handle DAILY USAGE (BINARY, NO DUPLICATES)
353
  # -------------------------------------------------
354
  doc = await media_clicks_col.find_one(
355
  {"userId": user_oid},
356
  {"ai_edit_daily_count": 1}
357
  )
358
+
359
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
360
 
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())
389
+ ]
390
 
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
+ {
398
+ "$set": {
399
+ "ai_edit_daily_count": final_daily_entries,
400
+ "updatedAt": now
401
+ }
402
+ }
403
  )
404
 
405
 
406
+
407
  # doc = await media_clicks_col.find_one(
408
  # {"userId": user_oid},
409
  # {"ai_edit_daily_count": 1}