diff --git a/neat/db.py b/neat/db.py index 9c10a04..4de05d8 100644 --- a/neat/db.py +++ b/neat/db.py @@ -30,19 +30,23 @@ class Database(object): @contract(connection=Connection, hosts=Table, vms=Table, - vm_resource_usage=Table) - def __init__(self, connection, hosts, vms, vm_resource_usage): + vm_resource_usage=Table, + host_states=Table) + def __init__(self, connection, hosts, vms, + vm_resource_usage, host_states): """ Initialize the database. :param connection: A database connection table. :param hosts: The hosts table. :param vms: The vms table. :param vm_resource_usage: The vm_resource_usage table. + :param host_states: The host_states table. """ self.connection = connection self.hosts = hosts self.vms = vms self.vm_resource_usage = vm_resource_usage + self.host_states = host_states log.debug('Instantiated a Database object') @contract diff --git a/neat/db_utils.py b/neat/db_utils.py index 6135313..37e2721 100644 --- a/neat/db_utils.py +++ b/neat/db_utils.py @@ -56,9 +56,16 @@ def init_db(sql_connection): Column('timestamp', DateTime, default=func.now()), Column('cpu_mhz', Integer, nullable=False)) + host_states = \ + Table('host_states', metadata, + Column('id', Integer, primary_key=True), + Column('host_id', Integer, ForeignKey('hosts.id'), nullable=False), + Column('timestamp', DateTime, default=func.now()), + Column('state', Integer, nullable=False)) + metadata.create_all() connection = engine.connect() - db = Database(connection, hosts, vms, vm_resource_usage) + db = Database(connection, hosts, vms, vm_resource_usage, host_states) log.debug('Initialized a DB connection to %s', sql_connection) return db diff --git a/tests/test_db_utils.py b/tests/test_db_utils.py index d50ac15..b2cfb8e 100644 --- a/tests/test_db_utils.py +++ b/tests/test_db_utils.py @@ -33,6 +33,7 @@ class DbUtils(TestCase): assert isinstance(db.hosts, Table) assert isinstance(db.vms, Table) assert isinstance(db.vm_resource_usage, Table) + assert isinstance(db.host_states, Table) assert db.hosts.c.keys() == \ ['id', 'hostname', 'cpu_mhz', 'cpu_cores', 'ram'] assert db.vms.c.keys() == \ @@ -41,3 +42,7 @@ class DbUtils(TestCase): ['id', 'vm_id', 'timestamp', 'cpu_mhz'] assert list(db.vm_resource_usage.foreign_keys)[0].target_fullname \ == 'vms.id' + assert db.host_states.c.keys() == \ + ['id', 'host_id', 'timestamp', 'state'] + assert list(db.host_states.foreign_keys)[0].target_fullname \ + == 'hosts.id'