diff --git a/ceilometer/storage/impl_sqlalchemy.py b/ceilometer/storage/impl_sqlalchemy.py index 757ba89090..9de6d27560 100644 --- a/ceilometer/storage/impl_sqlalchemy.py +++ b/ceilometer/storage/impl_sqlalchemy.py @@ -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 diff --git a/tests/storage/base.py b/tests/storage/base.py index d2b76e604e..8397b4b3ef 100644 --- a/tests/storage/base.py +++ b/tests/storage/base.py @@ -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',