Enhances get_meters to return unique meters

Enhances get_meters in the storage layer
and API to accept a unique flag and return
back a list of unique meters for clients like
Horizon to consume.

DocImpact
Change-Id: Ifdcb907df867ae650eae733bc7b635a283939064
Closes-Bug: 1506959
This commit is contained in:
Rohit Jaiswal 2015-12-18 12:56:59 -08:00
parent 39f22e0f3b
commit 52f48485ee
8 changed files with 108 additions and 40 deletions

View File

@ -477,11 +477,12 @@ class MetersController(rest.RestController):
def _lookup(self, meter_name, *remainder):
return MeterController(meter_name), remainder
@wsme_pecan.wsexpose([Meter], [base.Query], int)
def get_all(self, q=None, limit=None):
@wsme_pecan.wsexpose([Meter], [base.Query], int, str)
def get_all(self, q=None, limit=None, unique=''):
"""Return all known meters, based on the data recorded so far.
:param q: Filter rules for the meters to be returned.
:param unique: flag to indicate unique meters to be returned.
"""
rbac.enforce('get_meters', pecan.request)
@ -494,5 +495,6 @@ class MetersController(rest.RestController):
q, pecan.request.storage_conn.get_meters,
['limit'], allow_timestamps=False)
return [Meter.from_db_model(m)
for m in pecan.request.storage_conn.get_meters(limit=limit,
for m in pecan.request.storage_conn.get_meters(
limit=limit, unique=strutils.bool_from_string(unique),
**kwargs)]

View File

@ -194,7 +194,7 @@ class Connection(object):
@staticmethod
def get_meters(user=None, project=None, resource=None, source=None,
metaquery=None, limit=None):
metaquery=None, limit=None, unique=False):
"""Return an iterable of model.Meter instances.
Iterable items containing meter information.
@ -204,6 +204,7 @@ class Connection(object):
:param source: Optional source filter.
:param metaquery: Optional dict with metadata to match on.
:param limit: Maximum number of results to return.
:param unique: If set to true, return only unique meter information.
"""
raise ceilometer.NotImplementedError('Meters not implemented')

View File

@ -241,7 +241,7 @@ class Connection(hbase_base.Connection, base.Connection):
metadata=md)
def get_meters(self, user=None, project=None, resource=None, source=None,
metaquery=None, limit=None):
metaquery=None, limit=None, unique=False):
"""Return an iterable of models.Meter instances
:param user: Optional ID for user that owns the resource.
@ -250,6 +250,7 @@ class Connection(hbase_base.Connection, base.Connection):
:param source: Optional source filter.
:param metaquery: Optional dict with metadata to match on.
:param limit: Maximum number of results to return.
:param unique: If set to true, return only unique meter information.
"""
if limit == 0:
return
@ -276,16 +277,30 @@ class Connection(hbase_base.Connection, base.Connection):
if limit and len(result) >= limit:
return
_m_rts, m_source, name, m_type, unit = m[0]
if unique:
meter_dict = {'name': name,
'type': m_type,
'unit': unit,
'resource_id': flatten_result['resource_id'],
'project_id': flatten_result['project_id'],
'user_id': flatten_result['user_id']}
'resource_id': None,
'project_id': None,
'user_id': None,
'source': None}
else:
meter_dict = {'name': name,
'type': m_type,
'unit': unit,
'resource_id':
flatten_result['resource_id'],
'project_id':
flatten_result['project_id'],
'user_id':
flatten_result['user_id']}
frozen_meter = frozenset(meter_dict.items())
if frozen_meter in result:
continue
result.add(frozen_meter)
if not unique:
meter_dict.update({'source': m_source
if m_source else None})

View File

@ -80,7 +80,7 @@ class Connection(base.Connection):
return []
def get_meters(self, user=None, project=None, resource=None, source=None,
limit=None, metaquery=None):
limit=None, metaquery=None, unique=False):
"""Return an iterable of dictionaries containing meter information.
{ 'name': name of the meter,
@ -96,6 +96,7 @@ class Connection(base.Connection):
:param source: Optional source filter.
:param limit: Maximum number of results to return.
:param metaquery: Optional dict with metadata to match on.
:param unique: If set to true, return only unique meter information.
"""
return []

View File

