Updated the DB cleaner to clean the host_resource_usage table as well

This commit is contained in:
Anton Beloglazov 2012-10-22 16:59:42 +11:00
parent bd85baf977
commit beacd650ca
3 changed files with 28 additions and 0 deletions

View File

@ -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.

View File

@ -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'))

View File

@ -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 = {}