Merge "Remove redundant meter (name,type,unit) tuples from Resource model"
This commit is contained in:
commit
31d4c0ced4
@ -425,17 +425,10 @@ class Connection(base.Connection):
|
|||||||
resource = lambda x: x['resource_id']
|
resource = lambda x: x['resource_id']
|
||||||
meters = self.db.meter.find(q, sort=sort_instructions)
|
meters = self.db.meter.find(q, sort=sort_instructions)
|
||||||
for resource_id, r_meters in itertools.groupby(meters, key=resource):
|
for resource_id, r_meters in itertools.groupby(meters, key=resource):
|
||||||
resource_meters = []
|
|
||||||
# Because we have to know first/last timestamp, and we need a full
|
# Because we have to know first/last timestamp, and we need a full
|
||||||
# list of references to the resource's meters, we need a tuple
|
# list of references to the resource's meters, we need a tuple
|
||||||
# here.
|
# here.
|
||||||
r_meters = tuple(r_meters)
|
r_meters = tuple(r_meters)
|
||||||
for meter in r_meters:
|
|
||||||
resource_meters.append(models.ResourceMeter(
|
|
||||||
counter_name=meter['counter_name'],
|
|
||||||
counter_type=meter['counter_type'],
|
|
||||||
counter_unit=meter.get('counter_unit', ''))
|
|
||||||
)
|
|
||||||
latest_meter = r_meters[0]
|
latest_meter = r_meters[0]
|
||||||
last_ts = latest_meter['timestamp']
|
last_ts = latest_meter['timestamp']
|
||||||
first_ts = r_meters[-1]['timestamp']
|
first_ts = r_meters[-1]['timestamp']
|
||||||
@ -446,8 +439,7 @@ class Connection(base.Connection):
|
|||||||
last_sample_timestamp=last_ts,
|
last_sample_timestamp=last_ts,
|
||||||
source=latest_meter['source'],
|
source=latest_meter['source'],
|
||||||
user_id=latest_meter['user_id'],
|
user_id=latest_meter['user_id'],
|
||||||
metadata=latest_meter['resource_metadata'],
|
metadata=latest_meter['resource_metadata'])
|
||||||
meter=resource_meters)
|
|
||||||
|
|
||||||
def get_meters(self, user=None, project=None, resource=None, source=None,
|
def get_meters(self, user=None, project=None, resource=None, source=None,
|
||||||
metaquery={}, pagination=None):
|
metaquery={}, pagination=None):
|
||||||
|
@ -300,7 +300,7 @@ class Connection(base.Connection):
|
|||||||
if pagination:
|
if pagination:
|
||||||
raise NotImplementedError(_('Pagination not implemented'))
|
raise NotImplementedError(_('Pagination not implemented'))
|
||||||
|
|
||||||
def make_resource(data, first_ts, last_ts, meter_refs):
|
def make_resource(data, first_ts, last_ts):
|
||||||
"""Transform HBase fields to Resource model."""
|
"""Transform HBase fields to Resource model."""
|
||||||
# convert HBase metadata e.g. f:r_display_name to display_name
|
# convert HBase metadata e.g. f:r_display_name to display_name
|
||||||
data['f:metadata'] = _metadata_from_document(data)
|
data['f:metadata'] = _metadata_from_document(data)
|
||||||
@ -313,10 +313,6 @@ class Connection(base.Connection):
|
|||||||
source=data['f:source'],
|
source=data['f:source'],
|
||||||
user_id=data['f:user_id'],
|
user_id=data['f:user_id'],
|
||||||
metadata=data['f:metadata'],
|
metadata=data['f:metadata'],
|
||||||
meter=[
|
|
||||||
models.ResourceMeter(*(m.split("!")))
|
|
||||||
for m in meter_refs
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
meter_table = self.conn.table(self.METER_TABLE)
|
meter_table = self.conn.table(self.METER_TABLE)
|
||||||
|
|
||||||
@ -343,11 +339,6 @@ class Connection(base.Connection):
|
|||||||
meters, key=_resource_id_from_record_tuple):
|
meters, key=_resource_id_from_record_tuple):
|
||||||
meter_rows = [data[1] for data in sorted(
|
meter_rows = [data[1] for data in sorted(
|
||||||
r_meters, key=_timestamp_from_record_tuple)]
|
r_meters, key=_timestamp_from_record_tuple)]
|
||||||
meter_references = [
|
|
||||||
_format_meter_reference(m['f:counter_name'],
|
|
||||||
m['f:counter_type'],
|
|
||||||
m['f:counter_unit'])
|
|
||||||
for m in meter_rows]
|
|
||||||
|
|
||||||
latest_data = meter_rows[-1]
|
latest_data = meter_rows[-1]
|
||||||
min_ts = timeutils.parse_strtime(meter_rows[0]['f:timestamp'])
|
min_ts = timeutils.parse_strtime(meter_rows[0]['f:timestamp'])
|
||||||
@ -358,15 +349,13 @@ class Connection(base.Connection):
|
|||||||
yield make_resource(
|
yield make_resource(
|
||||||
latest_data,
|
latest_data,
|
||||||
min_ts,
|
min_ts,
|
||||||
max_ts,
|
max_ts
|
||||||
meter_references
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
yield make_resource(
|
yield make_resource(
|
||||||
latest_data,
|
latest_data,
|
||||||
min_ts,
|
min_ts,
|
||||||
max_ts,
|
max_ts
|
||||||
meter_references
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_meters(self, user=None, project=None, resource=None, source=None,
|
def get_meters(self, user=None, project=None, resource=None, source=None,
|
||||||
|
@ -661,16 +661,6 @@ class Connection(base.Connection):
|
|||||||
last_sample_timestamp=result['last_sample_timestamp'],
|
last_sample_timestamp=result['last_sample_timestamp'],
|
||||||
source=result['source'],
|
source=result['source'],
|
||||||
metadata=result['metadata'],
|
metadata=result['metadata'],
|
||||||
meter=[
|
|
||||||
models.ResourceMeter(
|
|
||||||
counter_name=m_n,
|
|
||||||
counter_type=m_t,
|
|
||||||
counter_unit=m_u,
|
|
||||||
)
|
|
||||||
for m_n, m_u, m_t in zip(result['meters_name'],
|
|
||||||
result['meters_unit'],
|
|
||||||
result['meters_type'])
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_meters(self, user=None, project=None, resource=None, source=None,
|
def get_meters(self, user=None, project=None, resource=None, source=None,
|
||||||
|
@ -467,14 +467,6 @@ class Connection(base.Connection):
|
|||||||
source=meter.sources[0].id,
|
source=meter.sources[0].id,
|
||||||
user_id=meter.user_id,
|
user_id=meter.user_id,
|
||||||
metadata=meter.resource_metadata,
|
metadata=meter.resource_metadata,
|
||||||
meter=[
|
|
||||||
api_models.ResourceMeter(
|
|
||||||
counter_name=m.counter_name,
|
|
||||||
counter_type=m.counter_type,
|
|
||||||
counter_unit=m.counter_unit,
|
|
||||||
)
|
|
||||||
for m in meter.resource.meters
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -140,8 +140,7 @@ class Resource(Model):
|
|||||||
def __init__(self, resource_id, project_id,
|
def __init__(self, resource_id, project_id,
|
||||||
first_sample_timestamp,
|
first_sample_timestamp,
|
||||||
last_sample_timestamp,
|
last_sample_timestamp,
|
||||||
source, user_id, metadata,
|
source, user_id, metadata):
|
||||||
meter):
|
|
||||||
"""Create a new resource.
|
"""Create a new resource.
|
||||||
|
|
||||||
:param resource_id: UUID of the resource
|
:param resource_id: UUID of the resource
|
||||||
@ -151,7 +150,6 @@ class Resource(Model):
|
|||||||
:param source: the identifier for the user/project id definition
|
:param source: the identifier for the user/project id definition
|
||||||
:param user_id: UUID of user owning the resource
|
:param user_id: UUID of user owning the resource
|
||||||
:param metadata: most current metadata for the resource (a dict)
|
:param metadata: most current metadata for the resource (a dict)
|
||||||
:param meter: list of the meters reporting data for the resource,
|
|
||||||
"""
|
"""
|
||||||
Model.__init__(self,
|
Model.__init__(self,
|
||||||
resource_id=resource_id,
|
resource_id=resource_id,
|
||||||
@ -161,28 +159,6 @@ class Resource(Model):
|
|||||||
source=source,
|
source=source,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
metadata=metadata,
|
metadata=metadata,
|
||||||
meter=meter,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ResourceMeter(Model):
|
|
||||||
"""The definitions of the meters for which data has been collected
|
|
||||||
for a resource.
|
|
||||||
|
|
||||||
See Resource.meter field.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, counter_name, counter_type, counter_unit):
|
|
||||||
"""Create a new resource meter.
|
|
||||||
|
|
||||||
:param counter_name: the name of the counter updating the resource
|
|
||||||
:param counter_type: one of gauge, delta, cumulative
|
|
||||||
:param counter_unit: official units name for the sample data
|
|
||||||
"""
|
|
||||||
Model.__init__(self,
|
|
||||||
counter_name=counter_name,
|
|
||||||
counter_type=counter_type,
|
|
||||||
counter_unit=counter_unit,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,8 +173,6 @@ class ResourceTest(DBTestBase,
|
|||||||
self.assertIn(resource.source, msgs_sources)
|
self.assertIn(resource.source, msgs_sources)
|
||||||
self.assertEqual(resource.user_id, 'user-id')
|
self.assertEqual(resource.user_id, 'user-id')
|
||||||
self.assertEqual(resource.metadata['display_name'], 'test-server')
|
self.assertEqual(resource.metadata['display_name'], 'test-server')
|
||||||
self.assertIn(models.ResourceMeter('instance', 'cumulative', ''),
|
|
||||||
resource.meter)
|
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
assert False, 'Never found resource-id'
|
assert False, 'Never found resource-id'
|
||||||
|
Loading…
Reference in New Issue
Block a user