def flat_to_tree_dict_helper_test(self): data = {'t': 1, 'count.test': 2, 'second.test': 4, 'more.tests': 5} formated_data = flat_to_tree_dict_helper(data) eq_( formated_data, { 'count': { 'test': 2 }, 'second': { 'test': 4 }, 't': 1, 'more': { 'tests': 5 } }) data = { 't': 1, 'count.test': 2, 'count.test.testmore': 422, 'second.test': 4, 'more.tests': 5 } formated_data = flat_to_tree_dict_helper(data) eq_( formated_data, { 'count': { 'test': 2, 'test_testmore': 422 }, 'second': { 'test': 4 }, 't': 1, 'more': { 'tests': 5 } }) data = {"name.first.name": 'Martin', "name.last.name": "Rusev"} formated_data = flat_to_tree_dict_helper(data) eq_(formated_data, {'name': { 'first_name': 'Martin', 'last_name': 'Rusev' }})
def get_counters_test(self): self._cleanup() plugin = plugin_model.get_or_create(name='test1', server_id=self.server['_id']) counters_collection = plugin_model._get_counters_collection() data = {'t': 1, 'count.test': 2, 'second.test': 4, 'more.tests': 5} formated_data = flat_to_tree_dict_helper(data) formated_data['plugin_id'] = plugin['_id'] formated_data['server_id'] = self.server_id counters_collection.insert(formated_data) result = plugin_model.get_counters( server=self.server, plugin=plugin) # Excludes t and plugin_id eq_( result, { u'count': { u'test': 2 }, u'second': { u'test': 4 }, u't': 1, u'more': { u'tests': 5 } }) self._cleanup()
def save_gauges(self, data=None, plugin=None, time=None, expires_at=None): if data is None: return formated_data = flat_to_tree_dict_helper(data) collection = self._get_gauges_data_collection() excluded_keys = ['_id', 't', 'gauge_id', 'expires_at'] for key, value in formated_data.items(): if key not in excluded_keys: gauge_data = {} gauge_keys = [] if type(value) is dict: gauge_keys = list(value.keys()) # Python 3.5 gauge = self.get_or_create_gauge_by_name(plugin=plugin, name=key, keys=gauge_keys) if type(value) is dict: gauge_data.update(value) # Multiple keys {"read":1, "write":3, 'waiting': 5} else: gauge_data['value'] = value # Just the value, : 1 gauge_data['t'] = time gauge_data['gauge_id'] = gauge['_id'] gauge_data['expires_at'] = expires_at collection.insert(gauge_data) collection.ensure_index([('gauge_id', self.desc)], background=True) collection.ensure_index([('t', self.desc)], background=True) collection.ensure_index([('expires_at', 1)], expireAfterSeconds=0)
def get_check_for_timestamp_test(self): self._cleanup() gauges_collection = plugin_model._get_gauges_data_collection() plugin = plugin_model.get_or_create(name='test1', server_id=self.server['_id']) plugin_with_error = plugin_model.get_or_create(name='error1', server_id=self.server['_id']) for i in range(0, 100): data = {'t': i, 'count.first': 2, 'count.second': 4, 'count.third': 5 } formated_data = flat_to_tree_dict_helper(data) formated_data['plugin_id'] = plugin['_id'] formated_data['server_id'] = self.server_id gauges_collection.insert(formated_data) error_data = {'plugin_id': plugin_with_error['_id'], 'server_id': self.server_id, 't': 10, 'error': 'Argghh'} error_collection = plugin_model._get_error_collection() error_collection.insert(error_data) result = plugin_model.get_check_for_timestamp(self.server, 10) for r in result: assert r['name'] in ['test1', 'error1'] self._cleanup()
def flat_to_tree_dict_helper_test(self): data = {'t': 1, 'count.test': 2, 'second.test': 4, 'more.tests': 5} formated_data = flat_to_tree_dict_helper(data) eq_(formated_data, {'count': {'test': 2}, 'second': {'test': 4}, 't': 1, 'more': {'tests': 5}}) data = {'t': 1, 'count.test': 2, 'count.test.testmore': 422, 'second.test': 4, 'more.tests': 5} formated_data = flat_to_tree_dict_helper(data) eq_(formated_data, {'count': {'test': 2, 'test_testmore': 422}, 'second': {'test': 4}, 't': 1, 'more': {'tests': 5}}) data = {"name.first.name": 'Martin', "name.last.name": "Rusev"} formated_data = flat_to_tree_dict_helper(data) eq_(formated_data, {'name': {'first_name': 'Martin', 'last_name': 'Rusev'}})
def upsert_gauges(self, plugin=None, gauges=None): # Example data - {'t': 1, 'count.test': 2, 'second.test': 4, 'more.tests': 5 } formated_data = flat_to_tree_dict_helper(gauges) excluded_keys = ['_id', 't', 'plugin_id', 'server_id','expires_at'] filtered_gauges = list((key) for key in formated_data.keys() if key not in excluded_keys) for g in filtered_gauges: gauge_params = {'name': g, 'plugin_id': plugin['_id']} result = self.gauge_collection.find_one(gauge_params) if result == None: self.gauge_collection.insert(gauge_params)
def get_counters_test(self): self._cleanup() plugin = plugin_model.get_or_create(name='test1', server_id=self.server['_id']) counters_collection = plugin_model._get_counters_collection() data = {'t': 1, 'count.test': 2, 'second.test': 4, 'more.tests': 5 } formated_data = flat_to_tree_dict_helper(data) formated_data['plugin_id'] = plugin['_id'] formated_data['server_id'] = self.server_id counters_collection.insert(formated_data) result = plugin_model.get_counters(server=self.server, plugin=plugin) # Excludes t and plugin_id eq_(result, {u'count': {u'test': 2}, u'second': {u'test': 4}, u't': 1, u'more': {u'tests': 5}}) self._cleanup()
def save_counters(self, data=None, server=None, plugin=None, time=None): if data is None: return formated_data = flat_to_tree_dict_helper(data) collection = self._get_counters_collection() if len(formated_data) > 0: params = {'plugin_id': plugin['_id'], 'server_id': server['_id']} collection.remove(params) formated_data['plugin_id'] = plugin['_id'] formated_data['server_id'] = server['_id'] formated_data['t'] = time if time else unix_utc_now() collection.insert(formated_data) collection.ensure_index([('server_id', self.desc)], background=True) collection.ensure_index([('plugin_id', self.desc)], background=True)
def get_check_for_timestamp_test(self): self._cleanup() gauges_collection = plugin_model._get_gauges_data_collection() plugin = plugin_model.get_or_create(name='test1', server_id=self.server['_id']) plugin_with_error = plugin_model.get_or_create( name='error1', server_id=self.server['_id']) for i in range(0, 100): data = { 't': i, 'count.first': 2, 'count.second': 4, 'count.third': 5 } formated_data = flat_to_tree_dict_helper(data) formated_data['plugin_id'] = plugin['_id'] formated_data['server_id'] = self.server_id gauges_collection.insert(formated_data) error_data = { 'plugin_id': plugin_with_error['_id'], 'server_id': self.server_id, 't': 10, 'error': 'Argghh' } error_collection = plugin_model._get_error_collection() error_collection.insert(error_data) result = plugin_model.get_check_for_timestamp(self.server, 10) for r in result: assert r['name'] in ['test1', 'error1'] self._cleanup()