Ensure missing period is treated consistently
Add a new test to ensure that all of the storage drivers return a consistent value for the period. Fix the SQLAlchemy driver so it returns 0 for the period if no period is provided in the input to the query. Change-Id: I3bc272aea1fd1ce29953d2080b58d75f5373513a Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
This commit is contained in:
@@ -393,7 +393,7 @@ class Connection(base.Connection):
|
||||
return make_query_from_filter(query, event_filter)
|
||||
|
||||
@staticmethod
|
||||
def _stats_result_to_dict(result, period_start, period_end):
|
||||
def _stats_result_to_dict(result, period, period_start, period_end):
|
||||
return {'count': result.count,
|
||||
'min': result.min,
|
||||
'max': result.max,
|
||||
@@ -403,8 +403,7 @@ class Connection(base.Connection):
|
||||
'duration_end': result.tsmax,
|
||||
'duration': timeutils.delta_seconds(result.tsmin,
|
||||
result.tsmax),
|
||||
'period': int(timeutils.delta_seconds(period_start,
|
||||
period_end)),
|
||||
'period': period,
|
||||
'period_start': period_start,
|
||||
'period_end': period_end}
|
||||
|
||||
@@ -431,7 +430,7 @@ class Connection(base.Connection):
|
||||
res = self._make_stats_query(event_filter).all()[0]
|
||||
|
||||
if not period:
|
||||
return [self._stats_result_to_dict(res, res.tsmin, res.tsmax)]
|
||||
return [self._stats_result_to_dict(res, 0, res.tsmin, res.tsmax)]
|
||||
|
||||
query = self._make_stats_query(event_filter)
|
||||
# HACK(jd) This is an awful method to compute stats by period, but
|
||||
@@ -449,9 +448,12 @@ class Connection(base.Connection):
|
||||
period_end = period_start + datetime.timedelta(seconds=period)
|
||||
q = query.filter(Meter.timestamp >= period_start)
|
||||
q = q.filter(Meter.timestamp < period_end)
|
||||
results.append(self._stats_result_to_dict(q.all()[0],
|
||||
period_start,
|
||||
period_end))
|
||||
results.append(self._stats_result_to_dict(
|
||||
result=q.all()[0],
|
||||
period=int(timeutils.delta_seconds(period_start, period_end)),
|
||||
period_start=period_start,
|
||||
period_end=period_end,
|
||||
))
|
||||
return results
|
||||
|
||||
|
||||
|
||||
@@ -773,6 +773,14 @@ class StatisticsTest(DBTestBase):
|
||||
assert results['sum'] == 27
|
||||
assert results['avg'] == 9
|
||||
|
||||
def test_no_period_in_query(self):
|
||||
f = storage.EventFilter(
|
||||
user='user-5',
|
||||
meter='volume.size',
|
||||
)
|
||||
results = self.conn.get_meter_statistics(f)[0]
|
||||
assert results['period'] == 0
|
||||
|
||||
def test_period_is_int(self):
|
||||
f = storage.EventFilter(
|
||||
meter='volume.size',
|
||||
|
||||
Reference in New Issue
Block a user