Skip to content

Commit

Permalink
Adde rule type_ in routing_history
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Foschini committed Jun 15, 2023
1 parent 7c67090 commit e64e40e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 1.6.x
### 1.6.2
#### Bugfix
* Added filter in routing_filter
### 1.6.1
#### Bugfix
* Fixed TypeError if output is None
Expand Down
18 changes: 12 additions & 6 deletions routingfilter/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def match(self, event: dict, type_: str = "streams", tag_field_name: str = "tags
for rule in rules:
# check if ALL the filters are matching
filters = [ConfigFilter(f) for f in rule.get("filters", [])]
if all(f.is_matching(event) for f in filters) and not self.rule_in_routing_history(event, rule):
if all(f.is_matching(event) for f in filters) and not self.rule_in_routing_history(type_, event, rule):
matching_rules.append(rule)
break
if not matching_rules:
for tag_field_name in msg_tags:
for rule in self.rules[type_]["rules"].get(tag_field_name, []):
# check if ALL the filters are matching
config_filters = [ConfigFilter(f) for f in rule.get("filters", [])]
if config_filters and all(f.is_matching(event) for f in config_filters) and not self.rule_in_routing_history(event, rule):
if config_filters and all(f.is_matching(event) for f in config_filters) and not self.rule_in_routing_history(type_, event, rule):
matching_rules.append(rule)
break # the first matching rule wins if it doesn't exist in the output field
# Rename "filters" to "rules" and "type" to "output" to be more generic
Expand All @@ -82,7 +82,8 @@ def match(self, event: dict, type_: str = "streams", tag_field_name: str = "tags
mr["rules"] = mr.pop("filters")
if type_ in mr:
mr["output"] = mr.pop(type_)
if mr["output"]:
# We are only interested in dict type
if mr["output"] and isinstance(mr["output"], dict):
for k in mr["output"].keys():
event["certego"]["routing_history"][k] = datetime.now().isoformat()
return matching_rules
Expand Down Expand Up @@ -193,17 +194,22 @@ def _validate_rules(self, rules: dict) -> None:
config_filter_obj = ConfigFilter(filter_)
config_filter_obj.is_matching({})

def rule_in_routing_history(self, event, rule):
def rule_in_routing_history(self, type_, event, rule):
"""Checking if the given rule has already been processed
:param type_: The type_ of the event
:type type_: dict
:param event: The entire event to process
:type event: dict
:param rule: The rule to check
:type rule: dict
"""
if rule["streams"] is None:
if rule[type_] is None:
return False
for key in rule["streams"].keys():
# we are only interested in rule[type_] containing dict
if not isinstance(rule[type_], dict):
return False
for key in rule[type_].keys():
if key in event["certego"]["routing_history"]:
return True
return False
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='routingfilter',
version='1.6.1',
version='1.6.2',
packages=['routingfilter'],
include_package_data=True,
install_requires=["IPy~=1.1", "macaddress~=2.0.2"],
Expand Down

0 comments on commit e64e40e

Please sign in to comment.