Fix sqlalchemy for show_data and v1 web api
When sqlalchemy (e.g. mysql) is used for ceilometer, show_data.py and web api do not work because some attributes and keys are different, such as meter/meters, the patch is fix them and make both can work. Change-Id: I4b827c3e6664213f1401b183ccbc3a1aaae872aa Signed-off-by: Shane Wang <shane.wang@intel.com>
This commit is contained in:
parent
c38ec40c5b
commit
36eccfb76a
@ -35,24 +35,42 @@ class SQLAlchemyStorage(base.StorageEngine):
|
||||
Tables:
|
||||
|
||||
- user
|
||||
- { _id: user id
|
||||
source: [ array of source ids reporting for the user ]
|
||||
}
|
||||
- { id: user uuid }
|
||||
- source
|
||||
- { id: source id }
|
||||
- project
|
||||
- { _id: project id
|
||||
source: [ array of source ids reporting for the project ]
|
||||
}
|
||||
- { id: project uuid }
|
||||
- meter
|
||||
- the raw incoming data
|
||||
- { id: meter id
|
||||
counter_name: counter name
|
||||
user_id: user uuid (->user.id)
|
||||
project_id: project uuid (->project.id)
|
||||
resource_id: resource uuid (->resource.id)
|
||||
resource_metadata: metadata dictionaries
|
||||
counter_type: counter type
|
||||
counter_volume: counter volume
|
||||
timestamp: datetime
|
||||
message_signature: message signature
|
||||
message_id: message uuid
|
||||
}
|
||||
- resource
|
||||
- the metadata for resources
|
||||
- { _id: uuid of resource,
|
||||
metadata: metadata dictionaries
|
||||
timestamp: datetime of last update
|
||||
user_id: uuid
|
||||
project_id: uuid
|
||||
meter: [ array of {counter_name: string, counter_type: string} ]
|
||||
}
|
||||
- { id: resource uuid
|
||||
resource_metadata: metadata dictionaries
|
||||
received_timestamp: received datetime
|
||||
timestamp: datetime
|
||||
project_id: project uuid (->project.id)
|
||||
user_id: user uuid (->user.id)
|
||||
}
|
||||
- sourceassoc
|
||||
- the relationships
|
||||
- { meter_id: meter id (->meter.id)
|
||||
project_id: project uuid (->project.id)
|
||||
resource_id: resource uuid (->resource.id)
|
||||
user_id: user uuid (->user.id)
|
||||
source_id: source id (->source.id)
|
||||
}
|
||||
"""
|
||||
|
||||
OPTIONS = []
|
||||
@ -231,6 +249,12 @@ class Connection(base.Connection):
|
||||
# caller's expectations.
|
||||
r['resource_id'] = r['id']
|
||||
del r['id']
|
||||
# Replace the 'resource_metadata' with 'metadata'
|
||||
r['metadata'] = r['resource_metadata']
|
||||
del r['resource_metadata']
|
||||
# Replace the 'meters' with 'meter'
|
||||
r['meter'] = r['meters']
|
||||
del r['meters']
|
||||
yield r
|
||||
|
||||
def get_raw_events(self, event_filter):
|
||||
@ -300,7 +324,7 @@ def model_query(*args, **kwargs):
|
||||
|
||||
def row2dict(row, srcflag=None):
|
||||
"""Convert User, Project, Meter, Resource instance to dictionary object
|
||||
with nested Source(s)
|
||||
with nested Source(s) and Meter(s)
|
||||
"""
|
||||
d = copy.copy(row.__dict__)
|
||||
for col in ['_sa_instance_state', 'sources']:
|
||||
@ -308,4 +332,6 @@ def row2dict(row, srcflag=None):
|
||||
del d[col]
|
||||
if not srcflag:
|
||||
d['sources'] = map(lambda x: row2dict(x, True), row.sources)
|
||||
if d.get('meters') is not None:
|
||||
d['meters'] = map(lambda x: row2dict(x, True), d['meters'])
|
||||
return d
|
||||
|
@ -236,7 +236,7 @@ class ResourceTest(SQLAlchemyEngineTestBase):
|
||||
|
||||
def test_new_resource_metadata(self):
|
||||
resource = self.session.query(Resource).get('resource-id')
|
||||
assert hasattr(resource, 'metadata')
|
||||
assert hasattr(resource, 'resource_metadata')
|
||||
metadata = resource.resource_metadata
|
||||
assert metadata['display_name'] == 'test-server'
|
||||
|
||||
@ -249,10 +249,10 @@ class ResourceTest(SQLAlchemyEngineTestBase):
|
||||
assert resource['resource_id'] == 'resource-id'
|
||||
assert resource['project_id'] == 'project-id'
|
||||
assert resource['user_id'] == 'user-id'
|
||||
assert 'resource_metadata' in resource
|
||||
assert 'meters' in resource
|
||||
assert 'metadata' in resource
|
||||
assert 'meter' in resource
|
||||
foo = map(lambda x: [x['counter_name'], x['counter_type']],
|
||||
resource['meters'])
|
||||
resource['meter'])
|
||||
assert ['instance', 'cumulative'] in foo
|
||||
break
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user