Adds a bandwidth filter DB call.

Change-Id: I7721bae1af900c215552f547e125c2b1e60b271e
This commit is contained in:
Josh Kearney 2012-01-24 11:09:44 -06:00
parent bc8d005609
commit 3563a693c5
2 changed files with 45 additions and 0 deletions
nova/db
api.py
sqlalchemy

@ -1535,6 +1535,11 @@ def bw_usage_get_by_instance(context, instance_id, start_period):
return IMPL.bw_usage_get_by_instance(context, instance_id, start_period)
def bw_usage_get_all_by_filters(context, filters):
"""Return bandwidth usage that matches all filters."""
return IMPL.bw_usage_get_all_by_filters(context, filters)
def bw_usage_update(context,
instance_id,
network_label,

@ -3653,6 +3653,46 @@ def bw_usage_get_by_instance(context, instance_id, start_period):
all()
@require_context
def bw_usage_get_all_by_filters(context, filters):
"""Return bandwidth usage that matches all filters."""
def _exact_match_filter(query, column, value):
"""Do exact match against a column. value to match can be a list
so you can match any value in the list.
"""
if isinstance(value, list) or isinstance(value, set):
column_attr = getattr(models.BandwidthUsage, column)
return query.filter(column_attr.in_(value))
else:
filter_dict = {}
filter_dict[column] = value
return query.filter_by(**filter_dict)
session = get_session()
query_prefix = session.query(models.BandwidthUsage).\
order_by(desc(models.BandwidthUsage.created_at))
# Make a copy of the filters dictionary to use going forward, as we'll
# be modifying it and we shouldn't affect the caller's use of it.
filters = filters.copy()
# Filters for exact matches that we can do along with the SQL query.
exact_match_filter_names = ["instance_id", "network_label",
"start_period", "last_refreshed", "bw_in", "bw_out"]
query_filters = [key for key in filters.iterkeys()
if key in exact_match_filter_names]
for filter_name in query_filters:
# Do the matching and remove the filter from the dictionary
# so we don't try it again below..
query_prefix = _exact_match_filter(query_prefix, filter_name,
filters.pop(filter_name))
return query_prefix.all()
@require_context
def bw_usage_update(context,
instance_id,