Exemple #1
0
def pairs(pattoo_db_record):
    """Create db Pair table entries.

    Args:
        pattoo_db_record: PattooDBrecord object

    Returns:
        result: List of Pair.idx_pair database index values

    """
    # Initialize key variables
    result = []

    # Get key-values
    _pairs = key_value_pairs(pattoo_db_record)

    # Get list of pairs in the database
    pair.insert_rows(_pairs)
    result = pair.idx_pairs(_pairs)

    # Return
    return result
Exemple #2
0
    def test_idx_pairs(self):
        """Testing method / function idx_pairs."""
        # Initialize key variables
        keypairs = []
        idx_pairs = []
        for _ in range(0, 10):
            time.sleep(0.05)
            key = data.hashstring(str(random()))
            value = data.hashstring(str(random()))
            keypairs.append((key, value))

        # Insert values in tables
        pair.insert_rows(keypairs)

        # Test
        for key, value in keypairs:
            idx_pairs.append(pair.pair_exists(key, value))

        result = pair.idx_pairs(keypairs)
        self.assertEqual(len(result), len(idx_pairs))
        for idx_pair in idx_pairs:
            self.assertTrue(idx_pair in result)
Exemple #3
0
    def test_checksums(self):
        """Testing method / function checksums."""
        # Initialize key variables
        expected = {}
        polling_interval = 1
        pattoo_agent_polled_target = 'panda_bear'
        pattoo_agent_program = 'koala_bear'
        pattoo_agent_hostname = 'grizzly_bear'

        # Insert an entry in the agent table
        agent_id = data.hashstring(str(random()))
        idx_agent = agent.idx_agent(agent_id, pattoo_agent_polled_target,
                                    pattoo_agent_program)

        # Populate database with key-value pairs
        for data_index in range(0, 10):
            time.sleep(0.1)

            # Add the checksum to the database
            checksum = data.hashstring(str(random()))
            datapoint.insert_row(checksum, DATA_FLOAT, polling_interval,
                                 idx_agent)
            idx_datapoint = datapoint.checksum_exists(checksum)

            # Define what we expect from the test function
            expected[checksum] = ChecksumLookup(
                idx_datapoint=idx_datapoint,
                polling_interval=polling_interval,
                last_timestamp=1)

            # Add key-pairs to the database
            record = PattooDBrecord(
                pattoo_checksum=checksum,
                pattoo_key='key',
                pattoo_agent_polling_interval=polling_interval,
                pattoo_agent_id=agent_id,
                pattoo_timestamp=int(time.time() * 1000),
                pattoo_data_type=DATA_FLOAT,
                pattoo_value=(data_index * 10),
                pattoo_agent_polled_target=pattoo_agent_polled_target,
                pattoo_agent_program=pattoo_agent_program,
                pattoo_agent_hostname=pattoo_agent_hostname,
                pattoo_metadata=[])
            pairs = get.key_value_pairs(record)
            pair.insert_rows(pairs)
            idx_pairs = pair.idx_pairs(pairs)

            # Create glue entry
            glue.insert_rows(idx_datapoint, idx_pairs)

        #######################################################################
        # This is added to verify that we only get a subset of results
        #######################################################################

        # Insert an entry in the agent table
        fake_agent_id = data.hashstring(str(random()))
        idx_agent = agent.idx_agent(fake_agent_id, pattoo_agent_polled_target,
                                    pattoo_agent_program)

        # Populate database with key-value pairs
        for data_index in range(0, 17):
            time.sleep(0.1)

            # Add the checksum to the database
            checksum = data.hashstring(str(random()))
            datapoint.insert_row(checksum, DATA_FLOAT, polling_interval,
                                 idx_agent)
            idx_datapoint = datapoint.checksum_exists(checksum)

            # Add key-pairs to the database
            record = PattooDBrecord(
                pattoo_checksum=checksum,
                pattoo_key='key',
                pattoo_agent_polling_interval=polling_interval,
                pattoo_agent_id=fake_agent_id,
                pattoo_timestamp=int(time.time() * 1000),
                pattoo_data_type=DATA_FLOAT,
                pattoo_value=(data_index * 10),
                pattoo_agent_polled_target=pattoo_agent_polled_target,
                pattoo_agent_program=pattoo_agent_program,
                pattoo_agent_hostname=pattoo_agent_hostname,
                pattoo_metadata=[])
            pairs = get.key_value_pairs(record)
            pair.insert_rows(pairs)
            idx_pairs = pair.idx_pairs(pairs)

            # Create glue entry
            glue.insert_rows(idx_datapoint, idx_pairs)

        # Test
        result = misc.agent_checksums(agent_id)
        self.assertTrue(bool(result))
        self.assertTrue(isinstance(result, dict))
        self.assertEqual(len(result), len(expected))
        for key, value in result.items():
            self.assertEqual(value.idx_datapoint, expected[key].idx_datapoint)
            self.assertEqual(value.polling_interval,
                             expected[key].polling_interval)
            self.assertEqual(value.last_timestamp,
                             expected[key].last_timestamp)