LogicGoInfotechSpaces commited on
Commit
b22b681
·
verified ·
1 Parent(s): 31e6847

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -108
app.py CHANGED
@@ -231,7 +231,7 @@ async def generate(
231
  # DAILY COUNT LOGIC
232
  # --------------------------------------------------
233
  now = datetime.utcnow()
234
- today_date = now.date()
235
 
236
  doc = media_clicks_col.find_one(
237
  {"userId": user_oid},
@@ -239,47 +239,45 @@ async def generate(
239
  )
240
 
241
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
242
- daily_updates = []
243
-
244
- if not daily_entries:
245
- daily_updates.append({"date": today_date, "count": 1})
246
- else:
247
- # Normalize DB dates (THIS FIXES THE ERROR)
248
- existing_dates = {
249
- (entry["date"].date() if isinstance(entry["date"], datetime) else entry["date"]): entry["count"]
250
- for entry in daily_entries
251
- }
252
 
253
- last_date_in_db = max(existing_dates.keys())
 
 
 
 
 
254
 
255
- next_day = last_date_in_db + timedelta(days=1)
256
- while next_day < today_date:
257
- if next_day not in existing_dates:
258
- daily_updates.append({"date": next_day, "count": 0})
259
- next_day += timedelta(days=1)
260
 
261
- if today_date not in existing_dates:
262
- daily_updates.append({"date": today_date, "count": 1})
 
 
 
263
 
264
- if daily_updates:
265
- media_clicks_col.update_one(
266
- {"userId": user_oid},
267
- {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
268
- )
269
 
270
- # Sort + trim
271
- doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
272
- daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
273
- daily_entries.sort(
274
- key=lambda x: x["date"].date() if isinstance(x["date"], datetime) else x["date"]
275
- )
276
 
277
- if len(daily_entries) > 32:
278
- daily_entries = daily_entries[-32:]
279
 
 
280
  media_clicks_col.update_one(
281
  {"userId": user_oid},
282
- {"$set": {"ai_edit_daily_count": daily_entries}}
 
 
 
 
 
 
283
  )
284
 
285
 
@@ -318,81 +316,6 @@ async def generate(
318
 
319
  except Exception as e:
320
  print("CATEGORY_LOG_ERROR:", e)
321
-
322
- # if user_id and category_id:
323
- # try:
324
- # admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
325
- # admin_db = admin_client["adminPanel"]
326
-
327
- # categories_col = admin_db.categories
328
- # media_clicks_col = admin_db.media_clicks
329
-
330
- # # Validate user_oid & category_oid
331
- # user_oid = ObjectId(user_id)
332
- # category_oid = ObjectId(category_id)
333
-
334
- # # Check category exists
335
- # category_doc = categories_col.find_one({"_id": category_oid})
336
- # if not category_doc:
337
- # raise HTTPException(400, f"Invalid category_id: {category_id}")
338
-
339
- # now = datetime.utcnow()
340
-
341
- # # --------------------------------------------------
342
- # # AI EDIT USAGE TRACKING (GLOBAL PER USER)
343
- # # --------------------------------------------------
344
- # media_clicks_col.update_one(
345
- # {"userId": user_oid},
346
- # {
347
- # "$set": {
348
- # "ai_edit_last_date": now,
349
- # "updatedAt": now
350
- # },
351
- # "$inc": {
352
- # "ai_edit_complete": 1
353
- # },
354
- # "$setOnInsert": {
355
- # "createdAt": now
356
- # }
357
- # },
358
- # upsert=True
359
- # )
360
-
361
- # # --------------------------------------------------
362
- # # CATEGORY CLICK LOGIC
363
- # # --------------------------------------------------
364
- # update_res = media_clicks_col.update_one(
365
- # {"userId": user_oid, "categories.categoryId": category_oid},
366
- # {
367
- # "$set": {
368
- # "updatedAt": now,
369
- # "categories.$.lastClickedAt": now
370
- # },
371
- # "$inc": {
372
- # "categories.$.click_count": 1
373
- # }
374
- # }
375
- # )
376
-
377
- # # If category does not exist → push new
378
- # if update_res.matched_count == 0:
379
- # media_clicks_col.update_one(
380
- # {"userId": user_oid},
381
- # {
382
- # "$set": {"updatedAt": now},
383
- # "$push": {
384
- # "categories": {
385
- # "categoryId": category_oid,
386
- # "click_count": 1,
387
- # "lastClickedAt": now
388
- # }
389
- # }
390
- # },
391
- # upsert=True
392
- # )
393
-
394
- # except Exception as e:
395
- # print("CATEGORY_LOG_ERROR:", e)
396
  # -------------------------
397
  # 4. HF INFERENCE
398
  # -------------------------
 
231
  # DAILY COUNT LOGIC
232
  # --------------------------------------------------
233
  now = datetime.utcnow()
234
+ today_date = datetime(now.year, now.month, now.day)
235
 
236
  doc = media_clicks_col.find_one(
237
  {"userId": user_oid},
 
239
  )
240
 
241
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
 
 
 
 
 
 
 
 
 
 
242
 
243
+ # Build UNIQUE date -> count map
244
+ daily_map = {}
245
+ for entry in daily_entries:
246
+ d = entry["date"]
247
+ d = datetime(d.year, d.month, d.day) if isinstance(d, datetime) else d
248
+ daily_map[d] = entry["count"] # overwrite = no duplicates
249
 
250
+ # Find last known date
251
+ last_date = max(daily_map.keys()) if daily_map else today_date
 
 
 
252
 
253
+ # Fill ALL missing days with 0
254
+ next_day = last_date + timedelta(days=1)
255
+ while next_day < today_date:
256
+ daily_map.setdefault(next_day, 0)
257
+ next_day += timedelta(days=1)
258
 
259
+ # Mark today as used (binary)
260
+ daily_map[today_date] = 1
 
 
 
261
 
262
+ # Rebuild list (OLD → NEW)
263
+ final_daily_entries = [
264
+ {"date": d, "count": daily_map[d]}
265
+ for d in sorted(daily_map.keys())
266
+ ]
 
267
 
268
+ # Keep last 32 days only
269
+ final_daily_entries = final_daily_entries[-32:]
270
 
271
+ # ATOMIC REPLACE (NO PUSH)
272
  media_clicks_col.update_one(
273
  {"userId": user_oid},
274
+ {
275
+ "$set": {
276
+ "ai_edit_daily_count": final_daily_entries,
277
+ "ai_edit_last_date": now,
278
+ "updatedAt": now
279
+ }
280
+ }
281
  )
282
 
283
 
 
316
 
317
  except Exception as e:
318
  print("CATEGORY_LOG_ERROR:", e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  # -------------------------
320
  # 4. HF INFERENCE
321
  # -------------------------