Skip to content

Commit db5c365

Browse files
jzbahraismcmurtry
andauthored
Task/removeoldkeys (#373)
* remove notification counts v1 * update utils * bump version --------- Co-authored-by: Stephen McMurtry <smcmurtry@gmail.com>
1 parent 5a38c25 commit db5c365

4 files changed

Lines changed: 22 additions & 148 deletions

File tree

.github/actions/waffles/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ docopt==0.6.2
22
Flask==2.3.3
33
markupsafe==2.1.5
44
setuptools==78.1.1 # required for distutils in Python 3.12
5-
git+https://github.com/cds-snc/notifier-utils.git@53.2.12#egg=notifications-utils
5+
git+https://github.com/cds-snc/notifier-utils.git@53.2.13#egg=notifications-utils

notifications_utils/clients/redis/annual_limit.py

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
"""
22
This module stores daily notification counts and annual limit statuses for a service in Redis using a hash structure:
33
4-
# TODO: Remove the first key once all services have been migrated to the new Redis structure
54
annual-limit: {
65
{service_id}: {
7-
notifications: {
8-
sms_delivered: int,
9-
email_delivered: int,
10-
sms_failed: int,
11-
email_failed: int
12-
},
136
status: {
147
near_sms_limit: Datetime,
158
near_email_limit: Datetime,
@@ -38,11 +31,6 @@
3831

3932
from notifications_utils.clients.redis.redis_client import RedisClient
4033

41-
# TODO: Remove the first 4 keys once all services have been migrated to the new Redis structure
42-
SMS_DELIVERED = "sms_delivered"
43-
EMAIL_DELIVERED = "email_delivered"
44-
SMS_FAILED = "sms_failed"
45-
EMAIL_FAILED = "email_failed"
4634
SMS_DELIVERED_TODAY = "sms_delivered_today"
4735
EMAIL_DELIVERED_TODAY = "email_delivered_today"
4836
SMS_FAILED_TODAY = "sms_failed_today"
@@ -51,7 +39,6 @@
5139
TOTAL_SMS_FISCAL_YEAR_TO_YESTERDAY = "total_sms_fiscal_year_to_yesterday"
5240
TOTAL_EMAIL_FISCAL_YEAR_TO_YESTERDAY = "total_email_fiscal_year_to_yesterday"
5341

54-
NOTIFICATION_FIELDS = [SMS_DELIVERED, EMAIL_DELIVERED, SMS_FAILED, EMAIL_FAILED]
5542
NOTIFICATION_FIELDS_V2 = [
5643
SMS_DELIVERED_TODAY,
5744
EMAIL_DELIVERED_TODAY,
@@ -69,14 +56,6 @@
6956
STATUS_FIELDS = [NEAR_SMS_LIMIT, NEAR_EMAIL_LIMIT, OVER_SMS_LIMIT, OVER_EMAIL_LIMIT]
7057

7158

72-
# TODO: Remove this once all services have been migrated to the new Redis structure
73-
def annual_limit_notifications_key(service_id):
74-
"""
75-
Generates the Redis hash key for storing daily metrics of a service.
76-
"""
77-
return f"annual-limit:{service_id}:notifications"
78-
79-
8059
def annual_limit_notifications_v2_key(service_id):
8160
"""
8261
Generates the Redis hash key for storing daily metrics of a service.
@@ -144,22 +123,14 @@ def increment_notification_count(self, service_id: str, field: str):
144123
service_id (str): _description_
145124
field (str): _description_
146125
"""
147-
# TODO: Remove the else
148126
if field in NOTIFICATION_FIELDS_V2:
149127
self._redis_client.increment_hash_value(annual_limit_notifications_v2_key(service_id), field)
150-
else:
151-
self._redis_client.increment_hash_value(annual_limit_notifications_key(service_id), field)
152128

153129
def get_notification_count(self, service_id: str, field: str):
154130
"""
155131
Retrieves the specified daily notification count for a service. (e.g. SMS_DELIVERED, EMAIL_FAILED, SMS_DELIVERED_TODAY etc.)
156132
"""
157133
count = self._redis_client.get_hash_field(annual_limit_notifications_v2_key(service_id), field)
158-
if count:
159-
return int(count.decode("utf-8"))
160-
# TODO: Remove this once all services have been migrated to the new Redis structure
161-
# TODO: Change the above to return 0 if count is None
162-
count = self._redis_client.get_hash_field(annual_limit_notifications_key(service_id), field)
163134
return 0 if not count else int(count.decode("utf-8"))
164135

165136
def get_all_notification_counts(self, service_id: str):
@@ -177,10 +148,7 @@ def get_all_notification_counts(self, service_id: str):
177148
int,
178149
NOTIFICATION_FIELDS_V2,
179150
)
180-
# TODO: Remove this once all services have been migrated to the new Redis structure
181-
return prepare_byte_dict(
182-
self._redis_client.get_all_from_hash(annual_limit_notifications_key(service_id)), int, NOTIFICATION_FIELDS
183-
)
151+
return {}
184152

185153
def reset_all_notification_counts(self, service_ids=None):
186154
"""Resets all daily notification metrics.
@@ -196,31 +164,14 @@ def reset_all_notification_counts(self, service_ids=None):
196164
)
197165
# We also want to remove the seeded_at field from the notifications_v2 hash
198166
self._redis_client.delete_hash_fields(hashes=hashes, fields=NOTIFICATION_FIELDS_V2 + [SEEDED_AT])
199-
# TODO: Remove the else once all services have been migrated to the new Redis structure
200-
hashes = (
201-
annual_limit_notifications_key("*")
202-
if not service_ids
203-
else [annual_limit_notifications_key(service_id) for service_id in service_ids]
204-
)
205-
self._redis_client.delete_hash_fields(hashes=hashes, fields=NOTIFICATION_FIELDS)
206167

