scout/parse/matchmaker.py
def parse_matches(patient_id, match_objs):
    """Parse a list of matchmaker matches objects and returns
       a readable list of matches to display in matchmaker matches view.

    Args:
        patient_id(str): id of a mme patient
        match_objs(list): list of match objs returned by MME server for the patient
                # match_objs looks like this:
                    [
                        {
                            'node' : { id : node_id , label: node_label},
                            'patients' : [
                                { 'patient': {patient1_data} },
                                { 'patient': {patient2_data} },
                                ..
                            ]
                        },
                        ..
                    ]

    Returns:
        parsed_matches(list): a list of parsed match objects

    """
    LOG.info('Parsing MatchMaker matches for patient {}'.format(patient_id))
    parsed_matches = []

    for match_obj in match_objs:

        # convert match date from millisecond to readable date
        milliseconds_date = match_obj['created']['$date']
        mdate = datetime.datetime.fromtimestamp(milliseconds_date/1000.0)
        match_type = 'external'
        matching_patients = []

        parsed_match = {
            'match_oid' : match_obj['_id']['$oid'],# save match object ID
            'match_date' : mdate
        }

        # if patient was used as query patient:
        if match_obj['data']['patient']['id'] == patient_id:
            match_results = match_obj['results'] # List of matching patients
            for node_result in match_results:
                if match_obj['match_type'] == 'internal':
                    match_type = 'internal'

                for patient in node_result['patients']:
                    match_patient = {
                        'patient_id' : patient['patient']['id'],
                        'score' : patient['score'],
                        'patient' : patient['patient'],
                        'node' : node_result['node']
                    }
                    matching_patients.append(match_patient)

        else: # else if patient was returned as a match result for another patient
            m_patient = match_obj['data']['patient']
            contact_institution = m_patient['contact'].get('institution')
            if contact_institution and 'Scout software user' in contact_institution:
                match_type = 'internal'

            # loop over match results to capture score for matching
            score = None
            for res in match_obj['results']:
                for patient in res['patients']:
                    LOG.info('Looping in else, patient:{}'.format(patient['patient']['id']))
                    if patient['patient']['id'] == patient_id:

                        score = patient['score']
                        match_patient = {
                            'patient_id' : m_patient['id'],
                            'score' : score,
                            'patient' : m_patient,
                            'node' : res['node']
                        }
                        matching_patients.append(match_patient)

        parsed_match['match_type'] = match_type
        parsed_match['patients'] = matching_patients
        parsed_matches.append(parsed_match)

    # sort results by descending score
    parsed_matches = sorted(parsed_matches, key=lambda k: k['match_date'], reverse=True)
    return parsed_matches
def parse_matches(patient_id, match_objs):
    """Parse a list of matchmaker matches objects and returns
       a readable list of matches to display in matchmaker matches view.

    Args:
        patient_id(str): id of a mme patient
        match_objs(list): list of match objs returned by MME server for the patient
                # match_objs looks like this:
                    [
                        {
                            'node' : { id : node_id , label: node_label},
                            'patients' : [
                                { 'patient': {patient1_data} },
                                { 'patient': {patient2_data} },
                                ..
                            ]
                        },
                        ..
                    ]

    Returns:
        parsed_matches(list): a list of parsed match objects

    """
    LOG.info('Parsing MatchMaker matches for patient {}'.format(patient_id))
    parsed_matches = []

    for match_obj in match_objs:

        # convert match date from millisecond to readable date
        milliseconds_date = match_obj['created']['$date']
        mdate = datetime.datetime.fromtimestamp(milliseconds_date/1000.0)
        match_type = 'external'
        matching_patients = []

        parsed_match = {
            'match_oid' : match_obj['_id']['$oid'],# save match object ID
            'match_date' : mdate
        }

        # if patient was used as query patient:
        if match_obj['data']['patient']['id'] == patient_id:
            match_results = match_obj['results'] # List of matching patients
            for node_result in match_results:
                if match_obj['match_type'] == 'internal':
                    match_type = 'internal'

                for patient in node_result['patients']:
                    match_patient = {
                        'patient_id' : patient['patient']['id'],
                        'score' : patient['score'],
                        'patient' : patient['patient'],
                        'node' : node_result['node']
                    }
                    matching_patients.append(match_patient)

        else: # else if patient was returned as a match result for another patient
            m_patient = match_obj['data']['patient']
            contact_institution = m_patient['contact'].get('institution')
            if contact_institution and 'Scout software user' in contact_institution:
                match_type = 'internal'

            # loop over match results to capture score for matching
            score = None
            for res in match_obj['results']:
                for patient in res['patients']:
                    LOG.info('Looping in else, patient:{}'.format(patient['patient']['id']))
                    if patient['patient']['id'] == patient_id:

                        score = patient['score']
                        match_patient = {
                            'patient_id' : m_patient['id'],
                            'score' : score,
                            'patient' : m_patient,
                            'node' : res['node']
                        }
                        matching_patients.append(match_patient)

        parsed_match['match_type'] = match_type
        parsed_match['patients'] = matching_patients
        parsed_matches.append(parsed_match)

    # sort results by descending score
    parsed_matches = sorted(parsed_matches, key=lambda k: k['match_date'], reverse=True)
    return parsed_matches
Parse a list of matchmaker matches objects and returns
       a readable list of matches to display in matchmaker matches view.

    Args:
        patient_id(str): id of a mme patient
        match_objs(list): list of match objs returned by MME server for the patient
                # match_objs looks like this:
                    [
                        {
                            'node' : { id : node_id , label: node_label},
                            'patients' : [
                                { 'patient': {patient1_data} },
                                { 'patient': {patient2_data} },
                                ..
                            ]
                        },
                        ..
                    ]

    Returns:
        parsed_matches(list): a list of parsed match objects