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

Annual Limit daily delivered & failed notification counts and seeding implementation #333

Merged
merged 8 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .github/actions/waffles/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
docopt==0.6.2
Flask==2.3.3
markupsafe==2.1.5
git+https://github.com/cds-snc/[email protected].6#egg=notifications-utils
git+https://github.com/cds-snc/[email protected].7#egg=notifications-utils
10 changes: 10 additions & 0 deletions notifications_utils/clients/redis/annual_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ def get_all_notification_counts(self, service_id: str):
"""
return decode_byte_dict(self._redis_client.get_all_from_hash(notifications_key(service_id)))

def reset_all_notification_counts(self):
"""
Resets all daily notification metrics for all services. Uses non-blocking scan_iter method to avoid locking the Redis server.
"""
pattern = notifications_key("*") # All notification keys regardless of service_id
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename this key to the annual limits notification key?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is already used in places, then it is fine


self._redis_client.bulk_set_hash_fields(
pattern, ({"email_delivered": 0, "email_failed": 0, "sms_delivered": 0, "sms_failed": 0})
)

def clear_notification_counts(self, service_id: str):
"""
Clears all daily notification metrics for a service.
Expand Down
14 changes: 14 additions & 0 deletions notifications_utils/clients/redis/redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ def delete_cache_keys_by_pattern(self, pattern):
return self.scripts["delete-keys-by-pattern"](args=[pattern])
return 0

def bulk_set_hash_fields(self, pattern, mapping, raise_exception=False):
"""
Bulk set hash fields.
:param pattern: the pattern to match keys
:param mappting: the mapping of fields to set
:param raise_exception: True if we should allow the exception to bubble up
"""
if self.active:
try:
for key in self.redis_store.scan_iter(pattern):
self.redis_store.hmset(key, mapping)
except Exception as e:
self.__handle_exception(e, raise_exception, "bulk_set_hash_fields", pattern)

def exceeded_rate_limit(self, cache_key, limit, interval, raise_exception=False):
"""
Rate limiting.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "notifications-utils"
version = "52.3.6"
version = "52.3.7"
description = "Shared python code for Notification - Provides logging utils etc."
authors = ["Canadian Digital Service"]
license = "MIT license"
Expand Down
25 changes: 25 additions & 0 deletions tests/test_annual_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ def test_clear_notification_counts(mock_annual_limit_client, mock_notification_c
assert len(mock_annual_limit_client.get_all_notification_counts(mocked_service_id)) == 0


@pytest.mark.parametrize(
"service_ids",
[
[
str(uuid.uuid4()),
str(uuid.uuid4()),
str(uuid.uuid4()),
str(uuid.uuid4()),
]
],
)
def test_bulk_reset_notification_counts(mock_annual_limit_client, mock_notification_count_types, service_ids):
for service_id in service_ids:
for field in mock_notification_count_types:
mock_annual_limit_client.increment_notification_count(service_id, field)
assert len(mock_annual_limit_client.get_all_notification_counts(service_id)) == 4

mock_annual_limit_client.reset_all_notification_counts()

for service_id in service_ids:
assert len(mock_annual_limit_client.get_all_notification_counts(service_id)) == 4
for field in mock_notification_count_types:
assert mock_annual_limit_client.get_notification_count(service_id, field) == 0


def test_set_annual_limit_status(mock_annual_limit_client, mocked_service_id):
mock_annual_limit_client.set_annual_limit_status(mocked_service_id, NEAR_SMS_LIMIT, datetime.utcnow())
result = mock_annual_limit_client.get_annual_limit_status(mocked_service_id, NEAR_SMS_LIMIT)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,51 @@ def test_set_hash_value(mocked_redis_client):
mocked_redis_client.redis_store.hset.assert_called_with(key, field, value)


@pytest.mark.parametrize(
"hash, updates, expected",
[
(
{
"key1": {
"field1": "value1",
"field2": 2,
"field3": "value3".encode("utf-8"),
},
"key2": {
"field1": "value1",
"field2": 2,
"field3": "value3".encode("utf-8"),
},
"key3": {
"field1": "value1",
"field2": 2,
"field3": "value3".encode("utf-8"),
},
},
{
"field1": "value2",
"field2": 3,
"field3": "value4".encode("utf-8"),
},
{
b"field1": b"value2",
b"field2": b"3",
b"field3": b"value4",
},
)
],
)
def test_bulk_set_hash_fields(better_mocked_redis_client, hash, updates, expected):
for key, fields in hash.items():
for field, value in fields.items():
better_mocked_redis_client.set_hash_value(key, field, value)

better_mocked_redis_client.bulk_set_hash_fields("key*", mapping=updates)

for key, _ in hash.items():
assert better_mocked_redis_client.redis_store.hgetall(key) == expected


def test_decrement_hash_value_should_decrement_value_by_one_for_key(mocked_redis_client):
key = "12345"
value = "template-1111"
Expand Down
Loading