@ -493,7 +493,7 @@ class Connection(base.Connection):
)
def get_meters(self, user=None, project=None, resource=None, source=None,
metaquery=None, limit=None):
metaquery=None, limit=None, unique=False):
"""Return an iterable of api_models.Meter instances
:param user: Optional ID for user that owns the resource.
@ -502,6 +502,7 @@ class Connection(base.Connection):
:param source: Optional source filter.
:param metaquery: Optional dict with metadata to match on.
:param limit: Maximum number of results to return.
:param unique: If set to true, return only unique meter information.
"""
if limit == 0:
return
@ -514,10 +515,17 @@ class Connection(base.Connection):
# NOTE(gordc): get latest sample of each meter/resource. we do not
# filter here as we want to filter only on latest record.
session = self._engine_facade.get_session()
subq = session.query(func.max(models.Sample.id).label('id')).join(
models.Resource,
models.Resource.internal_id == models.Sample.resource_id).group_by(
models.Sample.meter_id, models.Resource.resource_id)
models.Resource.internal_id == models.Sample.resource_id)
if unique:
subq = subq.group_by(models.Sample.meter_id)
else:
subq = subq.group_by(models.Sample.meter_id,
models.Resource.resource_id)
if resource:
subq = subq.filter(models.Resource.resource_id == resource)
subq = subq.subquery()
@ -538,6 +546,18 @@ class Connection(base.Connection):
require_meter=False)
query_sample = query_sample.limit(limit) if limit else query_sample
if unique:
for row in query_sample.all():
yield api_models.Meter(
name=row.name,
type=row.type,
unit=row.unit,
resource_id=None,
project_id=None,
source=None,
user_id=None)
else:
for row in query_sample.all():
yield api_models.Meter(
name=row.name,

View File

@ -50,7 +50,7 @@ class Connection(base.Connection):
)
def get_meters(self, user=None, project=None, resource=None, source=None,
metaquery=None, limit=None):
metaquery=None, limit=None, unique=False):
"""Return an iterable of models.Meter instances
:param user: Optional ID for user that owns the resource.
@ -59,6 +59,7 @@ class Connection(base.Connection):
:param source: Optional source filter.
:param metaquery: Optional dict with metadata to match on.
:param limit: Maximum number of results to return.
:param unique: If set to true, return only unique meter information.
"""
if limit == 0:
return
@ -77,23 +78,44 @@ class Connection(base.Connection):
q.update(metaquery)
count = 0
if unique:
meter_names = set()
for r in self.db.resource.find(q):
for r_meter in r['meter']:
if unique:
if r_meter['counter_name'] in meter_names:
continue
else:
meter_names.add(r_meter['counter_name'])
if limit and count >= limit:
return
else:
count += 1
if unique:
yield models.Meter(
name=r_meter['counter_name'],
type=r_meter['counter_type'],
# Return empty string if 'counter_unit' is not valid for
# backward compatibility.
# Return empty string if 'counter_unit' is not valid
# for backward compatibility.
unit=r_meter.get('counter_unit', ''),
resource_id=None,
project_id=None,
source=None,
user_id=None)
else:
yield models.Meter(
name=r_meter['counter_name'],
type=r_meter['counter_type'],
# Return empty string if 'counter_unit' is not valid
# for backward compatibility.
unit=r_meter.get('counter_unit', ''),
resource_id=r['_id'],
project_id=r['project_id'],
source=r['source'],
user_id=r['user_id'],
)
user_id=r['user_id'])
def get_samples(self, sample_filter, limit=None):
"""Return an iterable of model.Sample instances.

View File

@ -271,6 +271,13 @@ class TestListMeters(v2.FunctionalTest):
self.assertEqual(set(['test_source', 'test_source1']),
set(r['source'] for r in data))
def test_list_unique_meters(self):
data = self.get_json('/meters?unique=True')
self.assertEqual(4, len(data))
self.assertEqual(set(['meter.test', 'meter.mine', 'meter.test.new',
u'meter.accent\xe9\u0437']),
set(r['name'] for r in data))
def test_meters_query_with_timestamp(self):
date_time = datetime.datetime(2012, 7, 2, 10, 41)
isotime = date_time.isoformat()

View File

@ -379,7 +379,7 @@ class TestQueryToKwArgs(tests_base.BaseTestCase):
exc = self.assertRaises(
wsme.exc.UnknownArgument,
utils.query_to_kwargs,
q, storage_base.Connection.get_meters, ['limit'])
q, storage_base.Connection.get_meters, ['limit', 'unique'])
valid_keys = ['project', 'resource', 'source', 'user']
msg = ("unrecognized field in query: %s, "
"valid keys: %s") % (q, valid_keys)