Skip to content

Commit

Permalink
Add entry to subevent log when filtering out recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRiese committed Dec 10, 2024
1 parent 4561106 commit e27e1b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions corehq/apps/sms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ class MessagingEvent(models.Model, MessagingStatusMixin):
ERROR_FCM_NO_ACTION = "FCM_NO_ACTION"
ERROR_FCM_NOTIFICATION_FAILURE = "FCM_NOTIFICATION_FAILURE"
ERROR_FCM_DOMAIN_NOT_ENABLED = 'FCM_DOMAIN_NOT_ENABLED'
ERROR_FILTER_MISMATCH = 'FILTER_MISMATCH'

ERROR_MESSAGES = {
ERROR_NO_RECIPIENT:
Expand Down
24 changes: 17 additions & 7 deletions corehq/messaging/scheduling/scheduling_partitioned/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ def _get_filter_value(self, filter_value_or_property_name):

def _passes_user_data_filter(self, contact):
if not isinstance(contact, CouchUser):
return True
return True, ""

if not self.memoized_schedule.user_data_filter:
return True
return True, ""

if self.memoized_schedule.use_user_case_for_filter:
user_case = contact.get_usercase_by_domain(self.domain)
Expand All @@ -275,11 +275,11 @@ def _passes_user_data_filter(self, contact):
actual_values_set = self.convert_to_set(user_data.get(key, ""))

if actual_values_set.isdisjoint(allowed_values_set):
return False
return False, f"Filtered out on property {key}: exp: ({','.join(allowed_values_set)}), act: ({','.join(actual_values_set)})"

Check failure on line 278 in corehq/messaging/scheduling/scheduling_partitioned/models.py

View workflow job for this annotation

GitHub Actions / Flake8

corehq/messaging/scheduling/scheduling_partitioned/models.py#L278

Line too long (140 > 115 characters) (E501)

return True
return True, ""

def expand_recipients(self):
def expand_recipients(self, log_filter=None):
"""
Can be used as a generator to iterate over all individual contacts who
are the recipients of this ScheduleInstance.
Expand All @@ -290,8 +290,11 @@ def expand_recipients(self):

for member in recipient_list:
for contact in self._expand_recipient(member):
if self._passes_user_data_filter(contact):
passed, error_msg = self._passes_user_data_filter(contact)
if passed:
yield contact
elif log_filter is not None:
log_filter(contact, error_msg)

def get_content_send_lock(self, recipient):
if is_commcarecase(recipient):
Expand Down Expand Up @@ -331,8 +334,15 @@ def send_current_event_content_to_recipients(self):

logged_event = MessagingEvent.create_from_schedule_instance(self, content)

def log_filter_error(filtered_recipient, msg):
sub_event = logged_event.create_subevent_from_contact_and_content(
filtered_recipient,
content,
)
sub_event.error(MessagingEvent.ERROR_FILTER_MISMATCH, msg)

recipient_count = 0
for recipient in self.expand_recipients():
for recipient in self.expand_recipients(log_filter_error):
recipient_count += 1

# The framework will retry sending a non-processed schedule instance
Expand Down

0 comments on commit e27e1b8

Please sign in to comment.