From 4110addd4d450954aa32aa9045b99de8205543d0 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sun, 22 Jan 2012 19:29:10 -0500 Subject: usage: Fix time filtering Querying for resource usage within a given time range seems to have a logic flaw: it will only report instances that have run the entire length of the specified range. AIUI What we really want is any instance that spent any time consuming resources at any point in the specified range. Fix the logic to implement this. v2: Update function comments as well Change-Id: I4fa567982457e5f447ec33cf3d96457f50d71d86 --- nova/db/sqlalchemy/api.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 5577fb932..4f5e92bc2 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1556,36 +1556,36 @@ def instance_get_all_by_filters(context, filters): @require_context def instance_get_active_by_window(context, begin, end=None, project_id=None): - """Return instances that were continuously active over window.""" + """Return instances that were active during window.""" session = get_session() - query = session.query(models.Instance).\ - filter(models.Instance.launched_at < begin) + query = session.query(models.Instance) + + query = query.filter(or_(models.Instance.terminated_at == None, + models.Instance.terminated_at > begin)) if end: - query = query.filter(or_(models.Instance.terminated_at == None, - models.Instance.terminated_at > end)) - else: - query = query.filter(models.Instance.terminated_at == None) + query = query.filter(models.Instance.launched_at < end) if project_id: query = query.filter_by(project_id=project_id) + return query.all() @require_admin_context def instance_get_active_by_window_joined(context, begin, end=None, project_id=None): - """Return instances and joins that were continuously active over window.""" + """Return instances and joins that were active during window.""" session = get_session() - query = session.query(models.Instance).\ - options(joinedload('security_groups')).\ - options(joinedload('instance_type')).\ - filter(models.Instance.launched_at < begin) + query = session.query(models.Instance) + + query = query.options(joinedload('security_groups')).\ + options(joinedload('instance_type')).\ + filter(or_(models.Instance.terminated_at == None, + models.Instance.terminated_at > begin)) if end: - query = query.filter(or_(models.Instance.terminated_at == None, - models.Instance.terminated_at > end)) - else: - query = query.filter(models.Instance.terminated_at == None) + query = query.filter(models.Instance.launched_at < end) if project_id: query = query.filter_by(project_id=project_id) + return query.all() -- cgit