207168
def seed_annual_limit_notifications(self, service_id: str, mapping: dict):
208169
"""Seeds annual limit notifications for a service.
209-
# TODO: Update the docstring once all services have been migrated to the new Redis structure
210170
Args:
211171
service_id (str): Service to seed annual limit notifications for.
212172
mapping (dict): A dict used to map notification counts to their respective fields formatted as follows
213173
214174
Examples:
215-
`mapping` format:
216-
217-
{
218-
"sms_delivered": int,
219-
"email_delivered": int,
220-
"sms_failed": int,
221-
"email_failed": int,
222-
}
223-
as we added notifications_v2, the mapping can also be:
224175
{
225176
"sms_delivered_today": int,
226177
"email_delivered_today": int,
@@ -252,28 +203,6 @@ def seed_annual_limit_notifications(self, service_id: str, mapping: dict):
252203
v2_values = prepare_byte_dict(self._redis_client.get_all_from_hash(annual_limit_notifications_v2_key(service_id)), str)
253204
current_app.logger.info(f"[alimit-debug-redis] Finished setting values, result is: {v2_values}")
254205

255-
# Create a legacy mapping from either original legacy keys or mapped from V2 keys
256-
legacy_mapping = {}
257-
258-
# First try to use any legacy fields directly present in the mapping
259-
for k in NOTIFICATION_FIELDS:
260-
if k in mapping:
261-
legacy_mapping[k] = mapping[k]
262-
263-
# If legacy fields aren't present, map from V2 fields
264-
if SMS_DELIVERED_TODAY in mapping and SMS_DELIVERED not in legacy_mapping:
265-
legacy_mapping[SMS_DELIVERED] = mapping[SMS_DELIVERED_TODAY]
266-
if EMAIL_DELIVERED_TODAY in mapping and EMAIL_DELIVERED not in legacy_mapping:
267-
legacy_mapping[EMAIL_DELIVERED] = mapping[EMAIL_DELIVERED_TODAY]
268-
if SMS_FAILED_TODAY in mapping and SMS_FAILED not in legacy_mapping:
269-
legacy_mapping[SMS_FAILED] = mapping[SMS_FAILED_TODAY]
270-
if EMAIL_FAILED_TODAY in mapping and EMAIL_FAILED not in legacy_mapping:
271-
legacy_mapping[EMAIL_FAILED] = mapping[EMAIL_FAILED_TODAY]
272-
273-
# Store legacy fields if we have any
274-
if legacy_mapping:
275-
self._redis_client.bulk_set_hash_fields(key=annual_limit_notifications_key(service_id), mapping=legacy_mapping)
276-
277206
current_app.logger.info(f"[alimit-debug-redis] Setting seeded_at for service {service_id}")
278207
self.set_seeded_at(service_id)
279208
current_app.logger.info(
@@ -293,16 +222,12 @@ def set_seeded_at(self, service_id):
293222
self._redis_client.set_hash_value(
294223
annual_limit_notifications_v2_key(service_id), SEEDED_AT, datetime.utcnow().strftime("%Y-%m-%d")
295224
)
296-
# TODO: Remove the below once all services have been migrated to the new Redis structure
297-
# Setting the seeded at in status for backward compatibility
298-
self._redis_client.set_hash_value(annual_limit_status_key(service_id), SEEDED_AT, datetime.utcnow().strftime("%Y-%m-%d"))
299225

300226
def clear_notification_counts(self, service_id: str):
301227
"""
302228
Clears all daily notification metrics for a service.
303229
"""
304230
self._redis_client.expire(annual_limit_notifications_v2_key(service_id), -1)
305-
self._redis_client.expire(annual_limit_notifications_key(service_id), -1)
306231

307232
def set_annual_limit_status(self, service_id: str, field: str, value: datetime):
308233
"""
@@ -351,23 +276,15 @@ def clear_annual_limit_statuses(self, service_id: str):
351276
# Helper methods for daily metrics
352277
def increment_sms_delivered(self, service_id: str):
353278
self.increment_notification_count(service_id, SMS_DELIVERED_TODAY)
354-
# TODO: remove the below line
355-
self.increment_notification_count(service_id, SMS_DELIVERED)
356279

