diff --git a/notifications_routing/auditing.py b/notifications_routing/auditing.py index 902fc5f24c95dde4573e61f6bfff3dae735e1069..ecfe8b927e8c054570dc56cbf59009ef232a91e9 100644 --- a/notifications_routing/auditing.py +++ b/notifications_routing/auditing.py @@ -42,12 +42,12 @@ def _external_values(value): ]: del filtered_value["users"] if value["event"] == AuditingEvents.UNSUSCRIBED_USERS: - filtered_value.update({"targets": len(value["targets"])}) + filtered_value.update({"total": len(value["targets"])}) if value["event"] == AuditingEvents.EXPANDED_RECIPIENTS: del filtered_value["targets"] - if value["event"] in [AuditingEvents.CHANNEL_SNAPSHOT, AuditingEvents.NOTIFICATION_TARGETS]: - if "target_users" in filtered_value: - for user in filtered_value["target_users"]: + if value["event"] in [AuditingEvents.NOTIFICATION_TARGETS]: + if AuditingEvents.TARGET_USERS in filtered_value: + for user in filtered_value[AuditingEvents.TARGET_USERS]: del user["user_id"] del user["last_login"] if "users" in filtered_value: diff --git a/notifications_routing/data_source/postgres/postgres_data_source.py b/notifications_routing/data_source/postgres/postgres_data_source.py index 4d8c4010c46eccc38e78fdfafbef49d008b0e0aa..b760d6e909fd1b4582b698dd14795624eb38fea0 100644 --- a/notifications_routing/data_source/postgres/postgres_data_source.py +++ b/notifications_routing/data_source/postgres/postgres_data_source.py @@ -132,7 +132,7 @@ class PostgresDataSource(DataSource): return [self.__build_user(member) for member in notification.target_users] - def get_target_groups(self, notification_id: str, **kwargs) -> List[str]: + def get_target_groups(self, notification_id: str, **kwargs) -> List[Dict[str, str]]: """Return the list of groups targeted to a notification. :param notification_id: Notification UUID @@ -144,7 +144,7 @@ class PostgresDataSource(DataSource): if not notification: raise NotFoundDataSourceError(Notification, id=notification_id) - return [group.groupIdentifier for group in notification.target_groups] + return [self.__build_group(group) for group in notification.target_groups] def get_channel_unsubscribed_users(self, channel_id: str, **kwargs) -> List[str]: """Return the list of users unsubscribed to a channel. diff --git a/notifications_routing/preferences.py b/notifications_routing/preferences.py index 981a19dd0d4b9d02254f5f0b73ba0b42561ad8ba..9d7b6b17a18dad9f1e88fed37b81627b98a70d53 100644 --- a/notifications_routing/preferences.py +++ b/notifications_routing/preferences.py @@ -12,6 +12,7 @@ from notifications_routing.data_source.data_source import DataSource from notifications_routing.data_source.postgres.postgres_data_source import Device, Preference, UserFeedNotification from notifications_routing.exceptions import DuplicatedError from notifications_routing.utils import ( + AuditingEvents, OutputMessageKeys, convert_notification_email_to_json_string, convert_notification_push_to_json_string, @@ -124,7 +125,7 @@ def apply_user_preferences( audit_notification( message[OutputMessageKeys.ID], { - "event": "Preference added to feed", + "event": AuditingEvents.PREFENCE_ADDED, "method": preference.method, "preference": preference.preference_id, }, @@ -138,7 +139,7 @@ def apply_user_preferences( audit_notification( message[OutputMessageKeys.ID], { - "event": "Sent to consumer", + "event": AuditingEvents.SENT_CONSUMER, "method": preference.method, "device": device.type, "subtype": device.subtype if hasattr(device, "subtype") else "", @@ -222,7 +223,7 @@ def apply_all( audit_notification( message[OutputMessageKeys.ID], { - "event": "Critical sent to consumer", + "event": AuditingEvents.CRITICAL_CONSUMER, "device": device.type, "subtype": device.subtype if hasattr(device, "subtype") else "", "token": device.token, @@ -243,7 +244,7 @@ def apply_default_preferences(publisher: megabus.Publisher, message: Dict, email send_live_email(publisher, message, email) audit_notification( message[OutputMessageKeys.ID], - {"event": "Default sent to consumer", "device": "MAIL", "token": email}, + {"event": AuditingEvents.DEFAULT_CONSUMER, "device": "MAIL", "token": email}, user_id=email, ) diff --git a/notifications_routing/router.py b/notifications_routing/router.py index d8c6fa808878b3a2ddede5894af46e3bbc4cbd08..ca4156bdd420feecec53e97e1b9e5cca16fc7296 100644 --- a/notifications_routing/router.py +++ b/notifications_routing/router.py @@ -92,10 +92,17 @@ class Router(megabus.Listener): unique_usernames = [channel_user[self.data_source.USERNAME] for channel_user in users] logging.debug("notification %s channel usernames: %s", notification_id, unique_usernames) - for group_id in groups: - group_users = self.data_source.get_group_users(group_id) + for group in groups: + + logging.debug("checking users for group: %s", group) + group_users = self.data_source.get_group_users(group[DataSource.GROUP_ID]) audit_notification( - notification_id, {"event": AuditingEvents.GET_GROUP_USERS, "group": group_id, "users": group_users} + notification_id, + { + "event": AuditingEvents.GET_GROUP_USERS, + "group": group[DataSource.GROUP_IDENTIFIER], + "users": group_users, + }, ) logging.debug("channel %s groups users %s", channel_id, group_users) @@ -142,7 +149,7 @@ class Router(megabus.Listener): notification_id, channel_id, users, - [group[DataSource.GROUP_ID] for group in groups], + groups, [unsubscribed[DataSource.USERNAME] for unsubscribed in unsubscribed_users], ) @@ -150,7 +157,7 @@ class Router(megabus.Listener): notification_id, { "event": AuditingEvents.CHANNEL_SNAPSHOT, - "total_computed_users": len(users + unsubscribed_users), + AuditingEvents.TOTAL_COMPUTED_USERS: len(users + unsubscribed_users), "users": users + unsubscribed_users, "groups": [group[DataSource.GROUP_IDENTIFIER] for group in groups], }, @@ -173,8 +180,8 @@ class Router(megabus.Listener): notification_id, { "event": AuditingEvents.NOTIFICATION_TARGETS, - "target_users": target_users, - "target_groups": target_groups, + AuditingEvents.TARGET_USERS: target_users, + AuditingEvents.TARGET_GROUPS: [group[DataSource.GROUP_IDENTIFIER] for group in target_groups], }, external=True, ) @@ -279,7 +286,7 @@ class Router(megabus.Listener): ): audit_notification( message[OutputMessageKeys.ID], - {"event": "Mute active, skipped user"}, + {"event": AuditingEvents.MUTE_ACTIVE}, user_id=user[self.data_source.EMAIL], ) logging.info( @@ -304,7 +311,7 @@ class Router(megabus.Listener): if not delivery_methods_and_targets: audit_notification( message[OutputMessageKeys.ID], - {"event": "No preferences, skipped user"}, + {"event": AuditingEvents.NO_PREFERENCES}, user_id=user[self.data_source.EMAIL], ) diff --git a/notifications_routing/utils.py b/notifications_routing/utils.py index 1c10bde9a99e06cea93149d5b50fe0f8d3a7ee14..7bcd11a0475390668b991a41e38c2e0d45ea4854 100644 --- a/notifications_routing/utils.py +++ b/notifications_routing/utils.py @@ -61,15 +61,28 @@ class FeedFrequency(StrEnum): class AuditingEvents: """Audit event types.""" + # Router events GET_GROUP_USERS = "Get group users" UNSUSCRIBED_USERS = "Unsubscribed users" + CHANNEL_SNAPSHOT = "Channel snapshot" + NOTIFICATION_TARGETS = "Notification targets" START_PROCESSING_TARGETED_NOTIFICATION = "Start processing targeted notification" - START_PROCESSING = "Start processing" TARGET_USERS = "Target users" - CHANNEL_USERS_FOR_INTERSECTION = "Computed users for intersection" + START_PROCESSING = "Start processing" EXPANDED_RECIPIENTS = "Expanded recipients" - CHANNEL_SNAPSHOT = "Channel snapshot" - NOTIFICATION_TARGETS = "Notification targets" + MUTE_ACTIVE = "Mute active, skipped user" + NO_PREFERENCES = "No preferences, skipped user" + + # Router other field constants + TARGET_GROUPS = "Target groups" + TOTAL_COMPUTED_USERS = "Total computer users" + CHANNEL_USERS_FOR_INTERSECTION = "Computed users for intersection" + + # Preferences events + DEFAULT_CONSUMER = "Default sent to consumer" + SENT_CONSUMER = "Sent to consumer" + CRITICAL_CONSUMER = "Critical sent to consumer" + PREFENCE_ADDED = "Preference added to feed" def is_time_between(time, start_range, end_range): diff --git a/tests/integration/test_postgres_data_source.py b/tests/integration/test_postgres_data_source.py index 451cf12d213031f06a9a22f1584663e682cabb8d..f25337feaff2700dc054c7c5e7dc428e81835fa9 100644 --- a/tests/integration/test_postgres_data_source.py +++ b/tests/integration/test_postgres_data_source.py @@ -36,7 +36,7 @@ def test_get_notification_target_groups(appctx, data_source, target_notification assert len(groups) == 1 channel_group = groups[0] - assert str(channel_group) == "test-group" + assert str(channel_group["group_identifier"]) == "test-group" def test_get_channel_users(appctx, data_source, channel): diff --git a/tests/unit/test_router.py b/tests/unit/test_router.py index bfd9d7de45cd8ef789b4d89b6a16430d2a9745ca..622d3e8b4bdc8714dfaa66f935e6864f6fe22357 100644 --- a/tests/unit/test_router.py +++ b/tests/unit/test_router.py @@ -149,7 +149,10 @@ def test_process_users_target_users( ): """Test get_target_users.""" router_mock.data_source.get_target_users.return_value = [system_user_1, system_user_2] - router_mock.data_source.get_target_groups.return_value = [group_1["group_id"], group_2["group_id"]] + router_mock.data_source.get_target_groups.return_value = [ + {"group_id": "0557969f-acd8-433c-b9c9-59ea642b751b", "group_identifier": "group 1"}, + {"group_id": "378ec6a5-cf3f-459f-bb59-0de749edbba3", "group_identifier": "group 2"}, + ] router_mock.data_source.get_channel_unsubscribed_users.return_value = [system_user_1["username"]] router_mock.data_source.get_group_users.side_effect = [ [group_user_1], # ignored because its on unsub