Merge "Move utils.dict_to_keyval to opendaylight"

This commit is contained in:
Zuul 2017-12-13 06:10:19 +00:00 committed by Gerrit Code Review
commit ca51d0d7ad
4 changed files with 43 additions and 46 deletions

View File

@ -20,7 +20,6 @@ from six.moves.urllib import parse as urlparse
from ceilometer.network.statistics import driver
from ceilometer.network.statistics.opendaylight import client
from ceilometer import utils
LOG = log.getLogger(__name__)
@ -406,14 +405,36 @@ class OpenDayLightDriver(driver.Driver):
return _get_int_sample('matchedCount', statistic, resource_id,
resource_meta)
@staticmethod
def _iter_flow(extractor, data):
def dict_to_keyval(self, value, key_base=None):
"""Expand a given dict to its corresponding key-value pairs.
Generated keys are fully qualified, delimited using dot notation.
ie. key = 'key.child_key.grandchild_key[0]'
"""
val_iter, key_func = None, None
if isinstance(value, dict):
val_iter = six.iteritems(value)
key_func = lambda k: key_base + '.' + k if key_base else k
elif isinstance(value, (tuple, list)):
val_iter = enumerate(value)
key_func = lambda k: key_base + '[%d]' % k
if val_iter:
for k, v in val_iter:
key_gen = key_func(k)
if isinstance(v, dict) or isinstance(v, (tuple, list)):
for key_gen, v in self.dict_to_keyval(v, key_gen):
yield key_gen, v
else:
yield key_gen, v
def _iter_flow(self, extractor, data):
for flow_statistic in data['flow']['flowStatistics']:
for statistic in flow_statistic['flowStatistic']:
resource_meta = {'flow_id': statistic['flow']['id'],
'table_id': statistic['tableId']}
for key, value in utils.dict_to_keyval(statistic['flow'],
'flow'):
for key, value in self.dict_to_keyval(statistic['flow'],
'flow'):
resource_meta[key.replace('.', '_')] = value
yield extractor(statistic,
flow_statistic['node']['id'],

View File

@ -132,6 +132,23 @@ class TestOpenDayLightDriverSpecial(_Base):
active_hosts_data = {"hostConfig": []}
inactive_hosts_data = {"hostConfig": []}
def test_dict_to_kv(self):
data = {'a': 'A',
'b': 'B',
'nested': {'a': 'A',
'b': 'B',
},
'nested2': [{'c': 'A'}, {'c': 'B'}]
}
pairs = list(self.driver.dict_to_keyval(data))
self.assertEqual([('a', 'A'),
('b', 'B'),
('nested.a', 'A'),
('nested.b', 'B'),
('nested2[0].c', 'A'),
('nested2[1].c', 'B')],
sorted(pairs, key=lambda x: x[0]))
def test_not_implemented_meter(self):
sample_data = self.driver.get_sample_data('egg',
self.fake_odl_url,

View File

@ -87,23 +87,6 @@ class TestUtils(base.BaseTestCase):
def test_decimal_to_dt_with_none_parameter(self):
self.assertIsNone(utils.decimal_to_dt(None))
def test_dict_to_kv(self):
data = {'a': 'A',
'b': 'B',
'nested': {'a': 'A',
'b': 'B',
},
'nested2': [{'c': 'A'}, {'c': 'B'}]
}
pairs = list(utils.dict_to_keyval(data))
self.assertEqual([('a', 'A'),
('b', 'B'),
('nested.a', 'A'),
('nested.b', 'B'),
('nested2[0].c', 'A'),
('nested2[1].c', 'B')],
sorted(pairs, key=lambda x: x[0]))
def test_hash_of_set(self):
x = ['a', 'b']
y = ['a', 'b', 'a']

View File

@ -117,30 +117,6 @@ def decimal_to_dt(dec):
return daittyme.replace(microsecond=int(round(micro)))
def dict_to_keyval(value, key_base=None):
"""Expand a given dict to its corresponding key-value pairs.
Generated keys are fully qualified, delimited using dot notation.
ie. key = 'key.child_key.grandchild_key[0]'
"""
val_iter, key_func = None, None
if isinstance(value, dict):
val_iter = six.iteritems(value)
key_func = lambda k: key_base + '.' + k if key_base else k
elif isinstance(value, (tuple, list)):
val_iter = enumerate(value)
key_func = lambda k: key_base + '[%d]' % k
if val_iter:
for k, v in val_iter:
key_gen = key_func(k)
if isinstance(v, dict) or isinstance(v, (tuple, list)):
for key_gen, v in dict_to_keyval(v, key_gen):
yield key_gen, v
else:
yield key_gen, v
def hash_of_set(s):
return str(hash(frozenset(s)))