LogicGoInfotechSpaces commited on
Commit
e595815
·
verified ·
1 Parent(s): 1c6fc8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -24
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import io
3
  import json
4
  import traceback
5
- from datetime import datetime
6
  from typing import Optional
7
  import time
8
  from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request, Depends
@@ -173,12 +173,8 @@ async def generate(
173
  combined_img = pil_img1
174
 
175
  # -------------------------
176
- # 3. OPTIONAL CATEGORY CLICK LOGIC
177
  # -------------------------
178
- # -------------------------
179
- # 3. OPTIONAL CATEGORY CLICK LOGIC
180
- # -------------------------
181
-
182
  if user_id and category_id:
183
  try:
184
  admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
@@ -198,26 +194,58 @@ async def generate(
198
 
199
  now = datetime.utcnow()
200
 
 
 
 
 
201
  # --------------------------------------------------
202
  # AI EDIT USAGE TRACKING (GLOBAL PER USER)
203
  # --------------------------------------------------
204
  media_clicks_col.update_one(
205
  {"userId": user_oid},
206
  {
 
 
 
 
207
  "$set": {
208
  "ai_edit_last_date": now,
209
  "updatedAt": now
210
  },
211
  "$inc": {
212
  "ai_edit_complete": 1
213
- },
214
- "$setOnInsert": {
215
- "createdAt": now
216
  }
217
  },
218
  upsert=True
219
  )
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  # --------------------------------------------------
222
  # CATEGORY CLICK LOGIC
223
  # --------------------------------------------------
@@ -253,29 +281,49 @@ async def generate(
253
 
254
  except Exception as e:
255
  print("CATEGORY_LOG_ERROR:", e)
256
- # DO NOT break image generation
257
-
258
 
259
  # if user_id and category_id:
260
  # try:
261
  # admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
262
  # admin_db = admin_client["adminPanel"]
263
-
264
  # categories_col = admin_db.categories
265
  # media_clicks_col = admin_db.media_clicks
266
-
267
  # # Validate user_oid & category_oid
268
  # user_oid = ObjectId(user_id)
269
  # category_oid = ObjectId(category_id)
270
-
271
  # # Check category exists
272
  # category_doc = categories_col.find_one({"_id": category_oid})
273
  # if not category_doc:
274
  # raise HTTPException(400, f"Invalid category_id: {category_id}")
275
-
276
  # now = datetime.utcnow()
277
-
278
- # # Try updating existing category click
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  # update_res = media_clicks_col.update_one(
280
  # {"userId": user_oid, "categories.categoryId": category_oid},
281
  # {
@@ -283,16 +331,17 @@ async def generate(
283
  # "updatedAt": now,
284
  # "categories.$.lastClickedAt": now
285
  # },
286
- # "$inc": {"categories.$.click_count": 1}
 
 
287
  # }
288
  # )
289
-
290
- # # If user or category does not exist → create/push new
291
  # if update_res.matched_count == 0:
292
  # media_clicks_col.update_one(
293
  # {"userId": user_oid},
294
  # {
295
- # "$setOnInsert": {"createdAt": now},
296
  # "$set": {"updatedAt": now},
297
  # "$push": {
298
  # "categories": {
@@ -304,11 +353,9 @@ async def generate(
304
  # },
305
  # upsert=True
306
  # )
307
-
308
  # except Exception as e:
309
  # print("CATEGORY_LOG_ERROR:", e)
310
- # but do NOT stop the image generation
311
-
312
  # -------------------------
313
  # 4. HF INFERENCE
314
  # -------------------------
 
2
  import io
3
  import json
4
  import traceback
5
+ from datetime import datetime,timedelta
6
  from typing import Optional
7
  import time
8
  from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request, Depends
 
173
  combined_img = pil_img1
174
 
175
  # -------------------------
176
+ # 3. CATEGORY CLICK LOGIC
177
  # -------------------------
 
 
 
 
178
  if user_id and category_id:
179
  try:
180
  admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
 
194
 
195
  now = datetime.utcnow()
196
 
197
+ # Normalize dates (UTC midnight)
198
+ today_date = datetime(now.year, now.month, now.day)
199
+ yesterday_date = today_date - timedelta(days=1)
200
+
201
  # --------------------------------------------------
202
  # AI EDIT USAGE TRACKING (GLOBAL PER USER)
203
  # --------------------------------------------------
204
  media_clicks_col.update_one(
205
  {"userId": user_oid},
206
  {
207
+ "$setOnInsert": {
208
+ "createdAt": now,
209
+ "ai_edit_daily_count": []
210
+ },
211
  "$set": {
212
  "ai_edit_last_date": now,
213
  "updatedAt": now
214
  },
215
  "$inc": {
216
  "ai_edit_complete": 1
 
 
 
217
  }
218
  },
219
  upsert=True
220
  )
221
 
222
+ # --------------------------------------------------
223
+ # DAILY COUNT LOGIC
224
+ # --------------------------------------------------
225
+ doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
226
+ daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
227
+
228
+ is_first_time = len(daily_entries) == 0
229
+ existing_dates = {entry["date"].date(): entry["count"] for entry in daily_entries}
230
+ daily_updates = []
231
+
232
+ if is_first_time:
233
+ # First-ever usage → only today
234
+ daily_updates.append({"date": today_date, "count": 1})
235
+ else:
236
+ # Backfill yesterday if missing
237
+ if yesterday_date.date() not in existing_dates:
238
+ daily_updates.append({"date": yesterday_date, "count": 0})
239
+ # Insert today if missing
240
+ if today_date.date() not in existing_dates:
241
+ daily_updates.append({"date": today_date, "count": 1})
242
+
243
+ if daily_updates:
244
+ media_clicks_col.update_one(
245
+ {"userId": user_oid},
246
+ {"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
247
+ )
248
+
249
  # --------------------------------------------------
250
  # CATEGORY CLICK LOGIC
251
  # --------------------------------------------------
 
281
 
282
  except Exception as e:
283
  print("CATEGORY_LOG_ERROR:", e)
 
 
284
 
285
  # if user_id and category_id:
286
  # try:
287
  # admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
288
  # admin_db = admin_client["adminPanel"]
289
+
290
  # categories_col = admin_db.categories
291
  # media_clicks_col = admin_db.media_clicks
292
+
293
  # # Validate user_oid & category_oid
294
  # user_oid = ObjectId(user_id)
295
  # category_oid = ObjectId(category_id)
296
+
297
  # # Check category exists
298
  # category_doc = categories_col.find_one({"_id": category_oid})
299
  # if not category_doc:
300
  # raise HTTPException(400, f"Invalid category_id: {category_id}")
301
+
302
  # now = datetime.utcnow()
303
+
304
+ # # --------------------------------------------------
305
+ # # AI EDIT USAGE TRACKING (GLOBAL PER USER)
306
+ # # --------------------------------------------------
307
+ # media_clicks_col.update_one(
308
+ # {"userId": user_oid},
309
+ # {
310
+ # "$set": {
311
+ # "ai_edit_last_date": now,
312
+ # "updatedAt": now
313
+ # },
314
+ # "$inc": {
315
+ # "ai_edit_complete": 1
316
+ # },
317
+ # "$setOnInsert": {
318
+ # "createdAt": now
319
+ # }
320
+ # },
321
+ # upsert=True
322
+ # )
323
+
324
+ # # --------------------------------------------------
325
+ # # CATEGORY CLICK LOGIC
326
+ # # --------------------------------------------------
327
  # update_res = media_clicks_col.update_one(
328
  # {"userId": user_oid, "categories.categoryId": category_oid},
329
  # {
 
331
  # "updatedAt": now,
332
  # "categories.$.lastClickedAt": now
333
  # },
334
+ # "$inc": {
335
+ # "categories.$.click_count": 1
336
+ # }
337
  # }
338
  # )
339
+
340
+ # # If category does not exist → push new
341
  # if update_res.matched_count == 0:
342
  # media_clicks_col.update_one(
343
  # {"userId": user_oid},
344
  # {
 
345
  # "$set": {"updatedAt": now},
346
  # "$push": {
347
  # "categories": {
 
353
  # },
354
  # upsert=True
355
  # )
356
+
357
  # except Exception as e:
358
  # print("CATEGORY_LOG_ERROR:", e)
 
 
359
  # -------------------------
360
  # 4. HF INFERENCE
361
  # -------------------------