LogicGoInfotechSpaces commited on
Commit
ddd2551
·
verified ·
1 Parent(s): 30fb580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -1014,32 +1014,53 @@ def sync_log_media_click(user_id_str: str, category_id_str: str):
1014
  upsert=True
1015
  )
1016
 
 
1017
  # Fetch current daily count entries
1018
  doc = _media_clicks_col.find_one(
1019
  {"userId": user_oid},
1020
  {"ai_edit_daily_count": 1}
1021
  )
1022
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
1023
-
1024
- is_first_time = len(daily_entries) == 0
1025
- existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
1026
  daily_updates = []
1027
-
1028
- if is_first_time:
1029
  # First ever usage → only today
1030
  daily_updates.append({"date": today_date, "count": 1})
1031
  else:
1032
- # Insert yesterday if missing
1033
- if yesterday_date.date() not in existing_dates:
1034
- daily_updates.append({"date": yesterday_date, "count": 0})
1035
- # Insert today if missing
1036
- if today_date.date() not in existing_dates:
 
 
 
 
 
 
 
 
 
 
1037
  daily_updates.append({"date": today_date, "count": 1})
1038
-
 
1039
  if daily_updates:
1040
  _media_clicks_col.update_one(
1041
  {"userId": user_oid},
1042
- {"$push": {"ai_edit_daily_count": {"$each": daily_updates, "$sort": {"date": 1}}}}
 
 
 
 
 
 
 
 
 
 
 
1043
  )
1044
 
1045
  # --------------------------------------------------
 
1014
  upsert=True
1015
  )
1016
 
1017
+ # Fetch current daily count entries
1018
  # Fetch current daily count entries
1019
  doc = _media_clicks_col.find_one(
1020
  {"userId": user_oid},
1021
  {"ai_edit_daily_count": 1}
1022
  )
1023
  daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
1024
+
 
 
1025
  daily_updates = []
1026
+
1027
+ if not daily_entries:
1028
  # First ever usage → only today
1029
  daily_updates.append({"date": today_date, "count": 1})
1030
  else:
1031
+ # Build existing dates map for quick lookup
1032
+ existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
1033
+
1034
+ # Find last recorded date
1035
+ last_date_in_db = max(entry["date"].date() for entry in daily_entries)
1036
+
1037
+ # Fill missing days between last date and today-1 with 0
1038
+ next_day = last_date_in_db + timedelta(days=1)
1039
+ while next_day < today_date:
1040
+ if next_day not in existing_dates:
1041
+ daily_updates.append({"date": next_day, "count": 0})
1042
+ next_day += timedelta(days=1)
1043
+
1044
+ # Add today if not already present
1045
+ if today_date not in existing_dates:
1046
  daily_updates.append({"date": today_date, "count": 1})
1047
+
1048
+ # Push updates if any
1049
  if daily_updates:
1050
  _media_clicks_col.update_one(
1051
  {"userId": user_oid},
1052
+ {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
1053
+ )
1054
+
1055
+ # Fetch again, sort oldest → newest, and trim to last 32 entries
1056
+ doc = _media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
1057
+ daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
1058
+ daily_entries.sort(key=lambda x: x["date"])
1059
+ if len(daily_entries) > 32:
1060
+ daily_entries = daily_entries[-32:]
1061
+ _media_clicks_col.update_one(
1062
+ {"userId": user_oid},
1063
+ {"$set": {"ai_edit_daily_count": daily_entries}}
1064
  )
1065
 
1066
  # --------------------------------------------------