Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added count method #15

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.x
### 2.1.0
#### Features
* Added count method in Routing

## 2.0.x
### 2.0.0
#### Changes
Expand Down
11 changes: 11 additions & 0 deletions routing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,17 @@ def test_get_stats(self):
self.assertDictEqual(self.routing.get_stats(delete=True), expected_stats)
self.assertDictEqual(self.routing.get_stats(), {"streams": {"exists-fh0wery": {}, "equals-fbh49ry29": {}}, "customer": {"customer-dh8rh9fow": {}}})

def test_count(self):
rule_list = [
load_test_data("test_rule_1_equals"),
load_test_data("test_rule_3_customer_equals"),
load_test_data("test_rule_4_multiple_filters"),
load_test_data("test_rule_5_exists"),
load_test_data("test_rule_6_not_exists"),
]
self.routing.load_from_dicts(rule_list)
self.assertEqual(self.routing.count(), 5)


if __name__ == "__main__":
unittest.main()
9 changes: 9 additions & 0 deletions routingfilter/filters/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ def __init__(self, tag: str):
self._rules = []
self.logger = logging.getLogger(self.__class__.__name__)

def count(self) -> int:
"""
Return the number of rules.

:return: number of rules
:rtype: int
"""
return len(self._rules)

def match(self, event: DictQuery, tag: str) -> Results | None:
"""
Call all match methods of the Rules and return the result of first match.
Expand Down
12 changes: 12 additions & 0 deletions routingfilter/filters/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ def __init__(self, stream):
self._ruleManagers = {}
self.logger = logging.getLogger(self.__class__.__name__)

def count(self) -> int:
"""
Returns the number of rules in the stream.

:return: number of rules
:rtype: int
"""
count_rules = 0
for key in self._ruleManagers.keys():
count_rules += self._ruleManagers[key].count()
return count_rules

def match(self, event: DictQuery, tag_field_name: str) -> List[Results]:
"""
Call all ruleManagers that contain tha tag of event "tags" field (that could be a list).
Expand Down
11 changes: 11 additions & 0 deletions routingfilter/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def __init__(self):
self.variables = {}
self.logger = logging.getLogger(self.__class__.__name__)

def count(self) -> int:
"""
Return the number of the rules.

:return: number of the rules
:rtype: int
"""
streams_count = self.streams.count()
customer_count = self.customer.count()
return streams_count + customer_count

def get_stats(self, delete: bool = False) -> dict:
"""
Call get_stats of streams and return the stats. If delete is True, reset the stats.
Expand Down
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="2.0.0",
version="2.1.0",
packages=["routingfilter"],
include_package_data=True,
install_requires=["IPy~=1.1", "macaddress~=2.0.2"],
Expand Down
Loading