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
This commit is contained in:
Cole Robinson 2012-01-22 19:29:10 -05:00
parent d8363ae3e1
commit c710b60150

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