fix: cat_name not defined in impact_service evaluate_event_impacts

_find_cat() returned only default_impacts, never the category name.
Now returns (name, defaults) tuple; cat_name → matched_cat_name.
Fixes: 'name cat_name is not defined' on AI evaluate.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OpenSquared
2026-06-25 21:55:43 +02:00
parent acab69b65c
commit bf938067a1

View File

@@ -127,16 +127,17 @@ def evaluate_event_impacts(
all_cats = get_all_event_categories()
def _find_cat(candidates):
"""Returns (category_name, default_impacts) or (None, None)."""
for c in candidates:
if c is None:
continue
row = next((x for x in all_cats if x["name"].lower() == c.lower()), None)
if row:
try:
return json.loads(row.get("default_impacts") or "[]")
return row["name"], json.loads(row.get("default_impacts") or "[]")
except Exception:
pass
return None
return None, None
sub = (event.get("sub_type") or "").strip()
cat = (event.get("category") or "").strip()
@@ -153,7 +154,7 @@ def evaluate_event_impacts(
elif row.get("type") == cat and not sub:
candidates.append(row["name"])
category_defaults = _find_cat(candidates)
matched_cat_name, category_defaults = _find_cat(candidates)
api_key = _get_api_key()
if not api_key:
@@ -190,7 +191,7 @@ def evaluate_event_impacts(
"source_id": event_id,
"source_name": event["name"],
"source_date": event["start_date"],
"category_name": cat_name,
"category_name": matched_cat_name or sub or cat,
"instrument_id": imp["instrument_id"],
"impact_score": float(imp.get("impact_score", 0.5)),
"direction": imp.get("direction", "neutral"),