def build_transfer(**kwargs) -> Transfer: return Transfer( conversation_id=kwargs.get("conversation_id", a_string(36)), sla_duration=kwargs.get("sla_duration", a_duration()), requesting_practice=kwargs.get( "requesting_practice", Practice(asid=a_string(12), supplier=a_string(12), ods_code=a_string(4)), ), sending_practice=kwargs.get( "sending_practice", Practice(asid=a_string(12), supplier=a_string(12), ods_code=a_string(4)), ), sender_error_codes=kwargs.get("sender_error_codes", []), final_error_codes=kwargs.get("final_error_codes", []), intermediate_error_codes=kwargs.get("intermediate_error_codes", []), outcome=kwargs.get( "outcome", TransferOutcome(status=TransferStatus.INTEGRATED_ON_TIME, failure_reason=None), ), date_requested=kwargs.get("date_requested", a_datetime()), date_completed=kwargs.get("date_completed", None), last_sender_message_timestamp=None, )
def derive_transfer( self, conversation: Gp2gpConversation, organisation_lookup: OrganisationLookup ) -> Transfer: sla_duration = _calculate_sla(conversation, self._probe) requesting_practice_asid = conversation.requesting_practice_asid() sending_practice_asid = conversation.sending_practice_asid() return Transfer( conversation_id=conversation.conversation_id(), sla_duration=sla_duration, requesting_practice=Practice( asid=requesting_practice_asid, supplier=conversation.requesting_supplier(), ods_code=organisation_lookup.practice_ods_code_from_asid(requesting_practice_asid), ), sending_practice=Practice( asid=sending_practice_asid, supplier=conversation.sending_supplier(), ods_code=organisation_lookup.practice_ods_code_from_asid(sending_practice_asid), ), sender_error_codes=conversation.sender_error_codes(), final_error_codes=conversation.final_error_codes(), intermediate_error_codes=conversation.intermediate_error_codes(), outcome=TransferOutcome.from_gp2gp_conversation(conversation, sla_duration), date_requested=conversation.date_requested(), date_completed=conversation.effective_final_acknowledgement_time(), last_sender_message_timestamp=conversation.last_sender_message_timestamp(), )
def test_parses_transfer_correctly_given_valid_message_list(): time_range = DateTimeRange(start=datetime(2019, 12, 1, tzinfo=UTC), end=datetime(2020, 1, 1, tzinfo=UTC)) requesting_asid_with_transfer = "343434343434" sending_asid_with_transfer = "111134343434" requesting_supplier = "EMIS" sending_supplier = "Vision" conversation_id = "abcdefg_1234" spine_messages = _build_successful_conversation( conversation_id=conversation_id, requesting_asid=requesting_asid_with_transfer, sending_asid=sending_asid_with_transfer, requesting_supplier=requesting_supplier, sending_supplier=sending_supplier, ehr_request_started_on=datetime(2019, 12, 30, 18, 2, 29, tzinfo=UTC), ehr_request_completed_on=datetime(2019, 12, 30, 18, 3, 21, tzinfo=UTC), ehr_request_started_acknowledged_on=datetime(2019, 12, 30, 18, 3, 23, tzinfo=UTC), ehr_request_completed_acknowledged_on=datetime(2020, 1, 1, 8, 41, 48, tzinfo=UTC), ) expected = [ Transfer( conversation_id=conversation_id, sla_duration=timedelta(days=1, seconds=52707), requesting_practice_asid=requesting_asid_with_transfer, sending_practice_asid=sending_asid_with_transfer, requesting_supplier=requesting_supplier, sending_supplier=sending_supplier, status=TransferStatus.INTEGRATED, date_requested=datetime(2019, 12, 30, 18, 2, 29, tzinfo=UTC), date_completed=datetime(2020, 1, 1, 8, 41, 48, tzinfo=UTC), sender_error_code=None, final_error_code=None, intermediate_error_codes=[], ) ] actual = list(parse_transfers_from_messages(spine_messages, time_range)) assert actual == expected
def test_write_transfers_correctly_writes_all_fields(): mock_s3 = MockS3() s3_data_manager = S3DataManager(mock_s3) io = TransferClassifierIO(s3_data_manager) transfer = Transfer( conversation_id="1234", sla_duration=timedelta(days=1), requesting_practice=Practice(asid="123", supplier="Supplier A", ods_code="A12"), sending_practice=Practice(asid="456", supplier="Supplier B", ods_code="B12"), sender_error_codes=[1, None], final_error_codes=[None, 32], intermediate_error_codes=[], outcome=TransferOutcome( status=TransferStatus.PROCESS_FAILURE, failure_reason=TransferFailureReason.FINAL_ERROR), date_requested=datetime(year=2021, month=3, day=5), date_completed=None, last_sender_message_timestamp=None, ) io.write_transfers(transfers=[transfer], s3_uri="s3://a_bucket/some_data.parquet", metadata=_SOME_METADATA) expected_table = { "conversation_id": ["1234"], "sla_duration": [86400], "requesting_practice_asid": ["123"], "requesting_practice_ods_code": ["A12"], "sending_practice_asid": ["456"], "sending_practice_ods_code": ["B12"], "requesting_supplier": ["Supplier A"], "sending_supplier": ["Supplier B"], "sender_error_codes": [[1, None]], "final_error_codes": [[None, 32]], "intermediate_error_codes": [[]], "status": ["Process failure"], "failure_reason": ["Final error"], "date_requested": [datetime(year=2021, month=3, day=5)], "date_completed": [None], "last_sender_message_timestamp": [None], } actual_table = mock_s3.object( "a_bucket", "some_data.parquet").read_parquet().to_pydict() assert actual_table == expected_table
def build_transfer(**kwargs): return Transfer( conversation_id=kwargs.get("conversation_id", a_string(36)), sla_duration=kwargs.get("sla_duration", a_duration()), requesting_practice_asid=kwargs.get("requesting_practice_asid", a_string(12)), sending_practice_asid=kwargs.get("sending_practice_asid", a_string(12)), requesting_supplier=kwargs.get("requesting_supplier", a_string(12)), sending_supplier=kwargs.get("sending_supplier", a_string(12)), sender_error_code=kwargs.get("sender_error_code", None), final_error_code=kwargs.get("final_error_code", None), intermediate_error_codes=kwargs.get("intermediate_error_codes", []), status=kwargs.get("status", TransferStatus.PENDING), date_requested=kwargs.get("date_requested", a_datetime()), date_completed=kwargs.get("date_completed", None), )
def test_includes_suppressed_transfers(): suppressed_transfer = build_transfer( conversation_id="456", sla_duration=timedelta(hours=2), requesting_practice_asid="121212121212", sending_practice_asid="343434343434", requesting_supplier="Vision", sending_supplier="SystemOne", final_error_code=15, status=TransferStatus.INTEGRATED, date_requested=date_requested, date_completed=date_completed, ) transfers = [suppressed_transfer] actual = filter_for_successful_transfers(transfers) expected = [ Transfer( conversation_id="456", sla_duration=timedelta(hours=2), requesting_practice_asid="121212121212", sending_practice_asid="343434343434", requesting_supplier="Vision", sending_supplier="SystemOne", sender_error_code=None, final_error_code=15, intermediate_error_codes=[], status=TransferStatus.INTEGRATED, date_requested=date_requested, date_completed=date_completed, ) ] assert list(actual) == expected
def test_calculates_correct_metrics_given_a_successful_transfer(): time_range = DateTimeRange(start=datetime(2019, 12, 1, tzinfo=UTC), end=datetime(2020, 1, 1, tzinfo=UTC)) requesting_practice_name = "Test GP" requesting_ods_code = "A12345" requesting_asid_with_transfer = "343434343434" sending_asid_with_transfer = "111134343434" requesting_supplier = "SystemOne" sending_supplier = "Unknown" conversation_id = "abcdefg_1234" transfers = [ Transfer( conversation_id=conversation_id, sla_duration=timedelta(days=1, seconds=52707), requesting_practice_asid=requesting_asid_with_transfer, sending_practice_asid=sending_asid_with_transfer, requesting_supplier=requesting_supplier, sending_supplier=sending_supplier, status=TransferStatus.INTEGRATED, date_requested=datetime(2019, 12, 30, 18, 2, 29, tzinfo=UTC), date_completed=datetime(2020, 1, 1, 8, 41, 48, tzinfo=UTC), sender_error_code=None, final_error_code=None, intermediate_error_codes=[], ) ] practice_list = [ PracticeDetails( asids=[requesting_asid_with_transfer], ods_code=requesting_ods_code, name=requesting_practice_name, ) ] expected = PracticeMetricsPresentation( generated_on=datetime(year=2020, month=1, day=15, hour=23, second=42, tzinfo=UTC), practices=[ PracticeSummary( ods_code=requesting_ods_code, name=requesting_practice_name, metrics=[ MonthlyMetrics( year=2019, month=12, requester=RequesterMetrics( integrated=IntegratedPracticeMetrics( transfer_count=1, within_3_days_percentage=100, within_8_days_percentage=0, beyond_8_days_percentage=0, ), ), ) ], ) ], ) actual = calculate_practice_metrics_data(transfers, practice_list, time_range) assert actual == expected