コード例 #1
0
    def idx_agent(self):
        """Insert new agent into database if necessary.

        Args:
            None

        Returns:
            idx_agent: IDX value of agent from database

        """
        # Initialize key variables
        agent_name = self.agent_data['agent_name']
        id_agent = self.agent_data['id_agent']

        # Get information on agent from database
        data = agent.GetIDAgent(id_agent)

        # Return if agent already exists in the table
        if data.exists() is True:
            idx_agent = data.idx_agent()
            return idx_agent

        # Add record to the database
        record = Agent(id_agent=general.encode(id_agent),
                       name=general.encode(agent_name))
        database = db.Database()
        database.add(record, 1081)

        # Get idx_agent value from database
        data = agent.GetIDAgent(id_agent)
        idx_agent = data.idx_agent()
        return idx_agent
コード例 #2
0
ファイル: cache.py プロジェクト: clayton-colovore/infoset-ng
    def idx_agent(self):
        """Insert new agent into database if necessary.

        Args:
            None

        Returns:
            idx_agent: IDX value of agent from database

        """
        # Initialize key variables
        agent_name = self.agent_data['agent_name']
        id_agent = self.agent_data['id_agent']

        # Get information on agent from database
        agent_data = db_agent.GetIDAgent(id_agent)

        # Return if agent already exists in the table
        if agent_data.exists() is True:
            idx_agent = agent_data.idx_agent()
            return idx_agent

        # Get information on agent from database
        name_data = db_agentname.GetAgentName(agent_name)

        # Insert data into table if required
        # Get idx_agentname
        if name_data.exists() is False:
            record = AgentName(
                name=general.encode(agent_name))
            database = db.Database()
            try:
                database.add(record, 1145)
            except pymysql.IntegrityError:
                # There may be a duplicate agent name if this is a brand
                # new database and there is a flurry of updates from multiple
                # agents. This is OK, pass.
                #
                # We are expecting a 'pymysql.err.IntegrityError' but for some
                # reason it could not be caught.
                pass
            new_name_data = db_agentname.GetAgentName(agent_name)
            idx_agentname = new_name_data.idx_agentname()
        else:
            idx_agentname = name_data.idx_agentname()

        # Add record to the database
        new_record = Agent(
            id_agent=general.encode(id_agent),
            idx_agentname=idx_agentname)
        database = db.Database()
        database.add(new_record, 1081)

        # Get idx_agent value from database
        new_agent_data = db_agent.GetIDAgent(id_agent)
        idx_agent = new_agent_data.idx_agent()
        return idx_agent
コード例 #3
0
def _setup_db_deviceagent(data):
    """Create the database for DeviceAgent table testing.

    Args:
        None

    Returns:
        result: Tuple of (idx_device, idx_agent)

    """
    # Initialize key variables
    devicename = data['devicename']
    description = data['device_description']
    id_agent = data['id_agent']
    agent_name = data['agent']
    last_timestamp = data['timestamp']

    # Add AgentName record to the database
    record = AgentName(name=general.encode(agent_name))
    database = db.Database()
    database.add(record, 1031)

    # Add Agent record to the database
    record = Agent(id_agent=general.encode(id_agent))
    database = db.Database()
    database.add(record, 1031)

    # Get idx_agent value from database
    agent_info = db_agent.GetIDAgent(id_agent)
    idx_agent = agent_info.idx_agent()

    # Add record to the database
    dev_record = Device(description=general.encode(description),
                        devicename=general.encode(devicename))
    database = db.Database()
    database.add(dev_record, 1034)

    # Get idx of newly added device
    device_info = db_device.GetDevice(devicename)
    idx_device = device_info.idx_device()

    # Update DeviceAgent table
    if hagent.device_agent_exists(idx_device, idx_agent) is False:
        # Add to DeviceAgent table
        da_record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent)
        database = db.Database()
        database.add(da_record, 1020)

    # Update DeviceAgent table with timestamp
    database = db.Database()
    session = database.session()
    record = session.query(DeviceAgent).filter(
        and_(DeviceAgent.idx_device == idx_device,
             DeviceAgent.idx_agent == idx_agent)).one()
    record.last_timestamp = last_timestamp
    database.commit(session, 1042)

    # Return
    result = (idx_device, idx_agent)
    return result
