示例#1
0
    def get_complete_dictionary_observables(
            list_type_observables: List[str]) -> Dict[str, ObservableGeneric]:
        try:
            if not list_type_observables:
                logger.warning(
                    'UtilityCatalogCache get_complete_dictionary_observables list_type_observable is None'
                )
                return None

            dict_return = dict()

            for type_observable in list_type_observables:
                dict_observable_type = \
                    CacheRedisAdapter.dictionary_get_all(label_info=UtilityCatalogCached.get_dictionary_name(type_observable),
                                                         type_value=ObservableGeneric)
                if not dict_observable_type:
                    logger.info(
                        'UtilityCatalogCache get_complete_dictionary_observables not available for type_obs: {}'
                        .format(type_observable))
                    continue

                logger.info(
                    'UtilityCatalogCache get_complete_dictionary_observables available for type_obs: {0}, '
                    'counter_elements: {1}'.format(type_observable,
                                                   len(dict_observable_type)))

                dict_return[type_observable] = dict_observable_type

            return dict_return
        except Exception as ex:
            logger.error(
                'UtilityCatalogCache: get_complete_dictionary_observables Exception: {}'
                .format(ex))
            return None
示例#2
0
    def get_list_obstobackup() -> List[ObservableGeneric]:
        try:
            dictionary_type_observable = \
                CacheRedisAdapter.dictionary_get_all(label_info=UtilityCatalogCached.LABEL_DICTIONARY_OBSERVABLE_TO_BACKUP,
                                                     type_value=str)

            if not dictionary_type_observable:
                return None

            list_return = list()

            for type_observable in dictionary_type_observable:

                if dictionary_type_observable[type_observable] is None:
                    continue

                list_partial = CacheRedisAdapter.get_cached_info(
                    label_info=dictionary_type_observable[type_observable],
                    type_data=list)

                if not list_partial:
                    continue

                list_return.extend(list_partial)
            return list_return

        except Exception as ex:
            logger.error(
                'get_dictionary_specific_observable Exception: {}'.format(ex))
            return None
    def test_method():
        try:
            LABEL_DICTIONARY_OBSERVABLE = 'OBSERVABLE'
            LABEL_COUNTER = 'TEST_COUNTER'
            LABEL_TEST_LIST = 'TEST_LIST'
            LABEL_TEST_LISTSTRING = 'TEST_LIST_STRING'
            STRING_TRY = 'PROVA'
            LABEL_LIST_OBSERVABLE = 'TEST_LIST_OBSERVABLE'
            LABEL_DICT_OBSERVABLE = 'TEST_DICT_OBSERVABLES'
            LABEL_GOST_FILTER = 'GOST_LARGE_SCALE_TEST'

            max_counter = 3
            timestamp = datetime.datetime.now(tz=pytz.utc)

            logger.info(
                'STARTED TEST DEMO {} Observable CatalogCache Write Simulated'.
                format(max_counter))

            start_value = 4
            increase_time = 10

            CacheRedisAdapter.set_cache_info(label_info='NEW', data=0)

            byte_array = CacheRecord.dumps(STRING_TRY)
            string_return = CacheRecord.loads(byte_array=byte_array,
                                              type_object=str)

            byte_array = CacheRecord.dumps(start_value)
            compare_value = CacheRecord.loads(byte_array=byte_array,
                                              type_object=int)

            logger.info('String pass: {0}, string get: {1}'.format(
                STRING_TRY, string_return))

            observable = UnitTestsUtilityObservables.test_method_create_observable(
                label_gost=LABEL_GOST_FILTER,
                index_element=1,
                device_index=1,
                timestamp=timestamp)

            byte_array = CacheRecord.dumps(record=observable)
            observable_test = CacheRecord.loads(byte_array=byte_array,
                                                type_object=Localization)

            CacheRedisAdapter.counter_create(label_info=LABEL_COUNTER,
                                             start_value=start_value)

            for counter in range(0, increase_time):
                CacheRedisAdapter.counter_increase(label_info=LABEL_COUNTER)

            value = CacheRedisAdapter.counter_get(label_info=LABEL_COUNTER)

            if value != start_value + increase_time:
                logger.error(
                    'Unexpected counter value: {0} rather than {1}'.format(
                        value, counter + increase_time))

            list_elements = list()
            dictionary_test = dict()

            for index_element in range(0, max_counter):
                observable = UnitTestsUtilityObservables.test_method_create_observable(
                    label_gost=LABEL_GOST_FILTER,
                    index_element=index_element,
                    device_index=index_element,
                    timestamp=timestamp)
                list_elements.append(observable)
                dictionary_test[observable.get_label_cache()] = observable

            CacheRedisAdapter.set_cache_info(label_info=LABEL_LIST_OBSERVABLE,
                                             data=list_elements)

            CacheRedisAdapter.set_cache_info(label_info=LABEL_DICT_OBSERVABLE,
                                             data=dictionary_test)

            list_return = CacheRedisAdapter.get_cached_info(
                label_info=LABEL_LIST_OBSERVABLE, type_data=list)

            dict_return = CacheRedisAdapter.get_cached_info(
                label_info=LABEL_DICT_OBSERVABLE, type_data=dict)

            CacheRedisAdapter.dictionary_create(
                label_info=LABEL_DICTIONARY_OBSERVABLE)

            CacheRedisAdapter.list_create(label_info=LABEL_TEST_LISTSTRING)
            CacheRedisAdapter.list_append_singleelement(
                label_info=LABEL_TEST_LISTSTRING, elem_to_append=STRING_TRY)
            string_get = CacheRedisAdapter.list_extractfirstelement(
                label_info=LABEL_TEST_LISTSTRING, type_element=str)

            logger.info('Test Set String: {0}, Extracted: {1}'.format(
                STRING_TRY, string_get))

            for index_element in range(0, max_counter):

                if (index_element % 500) == 0:
                    logger.info(
                        'TEST EXAMPLE COUNTER: {}'.format(index_element))

                localization_test = UnitTestsUtilityObservables.test_method_create_observable(
                    label_gost=LABEL_GOST_FILTER,
                    index_element=index_element,
                    device_index=index_element,
                    timestamp=timestamp)

                CacheRedisAdapter.dictionary_update_value(
                    label_info=LABEL_DICTIONARY_OBSERVABLE,
                    key=localization_test.get_label_cache(),
                    value=localization_test)

            logger.info(
                'STARTED TEST DEMO {} Observable CatalogCache Read Simulated'.
                format(max_counter))

            dictionary_imported = CacheRedisAdapter.dictionary_get_all(
                label_info=LABEL_DICTIONARY_OBSERVABLE,
                type_value=ObservableGeneric)

            for index_element in range(0, max_counter):
                label_cache = UnitTestsUtilityObservables.\
                    test_method_get_label_cache(label_gostfilter=LABEL_GOST_FILTER,
                                                index_element=index_element)

                if label_cache not in dictionary_imported or not dictionary_imported[
                        label_cache]:
                    return False

                localization_read = dictionary_imported[label_cache]

                if not UnitTestsUtilityObservables.test_method_check_obs_validity(
                        observable=localization_read,
                        index_element=index_element,
                        timestamp=timestamp):
                    return False

                if (index_element % 500) == 0:
                    logger.info(
                        'TEST EXAMPLE COUNTER READ: {}'.format(index_element))

            logger.info(
                'END TEST DEMO {} Observable CatalogCache Read Simulated'.
                format(max_counter))

            CacheRedisAdapter.list_create(label_info=LABEL_TEST_LIST)

            for index_element in range(0, max_counter):
                localiz_to_append = UnitTestsUtilityObservables.test_method_create_observable(
                    label_gost=LABEL_GOST_FILTER,
                    index_element=index_element,
                    device_index=index_element,
                    timestamp=timestamp)
                CacheRedisAdapter.list_append_singleelement(
                    label_info=LABEL_TEST_LIST,
                    elem_to_append=localiz_to_append)

            index_element = 0
            while CacheRedisAdapter.list_getcounterelements(
                    label_info=LABEL_TEST_LIST) > 0:
                localiz_extracted = CacheRedisAdapter.list_extractfirstelement(
                    label_info=LABEL_TEST_LIST, type_element=Localization)

                if not UnitTestsUtilityObservables.test_method_check_obs_validity(
                        observable=localiz_extracted,
                        index_element=index_element,
                        timestamp=timestamp):
                    return False

                index_element += 1

            logger.info(
                'END TEST DEMO {} Observable CatalogCache List Extract'.format(
                    max_counter))

            return True
        except Exception as ex:
            logger.error(
                'UnitTestCacheRedis test_method Exception: {}'.format(ex))
            return False