diff --git a/neat/db.py b/neat/db.py index e48b1d9..2fe95e8 100644 --- a/neat/db.py +++ b/neat/db.py @@ -296,6 +296,17 @@ class Database(object): self.vm_resource_usage.delete().where( self.vm_resource_usage.c.timestamp < datetime_threshold)) + @contract(datetime_threshold=datetime.datetime) + def cleanup_host_resource_usage(self, datetime_threshold): + """ Delete host resource usage data older than the threshold. + + :param datetime_threshold: A datetime threshold. + :type datetime_threshold: datetime.datetime + """ + self.connection.execute( + self.host_resource_usage.delete().where( + self.host_resource_usage.c.timestamp < datetime_threshold)) + @contract def insert_host_states(self, hosts): """ Insert host states for a set of hosts. diff --git a/neat/globals/db_cleaner.py b/neat/globals/db_cleaner.py index 70a3996..c40c04a 100644 --- a/neat/globals/db_cleaner.py +++ b/neat/globals/db_cleaner.py @@ -89,6 +89,7 @@ def execute(config, state): """ datetime_threshold = today() - state['time_delta'] state['db'].cleanup_vm_resource_usage(datetime_threshold) + state['db'].cleanup_host_resource_usage(datetime_threshold) if log.isEnabledFor(logging.INFO): log.info('Cleaned up data older than %s', datetime_threshold.strftime('%Y-%m-%d %H:%M:%S')) diff --git a/tests/test_db.py b/tests/test_db.py index ce89c3c..3fa3740 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -230,6 +230,22 @@ class Db(TestCase): db.cleanup_vm_resource_usage(time.replace(second=5)) assert db.select_cpu_mhz_for_vm(uuid, 100) == range(5, 10) + @qc(1) + def cleanup_host_resource_usage( + hostname=str_(of='abc123', min_length=5, max_length=10) + ): + db = db_utils.init_db('sqlite:///:memory:') + host_id = db.update_host(hostname, 1, 1, 1) + time = datetime.datetime.today() + for i in range(10): + db.host_resource_usage.insert().execute( + host_id=1, + cpu_mhz=i, + timestamp=time.replace(second=i)) + assert db.select_cpu_mhz_for_host(hostname, 100) == range(10) + db.cleanup_host_resource_usage(time.replace(second=5)) + assert db.select_cpu_mhz_for_host(hostname, 100) == range(5, 10) + def test_insert_host_states(self): db = db_utils.init_db('sqlite:///:memory:') hosts = {}