def datapoint(self): """Update the database timestamp for the datapoint. Args: None Returns: None """ # Initialize key variables idx_agent = self.idx_agent idx_device = self.idx_device last_timestamp = self.last_timestamp data_dict = {'last_timestamp': last_timestamp} idx_deviceagent = db_deviceagent.GetDeviceAgent( idx_device, idx_agent).idx_deviceagent() # Access the database database = db.Database() session = database.session() # Update session.query(Datapoint).filter( and_(Datapoint.idx_deviceagent == idx_deviceagent, Datapoint.enabled == 1)).update(data_dict) database.commit(session, 1057)
class TestGetDeviceAgent(unittest.TestCase): """Checks all functions and methods.""" ######################################################################### # General object setup ######################################################################### # Initiazlize key variables data = {} data['devicename'] = general.randomstring() data['id_agent'] = general.randomstring() data['agent'] = general.randomstring() data['timestamp'] = int(time.time()) # Setup database unittest_db.initialize_db() (idx_device_good, idx_agent_good) = unittest_db.setup_db_deviceagent(data) # Create device object good_device = db_deviceagent.GetDeviceAgent( idx_device_good, idx_agent_good) def test___init__(self): """Testing method __init__.""" # Test with non existent IDXDevice record = db_deviceagent.GetDeviceAgent('bogus', 'bogus') self.assertEqual(record.exists(), False) def test_exists(self): """Testing method exists.""" # Testing with known good value result = self.good_device.exists() self.assertEqual(result, True) # Test with known bad value expected = db_deviceagent.GetDeviceAgent(None, None) result = expected.exists() self.assertEqual(result, False) def test_enabled(self): """Testing method enabled.""" # Testing with known good value result = self.good_device.enabled() self.assertEqual(result, True) def test_last_timestamp(self): """Testing method last_timestamp.""" # Testing with known good value result = self.good_device.last_timestamp() self.assertEqual(result, self.data['timestamp']) def test_idx_deviceagent(self): """Testing method idx_deviceagent.""" # Testing with known good value result = self.good_device.idx_deviceagent() self.assertEqual(result, 1)
def test_exists(self): """Testing method exists.""" # Testing with known good value result = self.good_device.exists() self.assertEqual(result, True) # Test with known bad value expected = db_deviceagent.GetDeviceAgent(None, None) result = expected.exists() self.assertEqual(result, False)
def test___init__(self): """Testing method __init__.""" # Test with non existent IDXDevice record = db_deviceagent.GetDeviceAgent('bogus', 'bogus') self.assertEqual(record.exists(), False) self.assertEqual(record.enabled(), None) self.assertEqual(record.idx_agent(), None) self.assertEqual(record.idx_device(), None) self.assertEqual(record.idx_deviceagent(), None) self.assertEqual(record.last_timestamp(), None)
def valid(self): """Check if data keys are OK. Args: None Returns: valid: True if valid """ # Initialize key variables valid = True # Return if instantiation tests have failed if self._valid is False: valid = False return valid # Assign other values timestamp = int(self.data['timestamp']) id_agent = self.data['id_agent'] devicename = self.data['devicename'] # Check if there is a duplicate entry for this id_agent if db_agent.id_agent_exists(id_agent) is not False: idx_agent = db_agent.GetIDAgent(id_agent).idx_agent() # Check if device exists if db_device.devicename_exists(devicename) is True: idx_device = db_device.GetDevice(devicename).idx_device() # Check for device / agent entry existence if db_deviceagent.device_agent_exists( idx_device, idx_agent) is True: # Check if this device / agent has been updated before last_timesamp = db_deviceagent.GetDeviceAgent( idx_device, idx_agent).last_timestamp() # Validate if timestamp <= last_timesamp: log_message = ( 'Data for id_agent %s, devicename %s ' 'at timestamp %s ' 'is already found in database.' '') % (id_agent, devicename, timestamp) log.log2warning(1113, log_message) valid = False # Return return valid
def __init__(self, agent_data): """Instantiate the class. Args: agent_data: Agent data from successive Drains Returns: None """ # Initialize key variables self.agent_data = agent_data # Update Agent, Device and DeviceAgent database tables if # Device and agent are not already there self._idx_agent = self.idx_agent() self._idx_device = self.idx_device() self._idx_deviceagent = db_deviceagent.GetDeviceAgent( self._idx_device, self._idx_agent).idx_deviceagent()
def devicename_agents(devicename, id_agent): """Get last contact data from the DB. Args: devicename: Device table devicename id_agent: Agent table id_agent Returns: data: JSON data for the selected agent """ # Initialize key variables data = [] # Get starting timestamp secondsago = general.integerize(request.args.get('secondsago')) timestamp = general.integerize(request.args.get('ts_start')) ts_start = _ts_start(secondsago, timestamp) # Get idx_device and idx_agent device = db_device.GetDevice(devicename) if device.exists() is True: # Device Found idx_device = device.idx_device() # Now find idx_agent agent = db_agent.GetIDAgent(id_agent) if agent.exists() is True: idx_agent = agent.idx_agent() # Now get the idx_deviceagent deviceagent = db_deviceagent.GetDeviceAgent(idx_device, idx_agent) if deviceagent.exists() is True: idx_deviceagent = deviceagent.idx_deviceagent() # Now get the data data = db_data.last_contacts_by_device( int(idx_deviceagent), int(ts_start)) # Return return jsonify(data)
def __init__(self): """Method initializing the class.""" # Initialize key variables self.data = {} self.data['idx_datapoint'] = 1 self.data['idx_agentname'] = 1 self.data['idx_billcode'] = 1 self.data['idx_device'] = 1 self.data['idx_deviceagent'] = 1 self.data['idx_department'] = 1 self.data['timestamp'] = general.normalized_timestamp() self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['id_agent'] = general.hashstring(general.randomstring()) self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['device_description'] = general.hashstring( general.randomstring()) self.data['agent_name'] = general.hashstring(general.randomstring()) self.data['agent_source'] = general.hashstring(general.randomstring()) self.data['agent_label'] = general.hashstring(general.randomstring()) self.data['department_code'] = general.hashstring( general.randomstring()) self.data['department_name'] = general.hashstring( general.randomstring()) self.data['billcode_code'] = general.hashstring(general.randomstring()) self.data['billcode_name'] = general.hashstring(general.randomstring()) # Drop the database and create tables initialize_db() # Initialize agent variables agent_data = {} agent_data['devicename'] = self.data['devicename'] agent_data['device_description'] = self.data['device_description'] agent_data['id_agent'] = self.data['id_agent'] agent_data['agent'] = self.data['agent_name'] agent_data['timestamp'] = self.data['timestamp'] (self.data['idx_device'], self.data['idx_agent']) = _setup_db_deviceagent(agent_data) # Get DeviceAgent index value deviceagent = hagent.GetDeviceAgent(self.data['idx_device'], self.data['idx_agent']) self.data['idx_deviceagent'] = deviceagent.idx_deviceagent() # Insert Department data into database dept_data = Department(name=self.data['department_name'].encode(), code=self.data['department_code'].encode()) database = db.Database() database.add_all([dept_data], 1035) # Insert Billcode data into database bill_data = Billcode(name=self.data['billcode_name'].encode(), code=self.data['billcode_code'].encode()) database = db.Database() database.add_all([bill_data], 1039) # Insert Datapoint data into database new_data = Datapoint(agent_source=self.data['agent_source'].encode(), agent_label=self.data['agent_label'].encode(), last_timestamp=self.data['timestamp'], idx_deviceagent=self.data['idx_deviceagent'], id_datapoint=self.data['id_datapoint'].encode()) database = db.Database() database.add_all([new_data], 1072)
def __init__(self): """Method initializing the class.""" # Initialize key variables self.data = {} self.data['idx_datapoint'] = 1 self.data['idx_agentname'] = 1 self.data['idx_billcode'] = 1 self.data['idx_device'] = 1 self.data['idx_deviceagent'] = 1 self.data['idx_department'] = 1 self.data['timestamp'] = general.normalized_timestamp() self.data['last_timestamp'] = general.normalized_timestamp() self.data['devicename'] = general.hashstring(general.randomstring()) self.data['id_agent'] = general.hashstring(general.randomstring()) self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['device_description'] = general.hashstring( general.randomstring()) self.data['agent'] = general.hashstring(general.randomstring()) self.data['agent_source'] = general.hashstring(general.randomstring()) self.data['agent_label'] = general.hashstring(general.randomstring()) self.data['department_code'] = general.hashstring( general.randomstring()) self.data['department_name'] = general.hashstring( general.randomstring()) self.data['billcode_code'] = general.hashstring( general.randomstring()) self.data['billcode_name'] = general.hashstring( general.randomstring()) # Define data to Insert self.data['values'] = [] for timestamp in _timestamps(): value_dict = { 'idx_datapoint': self.data['idx_datapoint'], 'value': timestamp * (1 + random.uniform(0, 1)), 'timestamp': timestamp} self.data['values'].append(value_dict) # Drop the database and create tables initialize_db() # Initialize agent variables agent_data = {} agent_data['devicename'] = self.data['devicename'] agent_data['device_description'] = self.data['device_description'] agent_data['id_agent'] = self.data['id_agent'] agent_data['agent'] = self.data['agent'] agent_data['timestamp'] = self.data['timestamp'] ( self.data['idx_device'], self.data['idx_agent']) = _setup_db_deviceagent(agent_data) # Get DeviceAgent index value deviceagent = hagent.GetDeviceAgent( self.data['idx_device'], self.data['idx_agent']) self.data['idx_deviceagent'] = deviceagent.idx_deviceagent() # Insert Department data into database dept_data = Department( name=self.data['department_name'].encode(), code=self.data['department_code'].encode() ) database = db.Database() database.add_all([dept_data], 1035) # Insert Billcode data into database bill_data = Billcode( name=self.data['billcode_name'].encode(), code=self.data['billcode_code'].encode() ) database = db.Database() database.add_all([bill_data], 1039) # Insert Datapoint data into database new_data = Datapoint( agent_source=self.data['agent_source'].encode(), agent_label=self.data['agent_label'].encode(), last_timestamp=self.data['last_timestamp'], idx_deviceagent=self.data['idx_deviceagent'], id_datapoint=self.data['id_datapoint'].encode()) database = db.Database() database.add_all([new_data], 1144) # Insert timeseries data into database new_data_list = [] for item in self.data['values']: new_data_list.append( Data( idx_datapoint=item['idx_datapoint'], timestamp=item['timestamp'], value=item['value'])) database = db.Database() database.add_all(new_data_list, 1072)
class TestGetDeviceAgent(unittest.TestCase): """Checks all functions and methods.""" ######################################################################### # General object setup ######################################################################### # Setup database based on the config database = unittest_setup_db.TestData() # Define expected values expected = {} expected['idx_deviceagent'] = database.idx_deviceagent() expected['idx_device'] = database.idx_device() expected['idx_agent'] = database.idx_agent() expected['last_timestamp'] = database.last_timestamp() expected['enabled'] = True expected['exists'] = True # Create device object good_device = db_deviceagent.GetDeviceAgent(expected['idx_device'], expected['idx_agent']) def test___init__(self): """Testing method __init__.""" # Test with non existent IDXDevice record = db_deviceagent.GetDeviceAgent('bogus', 'bogus') self.assertEqual(record.exists(), False) self.assertEqual(record.enabled(), None) self.assertEqual(record.idx_agent(), None) self.assertEqual(record.idx_device(), None) self.assertEqual(record.idx_deviceagent(), None) self.assertEqual(record.last_timestamp(), None) def test_exists(self): """Testing method exists.""" # Testing with known good value result = self.good_device.exists() self.assertEqual(result, True) # Test with known bad value expected = db_deviceagent.GetDeviceAgent(None, None) result = expected.exists() self.assertEqual(result, False) def test_enabled(self): """Testing method enabled.""" # Testing with known good value result = self.good_device.enabled() self.assertEqual(result, True) def test_last_timestamp(self): """Testing method last_timestamp.""" # Testing with known good value result = self.good_device.last_timestamp() self.assertEqual(result, self.expected['last_timestamp']) def test_idx_deviceagent(self): """Testing method idx_deviceagent.""" # Testing with known good value result = self.good_device.idx_deviceagent() self.assertEqual(result, self.expected['idx_deviceagent']) def test_idx_agent(self): """Testing method idx_agent.""" # Testing with known good value result = self.good_device.idx_agent() self.assertEqual(result, self.expected['idx_agent']) def test_idx_device(self): """Testing method idx_device.""" # Testing with known good value result = self.good_device.idx_device() self.assertEqual(result, self.expected['idx_device']) def test_everything(self): """Testing method everything.""" # Testing with known good value result = self.good_device.everything() for key, _ in self.expected.items(): self.assertEqual(result[key], self.expected[key]) # 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___init__(self): """Testing method __init__.""" # Test with non existent IDXDevice record = db_deviceagent.GetDeviceAgent('bogus', 'bogus') self.assertEqual(record.exists(), False)