Commit
·
32fb484
1
Parent(s):
ecfd501
Add detailed debug logging to understand budget category structure and why category_ids are not being matched
Browse files- app/smart_recommendation.py +89 -0
app/smart_recommendation.py
CHANGED
|
@@ -196,6 +196,95 @@ class SmartBudgetRecommender:
|
|
| 196 |
except Exception as e:
|
| 197 |
print(f"⚠️ Error with comprehensive query: {e}")
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
# Check expenses collection as fallback
|
| 200 |
try:
|
| 201 |
try:
|
|
|
|
| 196 |
except Exception as e:
|
| 197 |
print(f"⚠️ Error with comprehensive query: {e}")
|
| 198 |
|
| 199 |
+
# Additional check: Look for budgets with nested category arrays
|
| 200 |
+
# Some budgets might have categories nested in arrays like headCategories[].categories[]
|
| 201 |
+
try:
|
| 202 |
+
try:
|
| 203 |
+
category_objid = ObjectId(category_id)
|
| 204 |
+
except (ValueError, TypeError):
|
| 205 |
+
category_objid = category_id
|
| 206 |
+
|
| 207 |
+
# Try to find budgets with nested category structures
|
| 208 |
+
nested_query = {
|
| 209 |
+
"$and": [
|
| 210 |
+
{"$or": user_conditions},
|
| 211 |
+
{
|
| 212 |
+
"$or": [
|
| 213 |
+
{"headCategories": {"$elemMatch": {"headCategory": category_objid}}},
|
| 214 |
+
{"headCategories": {"$elemMatch": {"headCategory": category_id}}},
|
| 215 |
+
{"headCategories": {"$elemMatch": {"categories": {"$elemMatch": {"category": category_objid}}}}},
|
| 216 |
+
{"headCategories": {"$elemMatch": {"categories": {"$elemMatch": {"category": category_id}}}}},
|
| 217 |
+
]
|
| 218 |
+
}
|
| 219 |
+
]
|
| 220 |
+
}
|
| 221 |
+
nested_count = self.db.budgets.count_documents(nested_query)
|
| 222 |
+
if nested_count > 0:
|
| 223 |
+
print(f"✅ Found {nested_count} budget(s) with nested category structure for user {user_id} with category_id {category_id}")
|
| 224 |
+
return True
|
| 225 |
+
except Exception as e:
|
| 226 |
+
print(f"⚠️ Error checking nested categories: {e}")
|
| 227 |
+
|
| 228 |
+
# Debug: Check if user has ANY budgets at all and what categories they have
|
| 229 |
+
try:
|
| 230 |
+
user_objid = ObjectId(user_id)
|
| 231 |
+
total_budgets = self.db.budgets.count_documents({"$or": [{"createdBy": user_objid}, {"createdBy": user_id}, {"user_id": user_objid}, {"user_id": user_id}]})
|
| 232 |
+
print(f"🔍 Debug: User {user_id} has {total_budgets} total budgets")
|
| 233 |
+
|
| 234 |
+
# Get all budgets and check their category structure
|
| 235 |
+
all_budgets = list(self.db.budgets.find({"$or": [{"createdBy": user_objid}, {"createdBy": user_id}]}).limit(10))
|
| 236 |
+
if all_budgets:
|
| 237 |
+
print(f"🔍 Debug: Analyzing {len(all_budgets)} budgets to find category structure:")
|
| 238 |
+
category_ids_found = set()
|
| 239 |
+
for i, budget in enumerate(all_budgets[:5], 1): # Show first 5
|
| 240 |
+
cat_id = budget.get('category') or budget.get('categoryId') or budget.get('headCategory')
|
| 241 |
+
head_cats = budget.get('headCategories', [])
|
| 242 |
+
print(f" Budget {i}:")
|
| 243 |
+
print(f" - category: {budget.get('category')}")
|
| 244 |
+
print(f" - categoryId: {budget.get('categoryId')}")
|
| 245 |
+
print(f" - headCategory: {budget.get('headCategory')}")
|
| 246 |
+
print(f" - headCategories: {head_cats}")
|
| 247 |
+
|
| 248 |
+
# Check if requested category_id is in headCategories
|
| 249 |
+
if head_cats and isinstance(head_cats, list):
|
| 250 |
+
for hc in head_cats:
|
| 251 |
+
if isinstance(hc, dict):
|
| 252 |
+
hc_id = hc.get('headCategory')
|
| 253 |
+
if hc_id:
|
| 254 |
+
cat_id_str = str(hc_id)
|
| 255 |
+
category_ids_found.add(cat_id_str)
|
| 256 |
+
if cat_id_str == str(category_id):
|
| 257 |
+
print(f" ✅ MATCH FOUND: category_id {category_id} found in headCategories.headCategory!")
|
| 258 |
+
|
| 259 |
+
# Check nested categories
|
| 260 |
+
nested_cats = hc.get('categories', [])
|
| 261 |
+
if nested_cats:
|
| 262 |
+
for nc in nested_cats:
|
| 263 |
+
if isinstance(nc, dict):
|
| 264 |
+
nc_id = nc.get('category')
|
| 265 |
+
if nc_id:
|
| 266 |
+
nc_id_str = str(nc_id)
|
| 267 |
+
category_ids_found.add(nc_id_str)
|
| 268 |
+
if nc_id_str == str(category_id):
|
| 269 |
+
print(f" ✅ MATCH FOUND: category_id {category_id} found in headCategories.categories.category!")
|
| 270 |
+
|
| 271 |
+
if cat_id:
|
| 272 |
+
cat_id_str = str(cat_id)
|
| 273 |
+
category_ids_found.add(cat_id_str)
|
| 274 |
+
if cat_id_str == str(category_id):
|
| 275 |
+
print(f" ✅ MATCH FOUND: category_id {category_id} found in direct category field!")
|
| 276 |
+
|
| 277 |
+
print(f"🔍 Debug: Unique category_ids found in user's budgets: {list(category_ids_found)}")
|
| 278 |
+
print(f"🔍 Debug: Looking for category_id: {category_id}")
|
| 279 |
+
if str(category_id) not in category_ids_found:
|
| 280 |
+
print(f" ❌ Requested category_id {category_id} NOT found in any of the user's budgets")
|
| 281 |
+
else:
|
| 282 |
+
print(f" ✅ Requested category_id {category_id} IS in user's budgets but query didn't match!")
|
| 283 |
+
except Exception as e:
|
| 284 |
+
print(f"⚠️ Error in debug check: {e}")
|
| 285 |
+
import traceback
|
| 286 |
+
traceback.print_exc()
|
| 287 |
+
|
| 288 |
# Check expenses collection as fallback
|
| 289 |
try:
|
| 290 |
try:
|