Skip to content

Commit d9ca243

Browse files
authored
fix: filters (#590)
1 parent 04190cb commit d9ca243

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

keep/api/routes/alerts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def get_alert(
573573
if alert:
574574
return alert[0]
575575
else:
576-
return HTTPException(status_code=404, detail="Alert not found")
576+
raise HTTPException(status_code=404, detail="Alert not found")
577577

578578

579579
@router.post(

keep/workflowmanager/workflowmanager.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,28 @@ def insert_events(self, tenant_id, events: typing.List[AlertDto]):
8888
# TODO: more sophisticated filtering/attributes/nested, etc
8989
filter_key = filter.get("key")
9090
filter_val = filter.get("value")
91-
if not getattr(event, filter_key, None):
91+
event_val = self._get_event_value(event, filter_key)
92+
if not event_val:
9293
self.logger.warning(
9394
"Failed to run filter, skipping the event. Probably misconfigured workflow."
9495
)
96+
should_run = False
9597
continue
9698
# if its list, check if the filter is in the list
97-
if isinstance(getattr(event, filter_key), list):
98-
for val in getattr(event, filter_key):
99+
if isinstance(event_val, list):
100+
for val in event_val:
99101
# if one filter applies, it should run
100102
if self._apply_filter(filter_val, val):
101103
should_run = True
102104
break
103105
should_run = False
104106
# elif the filter is string/int/float, compare them:
105-
elif type(getattr(event, filter_key, None)) in [
107+
elif type(event_val) in [
106108
int,
107109
str,
108110
float,
109111
]:
110-
val = getattr(event, filter_key)
111-
if not self._apply_filter(filter_val, val):
112+
if not self._apply_filter(filter_val, event_val):
112113
self.logger.debug(
113114
"Filter didn't match, skipping",
114115
extra={
@@ -148,6 +149,24 @@ def insert_events(self, tenant_id, events: typing.List[AlertDto]):
148149
}
149150
)
150151

152+
def _get_event_value(self, event, filter_key):
153+
# if the filter key is a nested key, get the value
154+
if "." in filter_key:
155+
filter_key_split = filter_key.split(".")
156+
# event is alert dto so we need getattr
157+
event_val = getattr(event, filter_key_split[0], None)
158+
if not event_val:
159+
return None
160+
# iterate the other keys
161+
for key in filter_key_split[1:]:
162+
event_val = event_val.get(key, None)
163+
# if the key doesn't exist, return None because we didn't find the value
164+
if not event_val:
165+
return None
166+
return event_val
167+
else:
168+
return getattr(event, filter_key, None)
169+
151170
# TODO should be fixed to support the usual CLI
152171
def run(self, workflows: list[Workflow]):
153172
"""

0 commit comments

Comments
 (0)