コード例 #4
0
ファイル: test_db_agent.py プロジェクト: jordanTDK/infoset-ng
class Other(unittest.TestCase):
    """Checks all functions and methods."""

    # Intstantiate a good agent
    idx_agent_good = 1

    # Setup database
    (good_id, _) = unittest_db.setup_db_agent()

    # Retrieve data
    good_agent = db_agent.GetIDAgent(good_id)

    def test_id_agent_exists(self):
        """Testing function id_agent_exists."""
        # Testing with known good value
        expected = True
        result = db_agent.id_agent_exists(self.good_id)
        self.assertEqual(result, expected)

        # Testing with known bad value
        expected = False
        result = db_agent.id_agent_exists('bogus')
        self.assertEqual(result, expected)

    def test_idx_agent_exists(self):
        """Testing function idx_agent_exists."""
        # Testing with known good value
        expected = True
        result = db_agent.idx_agent_exists(self.idx_agent_good)
        self.assertEqual(result, expected)

        # Testing with known bad value
        expected = False
        result = db_agent.idx_agent_exists(None)
        self.assertEqual(result, expected)
コード例 #5
0
    def idx_agent(self):
        """Insert new agent into database if necessary.

        Args:
            None

        Returns:
            idx_agent: IDX value of agent from database

        """
        # Initialize key variables
        agent_name = self.agent_data['agent_name']
        id_agent = self.agent_data['id_agent']

        # Get information on agent from database
        agent_data = db_agent.GetIDAgent(id_agent)

        # Return if agent already exists in the table
        if agent_data.exists() is True:
            idx_agent = agent_data.idx_agent()
            return idx_agent

        # Get information on agent from database
        name_data = db_agentname.GetAgentName(agent_name)

        # Insert data into table if required
        # Get idx_agentname
        if name_data.exists() is False:
            record = AgentName(name=general.encode(agent_name))
            database = db.Database()
            database.add(record, 1081)

            new_name_data = db_agentname.GetAgentName(agent_name)
            idx_agentname = new_name_data.idx_agentname()
        else:
            idx_agentname = name_data.idx_agentname()

        # Add record to the database
        new_record = Agent(id_agent=general.encode(id_agent),
                           idx_agentname=idx_agentname)
        database = db.Database()
        database.add(new_record, 1081)

        # Get idx_agent value from database
        new_agent_data = db_agent.GetIDAgent(id_agent)
        idx_agent = new_agent_data.idx_agent()
        return idx_agent
コード例 #6
0
    def test_valid(self):
        """Testing function valid."""
        # Initialize key variables
        devicename = self.data['devicename']
        id_agent = self.data['id_agent']
        last_timestamp = self.data['timestamp']

        # Drop the database and create tables
        unittest_setup_db.TestData()

        # Add record to the database
        record = Agent(id_agent=general.encode(id_agent))
        database = db.Database()
        database.add(record, 1040)

        # Test must be good as DeviceAgent last_timestamp not updated
        result = validate._CheckDuplicates(self.data)
        self.assertEqual(result.valid(), True)

        # Get idx_agent value from database
        data = db_agent.GetIDAgent(id_agent)
        idx_agent = data.idx_agent()

        # Add record to the database
        record = Device(devicename=general.encode(devicename))
        database = db.Database()
        database.add(record, 1024)

        # Test must be good as DeviceAgent last_timestamp not updated
        result = validate._CheckDuplicates(self.data)
        self.assertEqual(result.valid(), True)

        # Get idx of newly added device
        device_info = db_device.GetDevice(devicename)
        idx_device = device_info.idx_device()

        # Update DeviceAgent table
        if hagent.device_agent_exists(idx_device, idx_agent) is False:
            # Add to DeviceAgent table
            record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent)
            database = db.Database()
            database.add(record, 1055)

        # Test must be good as DeviceAgent last_timestamp not updated
        result = validate._CheckDuplicates(self.data)
        self.assertEqual(result.valid(), True)

        # Update database with timestamp
        database = db.Database()
        session = database.session()
        record = session.query(DeviceAgent).filter(
            and_(DeviceAgent.idx_device == idx_device,
                 DeviceAgent.idx_agent == idx_agent)).one()
        record.last_timestamp = last_timestamp
        database.commit(session, 1044)

        # Test must fail as DeviceAgent last_timestamp not updated
        result = validate._CheckDuplicates(self.data)
        self.assertEqual(result.valid(), False)
コード例 #7
0
ファイル: test_db_agent.py プロジェクト: OrandiH/infoset-ng
 def test_init(self):
     """Testing method __init__."""
     # Test with non existent AgentID
     record = db_agent.GetIDAgent('bogus')
     self.assertEqual(record.exists(), False)
     self.assertEqual(record.enabled(), None)
     self.assertEqual(record.idx_agent(), None)
     self.assertEqual(record.idx_agentname(), None)
コード例 #8
0
def db_agent_getid_agent(value):
    """Get Agent data from the DB by id_agent value.

    Args:
        None

    Returns:
        Home Page

    """
    # Return
    query = db_agent.GetIDAgent(value)
    data = query.everything()
    return jsonify(data)
コード例 #9
0
    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
