From 2d049c1da309b3b55eba13d36411c4cf491dbca8 Mon Sep 17 00:00:00 2001 From: Doug Hellmann <doug.hellmann@dreamhost.com> Date: Mon, 4 Mar 2013 12:38:18 -0500 Subject: [PATCH] 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> --- ceilometer/storage/impl_sqlalchemy.py | 16 +++++++++------- tests/storage/base.py | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ceilometer/storage/impl_sqlalchemy.py b/ceilometer/storage/impl_sqlalchemy.py index 0732b6f7c2..80c962958c 100644 --- a/ceilometer/storage/impl_sqlalchemy.py +++ b/ceilometer/storage/impl_sqlalchemy.py @@ -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 diff --git a/tests/storage/base.py b/tests/storage/base.py index 5831e9ef59..9b2768b16a 100644 --- a/tests/storage/base.py +++ b/tests/storage/base.py @@ -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',