Cast Int64 values to int, float in statistics
Currently statistics requests fail with type errors. It causes by the fact that pymongo returns bson.Int64 instead of float or int for big nums. It affects a `cpu` meter usually. Change-Id: I744a9f79a4bbc3b2ecdd73c126b0859f3f8a733c Closes-bug: #1532661
This commit is contained in:
parent
e5bbd1a90c
commit
1d73a6f12f
@ -591,9 +591,10 @@ class Connection(pymongo_base.Connection):
|
|||||||
|
|
||||||
def _stats_result_aggregates(self, result, aggregate):
|
def _stats_result_aggregates(self, result, aggregate):
|
||||||
stats_args = {}
|
stats_args = {}
|
||||||
for attr in Connection.STANDARD_AGGREGATES.keys():
|
for attr, func in Connection.STANDARD_AGGREGATES.items():
|
||||||
if attr in result:
|
if attr in result:
|
||||||
stats_args[attr] = result[attr]
|
stats_args.update(func.finalize(result,
|
||||||
|
version_array=self.version))
|
||||||
|
|
||||||
if aggregate:
|
if aggregate:
|
||||||
stats_args['aggregate'] = {}
|
stats_args['aggregate'] = {}
|
||||||
|
@ -43,7 +43,8 @@ OP_SIGN = {'lt': '$lt', 'le': '$lte', 'ne': '$ne', 'gt': '$gt', 'ge': '$gte'}
|
|||||||
MINIMUM_COMPATIBLE_MONGODB_VERSION = [2, 4]
|
MINIMUM_COMPATIBLE_MONGODB_VERSION = [2, 4]
|
||||||
COMPLETE_AGGREGATE_COMPATIBLE_VERSION = [2, 6]
|
COMPLETE_AGGREGATE_COMPATIBLE_VERSION = [2, 6]
|
||||||
|
|
||||||
FINALIZE_AGGREGATION_LAMBDA = lambda result, param=None: float(result)
|
FINALIZE_FLOAT_LAMBDA = lambda result, param=None: float(result)
|
||||||
|
FINALIZE_INT_LAMBDA = lambda result, param=None: int(result)
|
||||||
CARDINALITY_VALIDATION = (lambda name, param: param in ['resource_id',
|
CARDINALITY_VALIDATION = (lambda name, param: param in ['resource_id',
|
||||||
'user_id',
|
'user_id',
|
||||||
'project_id',
|
'project_id',
|
||||||
@ -515,7 +516,7 @@ class AggregationFields(object):
|
|||||||
finalize=None,
|
finalize=None,
|
||||||
parametrized=False,
|
parametrized=False,
|
||||||
validate=None):
|
validate=None):
|
||||||
self._finalize = finalize or FINALIZE_AGGREGATION_LAMBDA
|
self._finalize = finalize or FINALIZE_FLOAT_LAMBDA
|
||||||
self.group = lambda *args: group(*args) if parametrized else group
|
self.group = lambda *args: group(*args) if parametrized else group
|
||||||
self.project = (lambda *args: project(*args)
|
self.project = (lambda *args: project(*args)
|
||||||
if parametrized else project)
|
if parametrized else project)
|
||||||
@ -566,23 +567,28 @@ class Aggregation(object):
|
|||||||
SUM_AGGREGATION = Aggregation(
|
SUM_AGGREGATION = Aggregation(
|
||||||
"sum", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
"sum", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
||||||
{"sum": {"$sum": "$counter_volume"}},
|
{"sum": {"$sum": "$counter_volume"}},
|
||||||
{"sum": "$sum"}))
|
{"sum": "$sum"},
|
||||||
|
))
|
||||||
AVG_AGGREGATION = Aggregation(
|
AVG_AGGREGATION = Aggregation(
|
||||||
"avg", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
"avg", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
||||||
{"avg": {"$avg": "$counter_volume"}},
|
{"avg": {"$avg": "$counter_volume"}},
|
||||||
{"avg": "$avg"}))
|
{"avg": "$avg"},
|
||||||
|
))
|
||||||
MIN_AGGREGATION = Aggregation(
|
MIN_AGGREGATION = Aggregation(
|
||||||
"min", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
"min", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
||||||
{"min": {"$min": "$counter_volume"}},
|
{"min": {"$min": "$counter_volume"}},
|
||||||
{"min": "$min"}))
|
{"min": "$min"},
|
||||||
|
))
|
||||||
MAX_AGGREGATION = Aggregation(
|
MAX_AGGREGATION = Aggregation(
|
||||||
"max", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
"max", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
||||||
{"max": {"$max": "$counter_volume"}},
|
{"max": {"$max": "$counter_volume"}},
|
||||||
{"max": "$max"}))
|
{"max": "$max"},
|
||||||
|
))
|
||||||
COUNT_AGGREGATION = Aggregation(
|
COUNT_AGGREGATION = Aggregation(
|
||||||
"count", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
"count", AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
||||||
{"count": {"$sum": 1}},
|
{"count": {"$sum": 1}},
|
||||||
{"count": "$count"}))
|
{"count": "$count"},
|
||||||
|
FINALIZE_INT_LAMBDA))
|
||||||
STDDEV_AGGREGATION = Aggregation(
|
STDDEV_AGGREGATION = Aggregation(
|
||||||
"stddev",
|
"stddev",
|
||||||
AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
AggregationFields(MINIMUM_COMPATIBLE_MONGODB_VERSION,
|
||||||
|
@ -1649,3 +1649,45 @@ class TestUnparameterizedAggregates(v2.FunctionalTest):
|
|||||||
places=4)
|
places=4)
|
||||||
for a in standard_aggregates:
|
for a in standard_aggregates:
|
||||||
self.assertNotIn(a, r)
|
self.assertNotIn(a, r)
|
||||||
|
|
||||||
|
|
||||||
|
@tests_db.run_with('mongodb')
|
||||||
|
class TestBigValueStatistics(v2.FunctionalTest):
|
||||||
|
|
||||||
|
PATH = '/meters/volume.size/statistics'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestBigValueStatistics, self).setUp()
|
||||||
|
for i in range(0, 3):
|
||||||
|
s = sample.Sample(
|
||||||
|
'volume.size',
|
||||||
|
'gauge',
|
||||||
|
'GiB',
|
||||||
|
(i + 1) * (10 ** 12),
|
||||||
|
'user-id',
|
||||||
|
'project1',
|
||||||
|
'resource-id',
|
||||||
|
timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i),
|
||||||
|
resource_metadata={'display_name': 'test-volume',
|
||||||
|
'tag': 'self.sample',
|
||||||
|
},
|
||||||
|
source='source1',
|
||||||
|
)
|
||||||
|
msg = utils.meter_message_from_counter(
|
||||||
|
s, self.CONF.publisher.telemetry_secret,
|
||||||
|
)
|
||||||
|
self.conn.record_metering_data(msg)
|
||||||
|
|
||||||
|
def test_big_value_statistics(self):
|
||||||
|
data = self.get_json(self.PATH)
|
||||||
|
|
||||||
|
expected_values = {'count': 3,
|
||||||
|
'min': 10 ** 12,
|
||||||
|
'max': 3 * 10 ** 12,
|
||||||
|
'sum': 6 * 10 ** 12,
|
||||||
|
'avg': 2 * 10 ** 12}
|
||||||
|
self.assertEqual(1, len(data))
|
||||||
|
for d in data:
|
||||||
|
for name, expected_value in expected_values.items():
|
||||||
|
self.assertIn(name, d)
|
||||||
|
self.assertEqual(expected_value, d[name])
|
||||||
|
Loading…
Reference in New Issue
Block a user