Exemple #1
0
def resolve_entities(reference_list: List[Dict], entity_cache: Dict = None):
    """
    Utility function to fetch entities (assets, countries, etc.). Allows us to split functionality that requires data
    fetching.
    :param reference_list: A list of entity references (entityId and entityType dictionaries)
    :param entity_cache: Map of entity id to the entity for external cache management
    :return: None
    """
    entity_cache = entity_cache or {}
    for reference in reference_list:
        # Check if the entity is in the cache
        entity_id = reference.get(ENTITY_ID)
        if entity_id in entity_cache:
            entity = entity_cache[entity_id]
        else:
            try:
                entity = Entity.get(entity_id, 'MQID', reference.get(ENTITY_TYPE))
            except MqRequestError as e:
                _logger.warning(e)
                entity = entity_id

        if reference[TYPE] == DATA_ROW:
            # If the reference is for a data row, simply set the entity of the row.
            reference[REFERENCE].entity = entity
        elif reference[TYPE] == PROCESSOR:
            # If the reference is for a processor, set the given parameter as the entity.
            setattr(reference[REFERENCE], reference[PARAMETER], entity)
            data_query_info = reference[REFERENCE].children.get(reference[PARAMETER])
            if not data_query_info:
                raise MqValueError(
                    f'{reference[PARAMETER]} does not exist in children of '
                    f'{reference[REFERENCE].__class__.__name__}')
            data_query_info.entity = entity
Exemple #2
0
def resolve_entities(reference_list: List[Dict]):
    """
    Utility function to fetch entities (assets, countries, etc.). Allows us to split functionality that requires data
    fetching.
    :param reference_list: A list of entity references (entityId and entityType dictionaries)
    :return: None
    """
    entity_cache = {}
    for reference in reference_list:
        # Create a hash key of the entity data so we don't fetch the same entity multiple times.
        key = hash((reference.get(ENTITY_ID), reference.get(ENTITY_TYPE)))
        if key in entity_cache:
            entity = entity_cache[key]
        else:
            entity = Entity.get(reference.get(ENTITY_ID), 'MQID',
                                reference.get(ENTITY_TYPE))

        if reference[TYPE] == DATA_ROW:
            # If the reference is for a data row, simply set the entity of the row.
            reference[REFERENCE].entity = entity
        elif reference[TYPE] == PROCESSOR:
            # If the reference is for a processor, set the given parameter as the entity.
            setattr(reference[REFERENCE], reference[PARAMETER], entity)
            data_query_info = reference[REFERENCE].children.get(
                reference[PARAMETER])
            if not data_query_info:
                raise MqValueError(
                    f'{reference[PARAMETER]} does not exist in children of '
                    f'{reference[REFERENCE].__class__.__name__}')
            data_query_info.entity = entity