357280
def increment_sms_failed(self, service_id: str):
358281
self.increment_notification_count(service_id, SMS_FAILED_TODAY)
359-
# TODO: remove the below line
360-
self.increment_notification_count(service_id, SMS_FAILED)
361282

362283
def increment_email_delivered(self, service_id: str):
363284
self.increment_notification_count(service_id, EMAIL_DELIVERED_TODAY)
364-
# TODO: remove the below line
365-
self.increment_notification_count(service_id, EMAIL_DELIVERED)
366285

367286
def increment_email_failed(self, service_id: str):
368287
self.increment_notification_count(service_id, EMAIL_FAILED_TODAY)
369-
# TODO: remove the below line
370-
self.increment_notification_count(service_id, EMAIL_FAILED)
371288

372289
# Helper methods for annual limits statuses
373290
def set_nearing_sms_limit(self, service_id: str):
@@ -412,11 +329,8 @@ def delete_all_annual_limit_hashes(self, service_ids=None):
412329
"""
413330
if not service_ids:
414331
self._redis_client.delete_cache_keys_by_pattern(annual_limit_notifications_v2_key("*"))
415-
# TODO: Remove the line below
416-
self._redis_client.delete_cache_keys_by_pattern(annual_limit_notifications_key("*"))
417332
self._redis_client.delete_cache_keys_by_pattern(annual_limit_status_key("*"))
418333
else:
419334
for service_id in service_ids:
420335
self._redis_client.delete(annual_limit_notifications_v2_key(service_id))
421-
self._redis_client.delete(annual_limit_notifications_key(service_id))
422336
self._redis_client.delete(annual_limit_status_key(service_id))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "notifications-utils"
3-
version = "53.2.12"
3+
version = "53.2.13"
44
description = "Shared python code for Notification - Provides logging utils etc."
55
authors = ["Canadian Digital Service"]
66
license = "MIT license"

0 commit comments

Comments
 (0)