Merge "Ensure missing period is treated consistently"

This commit is contained in:
Jenkins 2013-03-05 08:29:06 +00:00 committed by Gerrit Code Review
commit d9c4de87af
2 changed files with 17 additions and 7 deletions

View File

@ -399,7 +399,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,
@ -409,8 +409,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}
@ -437,7 +436,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
@ -455,9 +454,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

View File

@ -774,6 +774,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',