コード例 #10
0
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)
コード例 #11
0
ファイル: agents.py プロジェクト: Jhamali/infosetDB
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
        query = db_agent.GetIDAgent(id_agent)
        data = [query.everything()]
    else:
        data = db_agent.get_all_agents()

    # Return
    return jsonify(data)
コード例 #12
0
ファイル: agents.py プロジェクト: OrandiH/infoset-ng
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)
コード例 #13
0
class TestGetIdentifier(unittest.TestCase):
    """Checks all functions and methods."""

    # Setup database based on the config
    database = unittest_setup_db.TestData()

    # Define expected values
    expected = {}
    expected['idx_agent'] = database.idx_agent()
    expected['id_agent'] = database.id_agent()
    expected['idx_agentname'] = database.idx_agentname()
    expected['agent'] = database.agent()
    expected['enabled'] = True
    expected['exists'] = True

    # Retrieve data
    good_agent = db_agent.GetIDAgent(expected['id_agent'])

    def test_init(self):
        """Testing method __init__."""
        # Test with non existent AgentID
        record = db_agent.GetIDAgent('bogus')
        self.assertEqual(record.exists(), False)
        self.assertEqual(record.enabled(), None)
        self.assertEqual(record.idx_agent(), None)
        self.assertEqual(record.agent(), None)
        self.assertEqual(record.idx_agentname(), None)

    def test_exists(self):
        """Testing method exists."""
        # Testing with known good value
        result = self.good_agent.exists()
        self.assertEqual(result, True)

    def test_idx_agentname(self):
        """Testing method idx_agentname."""
        # Testing with known good value
        result = self.good_agent.idx_agentname()
        self.assertEqual(result, self.expected['idx_agentname'])

    def test_idx_agent(self):
        """Testing method idx."""
        # Testing with known good value
        result = self.good_agent.idx_agent()
        self.assertEqual(result, self.expected['idx_agent'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_agent.idx_agent()
        self.assertNotEqual(result, expected)

    def test_agent(self):
        """Testing method name."""
        # Testing with known good value
        result = self.good_agent.agent()
        self.assertEqual(result, self.expected['agent'])

    def test_enabled(self):
        """Testing method enabled."""
        # Testing with known good value
        result = self.good_agent.enabled()
        self.assertEqual(result, self.expected['enabled'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_agent.enabled()
        self.assertNotEqual(result, expected)

    def test_everything(self):
        """Testing method everything."""
        # Testing with known good value
        result = self.good_agent.everything()
        for key, _ in self.expected.items():
            self.assertEqual(result[key], self.expected[key])

        # 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)
コード例 #14
0
ファイル: test_db_agent.py プロジェクト: jordanTDK/infoset-ng
 def test_init_getid_agent(self):
     """Testing method __init__."""
     # Test with non existent AgentID
     record = db_agent.GetIDAgent('bogus')
     self.assertEqual(record.exists(), False)
コード例 #15
0
ファイル: test_db_agent.py プロジェクト: jordanTDK/infoset-ng
class TestGetIdentifier(unittest.TestCase):
    """Checks all functions and methods."""

    # Setup database
    (good_id, expected) = unittest_db.setup_db_agent()

    # Retrieve data
    good_agent = db_agent.GetIDAgent(good_id)

    def test_init_getid_agent(self):
        """Testing method __init__."""
        # Test with non existent AgentID
        record = db_agent.GetIDAgent('bogus')
        self.assertEqual(record.exists(), False)

    def test_exists(self):
        """Testing method exists."""
        # Testing with known good value
        result = self.good_agent.exists()
        self.assertEqual(result, True)

    def test_idx_getidx_agent(self):
        """Testing method idx."""
        # Testing with known good value
        result = self.good_agent.idx_agent()
        self.assertEqual(result, self.expected['idx_agent'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_agent.idx_agent()
        self.assertNotEqual(result, expected)

    def test_name_getid_agent(self):
        """Testing method name."""
        # Testing with known good value
        result = self.good_agent.name()
        self.assertEqual(result, self.expected['name'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_agent.name()
        self.assertNotEqual(result, expected)

    def test_enabled_getid_agent(self):
        """Testing method enabled."""
        # Testing with known good value
        result = self.good_agent.enabled()
        self.assertEqual(result, self.expected['enabled'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_agent.enabled()
        self.assertNotEqual(result, expected)

    def test_last_timestamp_getid_agent(self):
        """Testing method last_timestamp."""
        # Testing with known good value
        result = self.good_agent.last_timestamp()
        self.assertEqual(result, self.expected['last_timestamp'])

        # Testing with known bad value
        expected = ('bogus')
        result = self.good_agent.last_timestamp()
        self.assertNotEqual(result, expected)

    def test_everything_getid_agent(self):
        """Testing method everything."""
        # Testing with known good value
        result = self.good_agent.everything()
        for key, _ in self.expected.items():
            self.assertEqual(result[key], self.expected[key])