def test_deviceagents(self): """Testing method / function db_devagt_get_all_device_agents.""" # Clear the memory cache CACHE.clear() # Get results response = self.API.get('/infoset/api/v1/deviceagents') data = json.loads(response.get_data(as_text=True)) result = data[0] # Verify reponse code self.assertEqual(response.status_code, 200) # Verify response content self.assertEqual(isinstance(data, list), True) self.assertEqual(result['idx_device'], self.test_object.idx_device()) self.assertEqual(result['exists'], self.test_object.exists()) self.assertEqual(result['enabled'], self.test_object.enabled()) self.assertEqual(result['idx_agent'], self.test_object.idx_agent()) self.assertEqual(result['last_timestamp'], self.test_object.last_timestamp()) self.assertEqual(result['idx_deviceagent'], self.test_object.idx_deviceagent()) # Test the number and names of keys keys = [ 'last_timestamp', 'idx_deviceagent', 'idx_agent', 'idx_device', 'enabled', 'exists' ] self.assertEqual(len(result), len(keys)) for key in keys: self.assertEqual(key in result, True)
def test_getdata_timestamp(self): """Testing method / function getdata.""" # Clear the memory cache CACHE.clear() # Initialize key variables precision = 5 ts_stop = int(datetime.utcnow().timestamp()) ts_start = ts_stop - 3600 # Get results for up to 1/4 hour ago - No data should be present uri = ('/infoset/api/v1/datapoints/{}/data?ts_start={}&ts_stop={}' ''.format(self.expected['idx_datapoint'], ts_start, ts_stop)) response = self.API.get(uri) result = json.loads(response.get_data(as_text=True)) # Convert the list of expected values to an easy to compare dict check_dict = {} for item in self.expected['values']: check_dict[str(item['timestamp'])] = item['value'] # Verify the expected data is there for timestamp, value in sorted(result.items()): if timestamp in check_dict: self.assertEqual(round(result[timestamp], precision), round(check_dict[timestamp], precision)) else: self.assertEqual(value, 0)
def test_deviceagents_query(self): """Testing method / function db_getidxdeviceagent.""" # Clear the memory cache CACHE.clear() # Get results uri = ('/infoset/api/v1/deviceagents/{}' ''.format(self.expected['idx_deviceagent'])) response = self.API.get(uri) result = json.loads(response.get_data(as_text=True)) # Verify reponse code self.assertEqual(response.status_code, 200) # Verify response content self.assertEqual(isinstance(result, dict), True) self.assertEqual(result['idx_device'], self.test_object.idx_device()) self.assertEqual(result['exists'], self.test_object.exists()) self.assertEqual(result['enabled'], self.test_object.enabled()) self.assertEqual(result['idx_agent'], self.test_object.idx_agent()) self.assertEqual(result['last_timestamp'], self.test_object.last_timestamp()) self.assertEqual(result['idx_deviceagent'], self.test_object.idx_deviceagent()) # Test the number and names of keys keys = [ 'last_timestamp', 'idx_deviceagent', 'idx_agent', 'idx_device', 'enabled', 'exists' ] self.assertEqual(len(result), len(keys)) for key in keys: self.assertEqual(key in result, True)
def db_getidxdeviceagent(value): """Get DeviceAgent data from the DB by idx value. Args: value: idx_deviceagent Returns: data: JSON data for the selected deviceagent """ # Initialize key variables idx_deviceagent = int(value) # Get data from cache key = ('DB/DeviceAgent/idx_deviceagent/{}'.format(idx_deviceagent)) cache_value = CACHE.get(key) # Process cache miss if cache_value is None: query = db_deviceagent.GetIDXDeviceAgent(idx_deviceagent) data = query.everything() CACHE.set(key, data) else: data = cache_value # Return return jsonify(data)
def db_deviceagent_agentindices(value): """Get all agent indices from the DB. Args: idx_device: Index value of device Returns: List of agent indices reporting on the device """ # Initialize key variables idx_device = int(value) # Get data from cache key = ('DB/Device/idx_device/{}/agents'.format(idx_device)) cache_value = CACHE.get(key) # Process cache miss if cache_value is None: data = db_deviceagent.agent_indices(idx_device) CACHE.set(key, data) else: data = cache_value # Return return jsonify(data)
def db_getidxdevice(value): """Get device data from the DB by idx value. Args: value: Index from the Device table Returns: Home Page """ # Initialize key variables idx_device = int(value) # Get data from cache key = ('DB/Device/idx_device/{}'.format(idx_device)) cache_value = CACHE.get(key) # Process cache miss if cache_value is None: query = db_device.GetIDXDevice(idx_device) data = query.everything() CACHE.set(key, data) else: data = cache_value # Return return jsonify(data)
def test_agents_query_2(self): """Testing method / function agents_query.""" # Clear the memory cache CACHE.clear() # Get results uri = ('/infoset/api/v1/agents?id_agent={}' ''.format(self.expected['id_agent'])) response = self.API.get(uri) data = json.loads(response.get_data(as_text=True)) result = data[0] # Verify reponse code self.assertEqual(response.status_code, 200) # Verify response content self.assertEqual(isinstance(data, list), True) self.assertEqual(result['id_agent'], self.test_object.id_agent()) self.assertEqual(result['exists'], self.test_object.exists()) self.assertEqual(result['enabled'], self.test_object.enabled()) self.assertEqual(result['agent'], self.test_object.agent()) # Test the number and names of keys keys = [ 'idx_agent', 'idx_agentname', 'id_agent', 'enabled', 'agent', 'exists' ] self.assertEqual(len(result), len(keys)) for key in keys: self.assertEqual(key in result, True)
def test_datapoints(self): """Testing method / function datapoints.""" # Clear the memory cache CACHE.clear() # Get results uri = ('/infoset/api/v1/datapoints/{}'.format( self.expected['idx_datapoint'])) response = self.API.get(uri) result = json.loads(response.get_data(as_text=True)) # Verify reponse code self.assertEqual(response.status_code, 200) # Verify response content self.assertEqual(isinstance(result, dict), True) self.assertEqual(result['idx_datapoint'], self.test_object.idx_datapoint()) self.assertEqual(result['id_datapoint'], self.test_object.id_datapoint()) self.assertEqual(result['idx_deviceagent'], self.test_object.idx_deviceagent()) self.assertEqual(result['idx_department'], self.test_object.idx_department()) self.assertEqual(result['idx_billcode'], self.test_object.idx_billcode()) self.assertEqual(result['agent_label'], self.test_object.agent_label()) self.assertEqual(result['agent_source'], self.test_object.agent_source()) self.assertEqual(result['billable'], self.test_object.billable()) self.assertEqual(result['base_type'], self.test_object.base_type()) self.assertEqual(result['timefixed_value'], self.test_object.timefixed_value()) self.assertEqual(result['last_timestamp'], self.test_object.last_timestamp()) self.assertEqual(result['exists'], self.test_object.exists()) self.assertEqual(result['enabled'], self.test_object.enabled()) # Test the number and names of keys keys = [ 'idx_datapoint', 'id_datapoint', 'idx_deviceagent', 'idx_department', 'idx_billcode', 'agent_label', 'agent_source', 'enabled', 'billable', 'base_type', 'timefixed_value', 'last_timestamp', 'exists' ] self.assertEqual(len(result), len(keys)) for key in keys: self.assertEqual(key in result, True)
def test_getdata_secondsago_1(self): """Testing method / function getdata.""" # Clear the memory cache CACHE.clear() # Initialize key variables precision = 5 ####################################################################### # Try an hour ago ####################################################################### # Get results for up to an hour ago uri = ('/infoset/api/v1/datapoints/{}/data?secondsago=3600' ''.format(self.expected['idx_datapoint'])) response = self.API.get(uri) result = json.loads(response.get_data(as_text=True)) # Convert the list of expected values to an easy to compare dict check_dict = {} for item in self.expected['values']: check_dict[str(item['timestamp'])] = item['value'] # Verify the expected data is there for timestamp, value in sorted(result.items()): if timestamp in check_dict: self.assertEqual(round(result[timestamp], precision), round(check_dict[timestamp], precision)) else: self.assertEqual(value, 0) ####################################################################### # Try an 1/4 ago. No data should be present ####################################################################### # Clear the memory cache CACHE.clear() # Get results uri = ('/infoset/api/v1/datapoints/{}/data?secondsago=900' ''.format(self.expected['idx_datapoint'])) response = self.API.get(uri) result = json.loads(response.get_data(as_text=True)) # Go through the results for timestamp, value in sorted(result.items()): self.assertEqual(result[timestamp], 0) self.assertEqual(value, 0)
def test_db_datapoint_summary(self): """Testing method / function db_datapoint_summary.""" # Clear the memory cache CACHE.clear() # Get results uri = '/infoset/api/v1/datapoints/all/summary' response = self.API.get(uri) data = json.loads(response.get_data(as_text=True)) # Verify reponse code self.assertEqual(response.status_code, 200) # Verify response content self.assertEqual(isinstance(data, dict), True) self.assertEqual(len(data), 1) for idx_datapoint, result in data.items(): self.assertEqual(int(idx_datapoint), self.test_object.idx_datapoint()) self.assertEqual(result['idx_deviceagent'], self.test_object.idx_deviceagent()) self.assertEqual(result['agent_label'], self.test_object.agent_label()) self.assertEqual(result['agent_source'], self.test_object.agent_source()) # Get remaining values self.assertEqual(result['devicename'], self.expected['devicename']) self.assertEqual(result['id_agent'], self.expected['id_agent']) self.assertEqual(result['agent'], self.expected['agent']) # Test the number and names of keys keys = [ 'idx_deviceagent', 'agent_label', 'agent_source', 'devicename', 'id_agent', 'agent' ] self.assertEqual(len(result), len(keys)) for key in keys: self.assertEqual(key in result, True) # All done break
def test_db_deviceagent_indices(self): """Testing method / function db_deviceagent_agentindices.""" # Clear the memory cache CACHE.clear() # Get results uri = ( '/infoset/api/v1/devices/{}/agents' ''.format(self.expected['idx_device']) ) response = self.API.get(uri) result = json.loads(response.get_data(as_text=True)) # Verify reponse code self.assertEqual(response.status_code, 200) # Verify response content self.assertEqual(isinstance(result, list), True) self.assertEqual(len(result), 1) self.assertEqual(result, [self.expected['idx_agent']])
def db_devagt_get_all_device_agents(): """Get all DeviceAgent data from the DB. Args: None Returns: Agent data """ # Get data from cache key = ('DB/DeviceAgent') cache_value = CACHE.get(key) # Process cache miss if cache_value is None: data = db_deviceagent.get_all_device_agents() CACHE.set(key, data) else: data = cache_value # Return return jsonify(data)
def agents(idx_agent): """Get Agent data from the DB by idx value. Args: idx_agent: Agent table idx_agent Returns: data: JSON data for the selected agent """ # Get data from cache key = ('DB/Agent/idx_agent/{}'.format(idx_agent)) cache_value = CACHE.get(key) # Process cache miss if cache_value is None: query = db_agent.GetIDXAgent(general.integerize(idx_agent)) data = query.everything() CACHE.set(key, data) else: data = cache_value # Return return jsonify(data)
def agents_query(): """Get Agent data from the DB by id_agent value. Args: None Returns: data: JSON data for the selected agent """ # Initialize key variables id_agent = request.args.get('id_agent') if bool(id_agent) is True: # Process id_datapoint request key = ('DB/Agent/id_agent/{}'.format(id_agent)) cache_value = CACHE.get(key) # Process cache miss if cache_value is None: query = db_agent.GetIDAgent(id_agent) data = query.everything() CACHE.set(key, data) else: data = cache_value else: # Process general request key = ('DB/Agent/id_agent') cache_value = CACHE.get(key) # Process cache miss if cache_value is None: data = db_agent.get_all_agents() CACHE.set(key, data) else: data = cache_value # Return return jsonify(data)
def test_lastcontacts(self): """Testing method / function lastcontacts.""" # Initialize key variables precision = 5 ####################################################################### # Try with secondsago = 3600 ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago uri = '/infoset/api/v1/lastcontacts?secondsago=3600' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test self.assertEqual(isinstance(results, list), True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] result = results[0] self.assertEqual(result['timestamp'], expected['timestamp']) self.assertEqual( round(result['value'], precision), round(expected['value'], precision)) ####################################################################### # Try with secondsago = 60 (No values expected) ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to a minute ago uri = '/infoset/api/v1/lastcontacts?secondsago=60' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test self.assertEqual(isinstance(results, list), True) self.assertEqual(bool(results), False) ####################################################################### # Try with ts_start = 0 ####################################################################### # Clear the memory cache CACHE.clear() # Get results uri = '/infoset/api/v1/lastcontacts?ts_start=0' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test self.assertEqual(isinstance(results, list), True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] result = results[0] self.assertEqual(result['timestamp'], expected['timestamp']) self.assertEqual( round(result['value'], precision), round(expected['value'], precision)) ####################################################################### # Try with ts_start = an hour ago ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago ts_start = int(datetime.utcnow().timestamp()) - 3600 uri = '/infoset/api/v1/lastcontacts?ts_start={}'.format(ts_start) response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test self.assertEqual(isinstance(results, list), True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] result = results[0] self.assertEqual(result['timestamp'], expected['timestamp']) self.assertEqual( round(result['value'], precision), round(expected['value'], precision)) ####################################################################### # Try with ts_start = a minute ago ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago ts_start = int(datetime.utcnow().timestamp()) - 60 uri = '/infoset/api/v1/lastcontacts?ts_start={}'.format(ts_start) response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test type of result self.assertEqual(isinstance(results, list), True) self.assertEqual(bool(results), False)
def test_id_agents(self): """Testing method / function id_agents.""" # Initialize key variables precision = 5 ####################################################################### # Try with default values ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago uri = '/infoset/api/v1/lastcontacts/id_agents' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Further testing result = results[0] self.assertEqual(result['agent'], self.expected['agent']) self.assertEqual(result['devicename'], self.expected['devicename']) self.assertEqual(result['id_agent'], self.expected['id_agent']) self.assertEqual('timeseries' in result, True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] # We should only have a single result to test for agent_label, data_dict in result['timeseries'].items(): # Test match for agent_label self.assertEqual(agent_label, self.expected['agent_label']) for key, value in data_dict.items(): # Test presence of timeseries values if key == 'timestamp': self.assertEqual(value, expected['timestamp']) elif key == 'value': self.assertEqual( round(value, precision), round(expected['value'], precision)) ####################################################################### # Try with secondsago = 3600 ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago uri = '/infoset/api/v1/lastcontacts/id_agents?secondsago=3600' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Further testing result = results[0] self.assertEqual(result['agent'], self.expected['agent']) self.assertEqual(result['devicename'], self.expected['devicename']) self.assertEqual(result['id_agent'], self.expected['id_agent']) self.assertEqual('timeseries' in result, True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] # We should only have a single result to test for agent_label, data_dict in result['timeseries'].items(): # Test match for agent_label self.assertEqual(agent_label, self.expected['agent_label']) for key, value in data_dict.items(): # Test presence of timeseries values if key == 'timestamp': self.assertEqual(value, expected['timestamp']) elif key == 'value': self.assertEqual( round(value, precision), round(expected['value'], precision)) ####################################################################### # Try with secondsago = 60 (No response expected) ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago uri = '/infoset/api/v1/lastcontacts/id_agents?secondsago=60' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test self.assertEqual(isinstance(results, list), True) self.assertEqual(bool(results), False) ####################################################################### # Try with ts_star = 0 ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago uri = '/infoset/api/v1/lastcontacts/id_agents?ts_start=0' response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Further testing result = results[0] self.assertEqual(result['agent'], self.expected['agent']) self.assertEqual(result['devicename'], self.expected['devicename']) self.assertEqual(result['id_agent'], self.expected['id_agent']) self.assertEqual('timeseries' in result, True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] # We should only have a single result to test for agent_label, data_dict in result['timeseries'].items(): # Test match for agent_label self.assertEqual(agent_label, self.expected['agent_label']) for key, value in data_dict.items(): # Test presence of timeseries values if key == 'timestamp': self.assertEqual(value, expected['timestamp']) elif key == 'value': self.assertEqual( round(value, precision), round(expected['value'], precision)) ####################################################################### # Try with ts_start = an hour ago ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago ts_start = int(datetime.utcnow().timestamp()) - 3600 uri = ( '/infoset/api/v1/lastcontacts/id_agents?ts_start={}' ''.format(ts_start)) response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Further testing result = results[0] self.assertEqual(result['agent'], self.expected['agent']) self.assertEqual(result['devicename'], self.expected['devicename']) self.assertEqual(result['id_agent'], self.expected['id_agent']) self.assertEqual('timeseries' in result, True) # First item in expected should be the most recent contact. # Expected['values'] is sorted by timestamp. expected = self.expected['values'][0] # We should only have a single result to test for agent_label, data_dict in result['timeseries'].items(): # Test match for agent_label self.assertEqual(agent_label, self.expected['agent_label']) for key, value in data_dict.items(): # Test presence of timeseries values if key == 'timestamp': self.assertEqual(value, expected['timestamp']) elif key == 'value': self.assertEqual( round(value, precision), round(expected['value'], precision)) ####################################################################### # Try with ts_start = 15 minutes ago (No values expected) ####################################################################### # Clear the memory cache CACHE.clear() # Get results for up to an hour ago ts_start = int(datetime.utcnow().timestamp()) - 900 uri = ( '/infoset/api/v1/lastcontacts/id_agents?ts_start={}' ''.format(ts_start)) response = self.API.get(uri) results = json.loads(response.get_data(as_text=True)) # Test self.assertEqual(isinstance(results, list), True) self.assertEqual(bool(results), False)