LogicGoInfotechSpaces commited on
Commit
fbce23a
·
verified ·
1 Parent(s): 22c1a80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -54
app.py CHANGED
@@ -970,6 +970,7 @@ def _verify_bearer_token(credentials: Optional[HTTPAuthorizationCredentials] = D
970
  if not _api_bearer_token or token != _api_bearer_token:
971
  raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
972
  return True
 
973
  def sync_log_media_click(user_id_str: str, category_id_str: str):
974
  """
975
  Synchronously logs a click event to the media_clicks collection.
@@ -984,6 +985,10 @@ def sync_log_media_click(user_id_str: str, category_id_str: str):
984
  category_oid = ObjectId(category_id_str.strip())
985
  now = datetime.utcnow()
986
 
 
 
 
 
987
  logger.info(
988
  f"Attempting background write for User:{user_id_str}, Category:{category_id_str}"
989
  )
@@ -994,20 +999,49 @@ def sync_log_media_click(user_id_str: str, category_id_str: str):
994
  _media_clicks_col.update_one(
995
  {"userId": user_oid},
996
  {
 
 
 
 
997
  "$set": {
998
  "ai_edit_last_date": now,
999
  "updatedAt": now
1000
  },
1001
  "$inc": {
1002
  "ai_edit_complete": 1
1003
- },
1004
- "$setOnInsert": {
1005
- "createdAt": now
1006
  }
1007
  },
1008
  upsert=True
1009
  )
1010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1011
  # --------------------------------------------------
1012
  # 2. CATEGORY CLICK LOGIC
1013
  # --------------------------------------------------
@@ -1034,9 +1068,7 @@ def sync_log_media_click(user_id_str: str, category_id_str: str):
1034
  _media_clicks_col.update_one(
1035
  {"userId": user_oid},
1036
  {
1037
- "$set": {
1038
- "updatedAt": now
1039
- },
1040
  "$push": {
1041
  "categories": {
1042
  "categoryId": category_oid,
@@ -1054,63 +1086,95 @@ def sync_log_media_click(user_id_str: str, category_id_str: str):
1054
 
1055
  except Exception as media_err:
1056
  logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
1057
- # swallow error – do not affect main flow
1058
  pass
1059
 
1060
  # def sync_log_media_click(user_id_str: str, category_id_str: str):
1061
- # """
1062
- # Synchronously logs a click event to the media_clicks collection.
1063
- # This function should be run using asyncio.to_thread().
1064
- # """
1065
- # if _media_clicks_col is None:
1066
- # return
1067
-
1068
- # try:
1069
- # user_oid = ObjectId(user_id_str.strip())
1070
- # category_oid = ObjectId(category_id_str.strip())
1071
- # now = datetime.utcnow()
1072
- # logger.info(f"Attempting background write for User:{user_id_str}, Category:{category_id_str}") # <--- NEW LOG
1073
-
1074
- # # 1. Try updating an existing category entry within the user's document
1075
- # update_result = _media_clicks_col.update_one(
1076
- # {
1077
- # "userId": user_oid,
1078
- # "categories.categoryId": category_oid
 
 
 
 
 
 
 
 
1079
  # },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1080
  # {
1081
  # "$set": {
1082
- # "updatedAt": now,
1083
- # "categories.$.lastClickedAt": now
1084
  # },
1085
- # "$inc": {
1086
- # "categories.$.click_count": 1
 
 
 
 
1087
  # }
1088
- # }
 
1089
  # )
1090
-
1091
- # # 2. If no category entry exists (matched_count == 0) -> push a new one (or create user doc)
1092
- # if update_result.matched_count == 0:
1093
- # _media_clicks_col.update_one(
1094
- # { "userId": user_oid },
1095
- # {
1096
- # "$setOnInsert": { "createdAt": now,"ai_edit_complete": 0 },
1097
- # "$set": { "updatedAt": now},
1098
- # "$push": {
1099
- # "categories": {
1100
- # "categoryId": category_oid,
1101
- # "click_count": 1,
1102
- # "lastClickedAt": now
1103
- # }
1104
- # }
1105
- # },
1106
- # upsert=True
1107
- # )
1108
-
1109
- # logger.info(f"Media click logged for User {user_id_str} on Category {category_id_str}")
1110
-
1111
- # except Exception as media_err:
1112
- # logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
1113
- # pass # Handle logging error gracefully
1114
 
1115
  DEFAULT_FACE_MODEL = 'GFPGANv1.4.pth'
1116
  DEFAULT_UPSCALE_MODEL = 'SRVGG, realesr-general-x4v3.pth'
 
970
  if not _api_bearer_token or token != _api_bearer_token:
971
  raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
972
  return True
973
+
974
  def sync_log_media_click(user_id_str: str, category_id_str: str):
975
  """
976
  Synchronously logs a click event to the media_clicks collection.
 
985
  category_oid = ObjectId(category_id_str.strip())
986
  now = datetime.utcnow()
987
 
988
+ # Normalize dates (UTC midnight)
989
+ today_date = datetime(now.year, now.month, now.day)
990
+ yesterday_date = today_date - timedelta(days=1)
991
+
992
  logger.info(
993
  f"Attempting background write for User:{user_id_str}, Category:{category_id_str}"
994
  )
 
999
  _media_clicks_col.update_one(
1000
  {"userId": user_oid},
1001
  {
1002
+ "$setOnInsert": {
1003
+ "createdAt": now,
1004
+ "ai_edit_daily_count": []
1005
+ },
1006
  "$set": {
1007
  "ai_edit_last_date": now,
1008
  "updatedAt": now
1009
  },
1010
  "$inc": {
1011
  "ai_edit_complete": 1
 
 
 
1012
  }
1013
  },
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}}}
1043
+ )
1044
+
1045
  # --------------------------------------------------
1046
  # 2. CATEGORY CLICK LOGIC
1047
  # --------------------------------------------------
 
1068
  _media_clicks_col.update_one(
1069
  {"userId": user_oid},
1070
  {
1071
+ "$set": {"updatedAt": now},
 
 
1072
  "$push": {
1073
  "categories": {
1074
  "categoryId": category_oid,
 
1086
 
1087
  except Exception as media_err:
1088
  logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
 
1089
  pass
1090
 
1091
  # def sync_log_media_click(user_id_str: str, category_id_str: str):
1092
+ # """
1093
+ # Synchronously logs a click event to the media_clicks collection.
1094
+ # This function should be run using asyncio.to_thread().
1095
+ # """
1096
+
1097
+ # if _media_clicks_col is None:
1098
+ # return
1099
+
1100
+ # try:
1101
+ # user_oid = ObjectId(user_id_str.strip())
1102
+ # category_oid = ObjectId(category_id_str.strip())
1103
+ # now = datetime.utcnow()
1104
+
1105
+ # logger.info(
1106
+ # f"Attempting background write for User:{user_id_str}, Category:{category_id_str}"
1107
+ # )
1108
+
1109
+ # # --------------------------------------------------
1110
+ # # 1. AI EDIT USAGE TRACKING (GLOBAL PER USER)
1111
+ # # --------------------------------------------------
1112
+ # _media_clicks_col.update_one(
1113
+ # {"userId": user_oid},
1114
+ # {
1115
+ # "$set": {
1116
+ # "ai_edit_last_date": now,
1117
+ # "updatedAt": now
1118
  # },
1119
+ # "$inc": {
1120
+ # "ai_edit_complete": 1
1121
+ # },
1122
+ # "$setOnInsert": {
1123
+ # "createdAt": now
1124
+ # }
1125
+ # },
1126
+ # upsert=True
1127
+ # )
1128
+
1129
+ # # --------------------------------------------------
1130
+ # # 2. CATEGORY CLICK LOGIC
1131
+ # # --------------------------------------------------
1132
+ # update_result = _media_clicks_col.update_one(
1133
+ # {
1134
+ # "userId": user_oid,
1135
+ # "categories.categoryId": category_oid
1136
+ # },
1137
+ # {
1138
+ # "$set": {
1139
+ # "updatedAt": now,
1140
+ # "categories.$.lastClickedAt": now
1141
+ # },
1142
+ # "$inc": {
1143
+ # "categories.$.click_count": 1
1144
+ # }
1145
+ # }
1146
+ # )
1147
+
1148
+ # # --------------------------------------------------
1149
+ # # 3. IF CATEGORY DOES NOT EXIST → PUSH NEW
1150
+ # # --------------------------------------------------
1151
+ # if update_result.matched_count == 0:
1152
+ # _media_clicks_col.update_one(
1153
+ # {"userId": user_oid},
1154
  # {
1155
  # "$set": {
1156
+ # "updatedAt": now
 
1157
  # },
1158
+ # "$push": {
1159
+ # "categories": {
1160
+ # "categoryId": category_oid,
1161
+ # "click_count": 1,
1162
+ # "lastClickedAt": now
1163
+ # }
1164
  # }
1165
+ # },
1166
+ # upsert=True
1167
  # )
1168
+
1169
+ # logger.info(
1170
+ # f"Media click logged for User {user_id_str} on Category {category_id_str}"
1171
+ # )
1172
+
1173
+ # except Exception as media_err:
1174
+ # logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
1175
+ # # swallow error – do not affect main flow
1176
+ # pass
1177
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1178
 
1179
  DEFAULT_FACE_MODEL = 'GFPGANv1.4.pth'
1180
  DEFAULT_UPSCALE_MODEL = 'SRVGG, realesr-general-x4v3.pth'