From c2a42bc90641cc5456689e50b4758a9ed1b0c229 Mon Sep 17 00:00:00 2001 From: Igor Degtiarov Date: Thu, 6 Aug 2015 13:56:24 +0300 Subject: [PATCH] Cast Int64 values to float Currently MongoDB and HBase return integer values from samples and statistics, but wsme expected a float type. In this CR MongoDB and HBase statistics and sample values cast to float. Change-Id: I97ccf8f8cd10e065ebd80ccbe1a688bf8d722b2f Closes-Bug: #1474031 --- ceilometer/storage/impl_hbase.py | 2 ++ ceilometer/storage/mongo/utils.py | 4 ++-- ceilometer/storage/pymongo_base.py | 2 ++ .../tests/functional/storage/test_storage_scenarios.py | 7 +++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ceilometer/storage/impl_hbase.py b/ceilometer/storage/impl_hbase.py index 2c491d18..f83ee0d8 100644 --- a/ceilometer/storage/impl_hbase.py +++ b/ceilometer/storage/impl_hbase.py @@ -310,6 +310,8 @@ class Connection(hbase_base.Connection, base.Connection): limit=limit, columns=columns) for ignored, meter in gen: d_meter = hbase_utils.deserialize_entry(meter)[0] + d_meter['message']['counter_volume'] = ( + float(d_meter['message']['counter_volume'])) d_meter['message']['recorded_at'] = d_meter['recorded_at'] yield models.Sample(**d_meter['message']) diff --git a/ceilometer/storage/mongo/utils.py b/ceilometer/storage/mongo/utils.py index 6a1efc9c..08fc7768 100644 --- a/ceilometer/storage/mongo/utils.py +++ b/ceilometer/storage/mongo/utils.py @@ -48,7 +48,7 @@ OP_SIGN = {'lt': '$lt', 'le': '$lte', 'ne': '$ne', 'gt': '$gt', 'ge': '$gte'} MINIMUM_COMPATIBLE_MONGODB_VERSION = [2, 4] COMPLETE_AGGREGATE_COMPATIBLE_VERSION = [2, 6] -TRIVIAL_LAMBDA = lambda result, param=None: result +FINALIZE_AGGREGATION_LAMBDA = lambda result, param=None: float(result) CARDINALITY_VALIDATION = (lambda name, param: param in ['resource_id', 'user_id', 'project_id', @@ -519,7 +519,7 @@ class AggregationFields(object): finalize=None, parametrized=False, validate=None): - self._finalize = finalize or TRIVIAL_LAMBDA + self._finalize = finalize or FINALIZE_AGGREGATION_LAMBDA self.group = lambda *args: group(*args) if parametrized else group self.project = (lambda *args: project(*args) if parametrized else project) diff --git a/ceilometer/storage/pymongo_base.py b/ceilometer/storage/pymongo_base.py index 365339b4..93c696d5 100644 --- a/ceilometer/storage/pymongo_base.py +++ b/ceilometer/storage/pymongo_base.py @@ -139,6 +139,8 @@ class Connection(base.Connection): del s['_id'] # Backward compatibility for samples without units s['counter_unit'] = s.get('counter_unit', '') + # Compatibility with MongoDB 3.+ + s['counter_volume'] = float(s.get('counter_volume')) # Tolerate absence of recorded_at in older datapoints s['recorded_at'] = s.get('recorded_at') # Check samples for metadata and "unquote" key if initially it diff --git a/ceilometer/tests/functional/storage/test_storage_scenarios.py b/ceilometer/tests/functional/storage/test_storage_scenarios.py index 611b01af..a86dd2e1 100644 --- a/ceilometer/tests/functional/storage/test_storage_scenarios.py +++ b/ceilometer/tests/functional/storage/test_storage_scenarios.py @@ -406,6 +406,13 @@ class RawSampleTest(DBTestBase, source='test-4' )) + def test_get_sample_counter_volume(self): + # NOTE(idegtiarov) Because wsme expected a float type of data this test + # checks type of counter_volume received from database. + f = storage.SampleFilter() + result = next(self.conn.get_samples(f, limit=1)) + self.assertIsInstance(result.counter_volume, float) + def test_get_samples_limit_zero(self): f = storage.SampleFilter() results = list(self.conn.get_samples(f, limit=0))