Commit
·
a67e1f8
1
Parent(s):
b71b4c6
Improve category name lookup with better logging and fallback searches
Browse files- app/smart_recommendation.py +74 -3
app/smart_recommendation.py
CHANGED
|
@@ -264,6 +264,7 @@ class SmartBudgetRecommender:
|
|
| 264 |
category_id_obj = category_id
|
| 265 |
|
| 266 |
# First, try to find in headCategories collection
|
|
|
|
| 267 |
if isinstance(category_id_obj, ObjectId):
|
| 268 |
head_category_doc = self.db.headcategories.find_one({"_id": category_id_obj})
|
| 269 |
else:
|
|
@@ -275,9 +276,11 @@ class SmartBudgetRecommender:
|
|
| 275 |
if head_category_doc:
|
| 276 |
category_name = head_category_doc.get("name") or head_category_doc.get("title")
|
| 277 |
if category_name:
|
|
|
|
| 278 |
return category_name
|
| 279 |
|
| 280 |
# If not found in headCategories, try categories collection
|
|
|
|
| 281 |
if isinstance(category_id_obj, ObjectId):
|
| 282 |
category_doc = self.db.categories.find_one({"_id": category_id_obj})
|
| 283 |
else:
|
|
@@ -289,12 +292,56 @@ class SmartBudgetRecommender:
|
|
| 289 |
if category_doc:
|
| 290 |
category_name = category_doc.get("name") or category_doc.get("title")
|
| 291 |
if category_name:
|
|
|
|
| 292 |
return category_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
except Exception as e:
|
| 294 |
-
print(f"Error looking up category name for {category_id}: {e}")
|
| 295 |
pass
|
| 296 |
|
| 297 |
-
# If not found in either collection, return the ID as string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
return str(category_id) if category_id else "Uncategorized"
|
| 299 |
|
| 300 |
def _get_category_stats_from_budgets(
|
|
@@ -411,10 +458,34 @@ class SmartBudgetRecommender:
|
|
| 411 |
# Extract category ID from budget (could be in category, categoryId, headCategory fields)
|
| 412 |
category_id = b.get("category") or b.get("categoryId") or b.get("headCategory") or b.get("category_id")
|
| 413 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
# Get category name from headCategories or categories collection using category ID
|
| 415 |
if category_id:
|
|
|
|
| 416 |
category_name = self._get_category_name(category_id)
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
else:
|
| 419 |
# Fallback to budget name if no category ID found
|
| 420 |
category_name = b.get("name", "Uncategorized")
|
|
|
|
| 264 |
category_id_obj = category_id
|
| 265 |
|
| 266 |
# First, try to find in headCategories collection
|
| 267 |
+
head_category_doc = None
|
| 268 |
if isinstance(category_id_obj, ObjectId):
|
| 269 |
head_category_doc = self.db.headcategories.find_one({"_id": category_id_obj})
|
| 270 |
else:
|
|
|
|
| 276 |
if head_category_doc:
|
| 277 |
category_name = head_category_doc.get("name") or head_category_doc.get("title")
|
| 278 |
if category_name:
|
| 279 |
+
print(f"✅ Found category name in headCategories: {category_id} -> {category_name}")
|
| 280 |
return category_name
|
| 281 |
|
| 282 |
# If not found in headCategories, try categories collection
|
| 283 |
+
category_doc = None
|
| 284 |
if isinstance(category_id_obj, ObjectId):
|
| 285 |
category_doc = self.db.categories.find_one({"_id": category_id_obj})
|
| 286 |
else:
|
|
|
|
| 292 |
if category_doc:
|
| 293 |
category_name = category_doc.get("name") or category_doc.get("title")
|
| 294 |
if category_name:
|
| 295 |
+
print(f"✅ Found category name in categories: {category_id} -> {category_name}")
|
| 296 |
return category_name
|
| 297 |
+
|
| 298 |
+
# Try searching by category ID as string in other fields (fallback)
|
| 299 |
+
if isinstance(category_id, str):
|
| 300 |
+
# Try searching in headCategories by other fields
|
| 301 |
+
fallback_head = self.db.headcategories.find_one({"$or": [
|
| 302 |
+
{"_id": category_id},
|
| 303 |
+
{"categoryId": category_id},
|
| 304 |
+
{"id": category_id}
|
| 305 |
+
]})
|
| 306 |
+
if fallback_head:
|
| 307 |
+
category_name = fallback_head.get("name") or fallback_head.get("title")
|
| 308 |
+
if category_name:
|
| 309 |
+
print(f"✅ Found category name in headCategories (fallback): {category_id} -> {category_name}")
|
| 310 |
+
return category_name
|
| 311 |
+
|
| 312 |
+
# Try searching in categories by other fields
|
| 313 |
+
fallback_cat = self.db.categories.find_one({"$or": [
|
| 314 |
+
{"_id": category_id},
|
| 315 |
+
{"categoryId": category_id},
|
| 316 |
+
{"id": category_id}
|
| 317 |
+
]})
|
| 318 |
+
if fallback_cat:
|
| 319 |
+
category_name = fallback_cat.get("name") or fallback_cat.get("title")
|
| 320 |
+
if category_name:
|
| 321 |
+
print(f"✅ Found category name in categories (fallback): {category_id} -> {category_name}")
|
| 322 |
+
return category_name
|
| 323 |
+
|
| 324 |
+
# If not found, log a warning
|
| 325 |
+
print(f"⚠️ Category ID not found in headCategories or categories: {category_id}")
|
| 326 |
except Exception as e:
|
| 327 |
+
print(f"❌ Error looking up category name for {category_id}: {e}")
|
| 328 |
pass
|
| 329 |
|
| 330 |
+
# If not found in either collection, log and return the ID as string
|
| 331 |
+
print(f"⚠️ Category ID not found in headCategories or categories collections: {category_id}")
|
| 332 |
+
# Try one more time with string search (in case ID is stored as string in a different field)
|
| 333 |
+
try:
|
| 334 |
+
if isinstance(category_id, str):
|
| 335 |
+
# Try searching by name field containing the ID (unlikely but worth trying)
|
| 336 |
+
head_cat_by_name = self.db.headcategories.find_one({"name": category_id})
|
| 337 |
+
if head_cat_by_name:
|
| 338 |
+
return head_cat_by_name.get("name") or str(category_id)
|
| 339 |
+
cat_by_name = self.db.categories.find_one({"name": category_id})
|
| 340 |
+
if cat_by_name:
|
| 341 |
+
return cat_by_name.get("name") or str(category_id)
|
| 342 |
+
except Exception as e:
|
| 343 |
+
print(f"Final fallback search failed: {e}")
|
| 344 |
+
|
| 345 |
return str(category_id) if category_id else "Uncategorized"
|
| 346 |
|
| 347 |
def _get_category_stats_from_budgets(
|
|
|
|
| 458 |
# Extract category ID from budget (could be in category, categoryId, headCategory fields)
|
| 459 |
category_id = b.get("category") or b.get("categoryId") or b.get("headCategory") or b.get("category_id")
|
| 460 |
|
| 461 |
+
# Also check if category is nested in headCategories array
|
| 462 |
+
if not category_id:
|
| 463 |
+
head_categories = b.get("headCategories", [])
|
| 464 |
+
if head_categories and isinstance(head_categories, list):
|
| 465 |
+
# Try to get category from first headCategory's categories array
|
| 466 |
+
for head_cat in head_categories:
|
| 467 |
+
if isinstance(head_cat, dict):
|
| 468 |
+
nested_categories = head_cat.get("categories", [])
|
| 469 |
+
if nested_categories and isinstance(nested_categories, list):
|
| 470 |
+
# Get first category ID from nested categories
|
| 471 |
+
for nested_cat in nested_categories:
|
| 472 |
+
if isinstance(nested_cat, dict):
|
| 473 |
+
category_id = nested_cat.get("category")
|
| 474 |
+
if category_id:
|
| 475 |
+
break
|
| 476 |
+
if category_id:
|
| 477 |
+
break
|
| 478 |
+
|
| 479 |
# Get category name from headCategories or categories collection using category ID
|
| 480 |
if category_id:
|
| 481 |
+
print(f"🔍 Looking up category ID: {category_id} (type: {type(category_id).__name__})")
|
| 482 |
category_name = self._get_category_name(category_id)
|
| 483 |
+
if category_name == str(category_id):
|
| 484 |
+
# Category name lookup failed, still showing ID
|
| 485 |
+
print(f"⚠️ Category ID not resolved: {category_id} (not found in headCategories or categories collections)")
|
| 486 |
+
print(f" This means the category ID doesn't exist in the database. Please check if the category exists.")
|
| 487 |
+
else:
|
| 488 |
+
print(f"✅ Found category ID: {category_id} -> Name: '{category_name}'")
|
| 489 |
else:
|
| 490 |
# Fallback to budget name if no category ID found
|
| 491 |
category_name = b.get("name", "Uncategorized")
|