Optimize getting unit in "metering" dashboard

Currently we using a meter-list for getting a "unit" for metric.
Meter list get all available metric on environment,
it's may be a thousands of records.
It's better to use sample-list for this metric with "limit" 1.
This request gets a 1 sample for queried metric.

Change-Id: I4bf0ab7ebcbcae1ddc90d8ee01d5f5a84ed4fbba
Related-bug: #1444456
This commit is contained in:
Ilya Tyaptin 2015-04-15 17:03:20 +03:00
parent a6748c94fd
commit aa75b68307
4 changed files with 27 additions and 24 deletions

View File

@ -289,10 +289,10 @@ def resource_list(request, query=None, ceilometer_usage_object=None):
return [Resource(r, ceilometer_usage_object) for r in resources] return [Resource(r, ceilometer_usage_object) for r in resources]
def sample_list(request, meter_name, query=None): def sample_list(request, meter_name, query=None, limit=None):
"""List the samples for this meters.""" """List the samples for this meters."""
samples = ceilometerclient(request).samples.list(meter_name=meter_name, samples = ceilometerclient(request).samples.list(meter_name=meter_name,
q=query) q=query, limit=limit)
return [Sample(s) for s in samples] return [Sample(s) for s in samples]

View File

@ -77,12 +77,13 @@ class MeteringLineChartTabTests(test.BaseAdminViewTests):
self.assertEqual(data.get('settings'), {}) self.assertEqual(data.get('settings'), {})
@test.create_stubs({api.keystone: ('tenant_list',), @test.create_stubs({api.keystone: ('tenant_list',),
api.ceilometer: ('meter_list', api.ceilometer: ('sample_list',
'statistic_list', 'statistic_list',
), }) ), })
def test_stats_for_line_chart(self): def test_stats_for_line_chart(self):
api.ceilometer.meter_list(IsA(http.HttpRequest))\ api.ceilometer.sample_list(IsA(http.HttpRequest),
.AndReturn(self.testdata.meters.list()) IsA(unicode),
limit=IsA(int)).AndReturn([])
api.ceilometer.statistic_list(IsA(http.HttpRequest), api.ceilometer.statistic_list(IsA(http.HttpRequest),
'memory', 'memory',
period=IsA(int), period=IsA(int),
@ -109,12 +110,13 @@ class MeteringLineChartTabTests(test.BaseAdminViewTests):
expected_names) expected_names)
@test.create_stubs({api.keystone: ('tenant_list',), @test.create_stubs({api.keystone: ('tenant_list',),
api.ceilometer: ('meter_list', api.ceilometer: ('sample_list',
'statistic_list', 'statistic_list',
), }) ), })
def test_stats_for_line_chart_attr_max(self): def test_stats_for_line_chart_attr_max(self):
api.ceilometer.meter_list(IsA(http.HttpRequest))\ api.ceilometer.sample_list(IsA(http.HttpRequest),
.AndReturn(self.testdata.meters.list()) IsA(unicode),
limit=IsA(int)).AndReturn([])
api.ceilometer.statistic_list(IsA(http.HttpRequest), api.ceilometer.statistic_list(IsA(http.HttpRequest),
'memory', period=IsA(int), 'memory', period=IsA(int),
query=IsA(list))\ query=IsA(list))\
@ -141,13 +143,14 @@ class MeteringLineChartTabTests(test.BaseAdminViewTests):
expected_names) expected_names)
@test.create_stubs({api.keystone: ('tenant_list',), @test.create_stubs({api.keystone: ('tenant_list',),
api.ceilometer: ('meter_list', api.ceilometer: ('sample_list',
'resource_list', 'resource_list',
'statistic_list' 'statistic_list'
), }) ), })
def test_stats_for_line_chart_no_group(self): def test_stats_for_line_chart_no_group(self):
api.ceilometer.meter_list(IsA(http.HttpRequest))\ api.ceilometer.sample_list(IsA(http.HttpRequest),
.AndReturn(self.testdata.meters.list()) IsA(unicode),
limit=IsA(int)).AndReturn([])
api.ceilometer.resource_list(IsA(http.HttpRequest), query=None, api.ceilometer.resource_list(IsA(http.HttpRequest), query=None,
ceilometer_usage_object=None)\ ceilometer_usage_object=None)\
.AndReturn(self.testdata.api_resources.list()) .AndReturn(self.testdata.api_resources.list())

View File

@ -26,8 +26,9 @@ class CeilometerApiTests(test.APITestCase):
meter_name = "meter_name" meter_name = "meter_name"
ceilometerclient = self.stub_ceilometerclient() ceilometerclient = self.stub_ceilometerclient()
ceilometerclient.samples = self.mox.CreateMockAnything() ceilometerclient.samples = self.mox.CreateMockAnything()
ceilometerclient.samples.list(meter_name=meter_name, q=[]).\ ceilometerclient.samples.list(meter_name=meter_name,
AndReturn(samples) q=[],
limit=None).AndReturn(samples)
self.mox.ReplayAll() self.mox.ReplayAll()
ret_list = api.ceilometer.sample_list(self.request, ret_list = api.ceilometer.sample_list(self.request,

View File

@ -165,6 +165,14 @@ def normalize_series_by_unit(series):
return series return series
def get_unit(meter, request):
sample_list = api.ceilometer.sample_list(request, meter, limit=1)
unit = ""
if sample_list:
unit = sample_list[0].counter_unit
return unit
class ProjectAggregatesQuery(object): class ProjectAggregatesQuery(object):
def __init__(self, request, date_from, date_to, def __init__(self, request, date_from, date_to,
period=None, additional_query=[]): period=None, additional_query=[]):
@ -196,11 +204,7 @@ class ProjectAggregatesQuery(object):
self.queries[tenant.name] = tenant_query self.queries[tenant.name] = tenant_query
def query(self, meter): def query(self, meter):
meter_list = [m for m in api.ceilometer.meter_list(self.request) unit = get_unit(meter, self.request)
if m.name == meter]
unit = ""
if len(meter_list) > 0:
unit = meter_list[0].unit
ceilometer_usage = api.ceilometer.CeilometerUsage(self.request) ceilometer_usage = api.ceilometer.CeilometerUsage(self.request)
resources = ceilometer_usage.resource_aggregates_with_statistics( resources = ceilometer_usage.resource_aggregates_with_statistics(
self.queries, [meter], period=self.period, self.queries, [meter], period=self.period,
@ -231,12 +235,7 @@ class MeterQuery(ProjectAggregatesQuery):
return True return True
return False return False
meter_list = [m for m in api.ceilometer.meter_list(self.request) unit = get_unit(meter, self.request)
if m.name == meter]
unit = ""
if len(meter_list) > 0:
unit = meter_list[0].unit
ceilometer_usage = api.ceilometer.CeilometerUsage(self.request) ceilometer_usage = api.ceilometer.CeilometerUsage(self.request)
resources = ceilometer_usage.resources_with_statistics( resources = ceilometer_usage.resources_with_statistics(