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
This commit is contained in:
Igor Degtiarov 2015-08-06 13:56:24 +03:00
parent f97b536736
commit c2a42bc906
4 changed files with 13 additions and 2 deletions

View File

@ -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'])

View File

@ -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)

View File

@ -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

View File

@ -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))