def test_sorts_messages_within_conversations(): message_one = build_message(conversation_id="abc", time=datetime(year=2020, month=6, day=6)) message_two = build_message(conversation_id="abc", time=datetime(year=2020, month=6, day=5)) messages = [message_one, message_two] expected = [Conversation("abc", [message_two, message_one])] transfer_service = TransferService( message_stream=messages, cutoff=timedelta(days=14), observability_probe=mock_transfer_observability_probe, ) actual = transfer_service.group_into_conversations() assert list(actual) == expected
def test_produces_correct_conversations(): message_one = build_message(conversation_id="abc") message_two = build_message(conversation_id="xyz") messages = [message_one, message_two] expected = [Conversation("abc", [message_one]), Conversation("xyz", [message_two])] transfer_service = TransferService( message_stream=messages, cutoff=timedelta(days=14), observability_probe=mock_transfer_observability_probe, ) actual = transfer_service.group_into_conversations() assert list(actual) == expected
def test_rejects_messages_after_cutoff(cutoff_interval, expected_message_ids): messages = [ build_message(conversation_id="a", guid="1", time=datetime(year=2020, month=6, day=6)), build_message(conversation_id="a", guid="2", time=datetime(year=2020, month=6, day=7)), build_message(conversation_id="a", guid="3", time=datetime(year=2020, month=6, day=8)), ] transfer_service = TransferService( message_stream=messages, cutoff=cutoff_interval, observability_probe=mock_transfer_observability_probe, ) conversations = transfer_service.group_into_conversations() actual_message_ids = [m.guid for m in next(conversations).messages] assert actual_message_ids == expected_message_ids
def run(self): transfer_observability_probe = TransferObservabilityProbe( logger=module_logger) log_date_range_info = self._construct_json_log_date_range_info() logger.info( "Attempting to classify conversations for a date range", extra={ "event": "ATTEMPTING_CLASSIFY_CONVERSATIONS_FOR_A_DATE_RANGE", **log_date_range_info, }, ) spine_messages = self._read_spine_messages() ods_metadata_monthly = self._read_ods_metadata() transfer_service = TransferService( message_stream=spine_messages, cutoff=self._config.conversation_cutoff, observability_probe=transfer_observability_probe, ) conversations = transfer_service.group_into_conversations() gp2gp_conversations = transfer_service.parse_conversations_into_gp2gp_conversations( conversations) for daily_start_datetime in self._reporting_window.get_dates(): metadata = { "cutoff-days": str(self._config.conversation_cutoff.days), "build-tag": self._config.build_tag, "start-datetime": convert_to_datetime_string(daily_start_datetime), "end-datetime": convert_to_datetime_string(daily_start_datetime + timedelta(days=1)), "ods-metadata-month": f"{daily_start_datetime.year}-{daily_start_datetime.month}", } conversations_started_in_reporting_window = filter_conversations_by_day( gp2gp_conversations, daily_start_datetime) organisation_lookup = ods_metadata_monthly.get_lookup( (daily_start_datetime.year, daily_start_datetime.month)) transfers = transfer_service.convert_to_transfers( conversations_started_in_reporting_window, organisation_lookup=organisation_lookup) self._write_transfers( transfers=transfers, daily_start_datetime=daily_start_datetime, cutoff=self._config.conversation_cutoff, metadata=metadata, ) logger.info( "Successfully classified conversations for a date range", extra={ "event": "CLASSIFIED_CONVERSATIONS_FOR_A_DATE_RANGE", **log_date_range_info, }, )