Commit
·
91647c6
1
Parent(s):
f1019bd
Remove all excessive debug logs from check_user_has_category_data
Browse files- app/smart_recommendation.py +35 -153
app/smart_recommendation.py
CHANGED
|
@@ -267,7 +267,7 @@ class SmartBudgetRecommender:
|
|
| 267 |
except Exception:
|
| 268 |
continue
|
| 269 |
|
| 270 |
-
# THIRD:
|
| 271 |
try:
|
| 272 |
comprehensive_query = {
|
| 273 |
"$and": [
|
|
@@ -275,167 +275,49 @@ class SmartBudgetRecommender:
|
|
| 275 |
{"$or": category_conditions}
|
| 276 |
]
|
| 277 |
}
|
| 278 |
-
|
| 279 |
-
if budget_count > 0:
|
| 280 |
-
print(f"✅ Found {budget_count} budget(s) using comprehensive query for user {user_id} with category_id {category_id}")
|
| 281 |
return True
|
| 282 |
-
except Exception
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
# Debug: Check if user has ANY budgets at all and what categories they have
|
| 286 |
-
try:
|
| 287 |
-
user_objid = ObjectId(user_id)
|
| 288 |
-
total_budgets = self.db.budgets.count_documents({"$or": [{"createdBy": user_objid}, {"createdBy": user_id}, {"user_id": user_objid}, {"user_id": user_id}]})
|
| 289 |
-
print(f"🔍 Debug: User {user_id} has {total_budgets} total budgets")
|
| 290 |
-
|
| 291 |
-
# Get all budgets and check their category structure
|
| 292 |
-
all_budgets = list(self.db.budgets.find({"$or": [{"createdBy": user_objid}, {"createdBy": user_id}]}).limit(10))
|
| 293 |
-
if all_budgets:
|
| 294 |
-
print(f"🔍 Debug: Analyzing {len(all_budgets)} budgets to find category structure:")
|
| 295 |
-
category_ids_found = set()
|
| 296 |
-
for i, budget in enumerate(all_budgets[:5], 1): # Show first 5
|
| 297 |
-
cat_id = budget.get('category') or budget.get('categoryId') or budget.get('headCategory')
|
| 298 |
-
head_cats = budget.get('headCategories', [])
|
| 299 |
-
print(f" Budget {i}:")
|
| 300 |
-
print(f" - category: {budget.get('category')}")
|
| 301 |
-
print(f" - categoryId: {budget.get('categoryId')}")
|
| 302 |
-
print(f" - headCategory: {budget.get('headCategory')}")
|
| 303 |
-
print(f" - headCategories: {head_cats}")
|
| 304 |
-
|
| 305 |
-
# Check if requested category_id is in headCategories
|
| 306 |
-
if head_cats and isinstance(head_cats, list):
|
| 307 |
-
for hc in head_cats:
|
| 308 |
-
if isinstance(hc, dict):
|
| 309 |
-
hc_id = hc.get('headCategory')
|
| 310 |
-
if hc_id:
|
| 311 |
-
cat_id_str = str(hc_id)
|
| 312 |
-
category_ids_found.add(cat_id_str)
|
| 313 |
-
if cat_id_str == str(category_id):
|
| 314 |
-
print(f" ✅ MATCH FOUND: category_id {category_id} found in headCategories.headCategory!")
|
| 315 |
-
|
| 316 |
-
# Check nested categories
|
| 317 |
-
nested_cats = hc.get('categories', [])
|
| 318 |
-
if nested_cats:
|
| 319 |
-
for nc in nested_cats:
|
| 320 |
-
if isinstance(nc, dict):
|
| 321 |
-
nc_id = nc.get('category')
|
| 322 |
-
if nc_id:
|
| 323 |
-
nc_id_str = str(nc_id)
|
| 324 |
-
category_ids_found.add(nc_id_str)
|
| 325 |
-
if nc_id_str == str(category_id):
|
| 326 |
-
print(f" ✅ MATCH FOUND: category_id {category_id} found in headCategories.categories.category!")
|
| 327 |
-
|
| 328 |
-
if cat_id:
|
| 329 |
-
cat_id_str = str(cat_id)
|
| 330 |
-
category_ids_found.add(cat_id_str)
|
| 331 |
-
if cat_id_str == str(category_id):
|
| 332 |
-
print(f" ✅ MATCH FOUND: category_id {category_id} found in direct category field!")
|
| 333 |
-
|
| 334 |
-
print(f"🔍 Debug: Unique category_ids found in user's budgets: {list(category_ids_found)}")
|
| 335 |
-
print(f"🔍 Debug: Looking for category_id: {category_id}")
|
| 336 |
-
if str(category_id) not in category_ids_found:
|
| 337 |
-
print(f" ❌ Requested category_id {category_id} NOT found in any of the user's budgets")
|
| 338 |
-
else:
|
| 339 |
-
print(f" ✅ Requested category_id {category_id} IS in user's budgets but query didn't match!")
|
| 340 |
-
except Exception as e:
|
| 341 |
-
print(f"⚠️ Error in debug check: {e}")
|
| 342 |
-
import traceback
|
| 343 |
-
traceback.print_exc()
|
| 344 |
|
| 345 |
-
# Check expenses collection as fallback
|
| 346 |
-
print(f"🔍 Checking expenses collection for user {user_id} with category_id {category_id}")
|
| 347 |
try:
|
| 348 |
try:
|
| 349 |
category_objid = ObjectId(category_id)
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
{"user_id":
|
| 353 |
-
{"
|
| 354 |
-
{"
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
{"
|
| 358 |
-
{"
|
| 359 |
-
{"
|
| 360 |
-
{"
|
| 361 |
-
{"
|
| 362 |
-
{"
|
| 363 |
-
{"user_id": user_id, "headCategory": category_id},
|
| 364 |
]
|
| 365 |
except (ValueError, TypeError):
|
| 366 |
-
|
| 367 |
-
{"user_id":
|
| 368 |
-
{"
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
{"
|
| 372 |
-
{"
|
|
|
|
| 373 |
]
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
print(f"⚠️ Error with expense query pattern {i}: {e}")
|
| 385 |
-
|
| 386 |
-
# Also try comprehensive query
|
| 387 |
-
try:
|
| 388 |
-
comprehensive_expense_query = {
|
| 389 |
-
"$and": [
|
| 390 |
-
{"$or": [
|
| 391 |
-
{"user_id": ObjectId(user_id)},
|
| 392 |
-
{"user_id": user_id}
|
| 393 |
-
]},
|
| 394 |
-
{"$or": [
|
| 395 |
-
{"category": category_objid if 'category_objid' in locals() else category_id},
|
| 396 |
-
{"category": category_id},
|
| 397 |
-
{"categoryId": category_objid if 'category_objid' in locals() else category_id},
|
| 398 |
-
{"categoryId": category_id},
|
| 399 |
-
{"headCategory": category_objid if 'category_objid' in locals() else category_id},
|
| 400 |
-
{"headCategory": category_id},
|
| 401 |
-
]}
|
| 402 |
-
]
|
| 403 |
-
}
|
| 404 |
-
expense_count = self.db.expenses.count_documents(comprehensive_expense_query)
|
| 405 |
-
if expense_count > 0:
|
| 406 |
-
print(f"✅ Found {expense_count} expense(s) using comprehensive query for user {user_id} with category_id {category_id}")
|
| 407 |
-
return True
|
| 408 |
-
except Exception as e:
|
| 409 |
-
print(f"⚠️ Error with comprehensive expense query: {e}")
|
| 410 |
-
|
| 411 |
-
# Debug: Check total expenses for this user
|
| 412 |
-
try:
|
| 413 |
-
total_user_expenses = self.db.expenses.count_documents({
|
| 414 |
-
"$or": [
|
| 415 |
-
{"user_id": ObjectId(user_id)},
|
| 416 |
-
{"user_id": user_id}
|
| 417 |
-
]
|
| 418 |
-
})
|
| 419 |
-
print(f"🔍 Debug: User {user_id} has {total_user_expenses} total expenses")
|
| 420 |
-
|
| 421 |
-
# Check if category exists in any expense (sample check)
|
| 422 |
-
sample_expenses = list(self.db.expenses.find({
|
| 423 |
-
"$or": [
|
| 424 |
-
{"user_id": ObjectId(user_id)},
|
| 425 |
-
{"user_id": user_id}
|
| 426 |
-
]
|
| 427 |
-
}).limit(5))
|
| 428 |
-
if sample_expenses:
|
| 429 |
-
print(f"🔍 Debug: Sample expense category fields:")
|
| 430 |
-
for exp in sample_expenses:
|
| 431 |
-
print(f" - category: {exp.get('category')}, categoryId: {exp.get('categoryId')}, headCategory: {exp.get('headCategory')}")
|
| 432 |
-
except Exception as e:
|
| 433 |
-
print(f"⚠️ Error in expense debug check: {e}")
|
| 434 |
-
|
| 435 |
-
except Exception as e:
|
| 436 |
-
print(f"❌ Error checking expenses: {e}")
|
| 437 |
-
import traceback
|
| 438 |
-
traceback.print_exc()
|
| 439 |
|
| 440 |
return False
|
| 441 |
|
|
|
|
| 267 |
except Exception:
|
| 268 |
continue
|
| 269 |
|
| 270 |
+
# THIRD: Try comprehensive query using $and and $or
|
| 271 |
try:
|
| 272 |
comprehensive_query = {
|
| 273 |
"$and": [
|
|
|
|
| 275 |
{"$or": category_conditions}
|
| 276 |
]
|
| 277 |
}
|
| 278 |
+
if self.db.budgets.count_documents(comprehensive_query) > 0:
|
|
|
|
|
|
|
| 279 |
return True
|
| 280 |
+
except Exception:
|
| 281 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
+
# FOURTH: Check expenses collection as fallback
|
|
|
|
| 284 |
try:
|
| 285 |
try:
|
| 286 |
category_objid = ObjectId(category_id)
|
| 287 |
+
expense_user_conditions = [
|
| 288 |
+
{"user_id": ObjectId(user_id)},
|
| 289 |
+
{"user_id": user_id},
|
| 290 |
+
{"createdBy": ObjectId(user_id)},
|
| 291 |
+
{"createdBy": user_id}
|
| 292 |
+
]
|
| 293 |
+
expense_category_conditions = [
|
| 294 |
+
{"category": category_objid},
|
| 295 |
+
{"category": category_id},
|
| 296 |
+
{"categoryId": category_objid},
|
| 297 |
+
{"categoryId": category_id},
|
| 298 |
+
{"headCategory": category_objid},
|
| 299 |
+
{"headCategory": category_id}
|
|
|
|
| 300 |
]
|
| 301 |
except (ValueError, TypeError):
|
| 302 |
+
expense_user_conditions = [
|
| 303 |
+
{"user_id": user_id},
|
| 304 |
+
{"createdBy": user_id}
|
| 305 |
+
]
|
| 306 |
+
expense_category_conditions = [
|
| 307 |
+
{"category": category_id},
|
| 308 |
+
{"categoryId": category_id},
|
| 309 |
+
{"headCategory": category_id}
|
| 310 |
]
|
| 311 |
|
| 312 |
+
for user_cond in expense_user_conditions:
|
| 313 |
+
for cat_cond in expense_category_conditions:
|
| 314 |
+
try:
|
| 315 |
+
if self.db.expenses.count_documents({**user_cond, **cat_cond}) > 0:
|
| 316 |
+
return True
|
| 317 |
+
except Exception:
|
| 318 |
+
continue
|
| 319 |
+
except Exception:
|
| 320 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
return False
|
| 323 |
|