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:
parent
a6748c94fd
commit
aa75b68307
@ -289,10 +289,10 @@ def resource_list(request, query=None, ceilometer_usage_object=None):
|
||||
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."""
|
||||
samples = ceilometerclient(request).samples.list(meter_name=meter_name,
|
||||
q=query)
|
||||
q=query, limit=limit)
|
||||
return [Sample(s) for s in samples]
|
||||
|
||||
|
||||
|
@ -77,12 +77,13 @@ class MeteringLineChartTabTests(test.BaseAdminViewTests):
|
||||
self.assertEqual(data.get('settings'), {})
|
||||
|
||||
@test.create_stubs({api.keystone: ('tenant_list',),
|
||||
api.ceilometer: ('meter_list',
|
||||
api.ceilometer: ('sample_list',
|
||||
'statistic_list',
|
||||
), })
|
||||
def test_stats_for_line_chart(self):
|
||||
api.ceilometer.meter_list(IsA(http.HttpRequest))\
|
||||
.AndReturn(self.testdata.meters.list())
|
||||
api.ceilometer.sample_list(IsA(http.HttpRequest),
|
||||
IsA(unicode),
|
||||
limit=IsA(int)).AndReturn([])
|
||||
api.ceilometer.statistic_list(IsA(http.HttpRequest),
|
||||
'memory',
|
||||
period=IsA(int),
|
||||
@ -109,12 +110,13 @@ class MeteringLineChartTabTests(test.BaseAdminViewTests):
|
||||
expected_names)
|
||||
|
||||
@test.create_stubs({api.keystone: ('tenant_list',),
|
||||
api.ceilometer: ('meter_list',
|
||||
api.ceilometer: ('sample_list',
|
||||
'statistic_list',
|
||||
), })
|
||||
def test_stats_for_line_chart_attr_max(self):
|
||||
api.ceilometer.meter_list(IsA(http.HttpRequest))\
|
||||
.AndReturn(self.testdata.meters.list())
|
||||
api.ceilometer.sample_list(IsA(http.HttpRequest),
|
||||
IsA(unicode),
|
||||
limit=IsA(int)).AndReturn([])
|
||||
api.ceilometer.statistic_list(IsA(http.HttpRequest),
|
||||
'memory', period=IsA(int),
|
||||
query=IsA(list))\
|
||||
@ -141,13 +143,14 @@ class MeteringLineChartTabTests(test.BaseAdminViewTests):
|
||||
expected_names)
|
||||
|
||||
@test.create_stubs({api.keystone: ('tenant_list',),
|
||||
api.ceilometer: ('meter_list',
|
||||
api.ceilometer: ('sample_list',
|
||||
'resource_list',
|
||||
'statistic_list'
|
||||
), })
|
||||
def test_stats_for_line_chart_no_group(self):
|
||||
api.ceilometer.meter_list(IsA(http.HttpRequest))\
|
||||
.AndReturn(self.testdata.meters.list())
|
||||
api.ceilometer.sample_list(IsA(http.HttpRequest),
|
||||
IsA(unicode),
|
||||
limit=IsA(int)).AndReturn([])
|
||||
api.ceilometer.resource_list(IsA(http.HttpRequest), query=None,
|
||||
ceilometer_usage_object=None)\
|
||||
.AndReturn(self.testdata.api_resources.list())
|
||||
|
@ -26,8 +26,9 @@ class CeilometerApiTests(test.APITestCase):
|
||||
meter_name = "meter_name"
|
||||
ceilometerclient = self.stub_ceilometerclient()
|
||||
ceilometerclient.samples = self.mox.CreateMockAnything()
|
||||
ceilometerclient.samples.list(meter_name=meter_name, q=[]).\
|
||||
AndReturn(samples)
|
||||
ceilometerclient.samples.list(meter_name=meter_name,
|
||||
q=[],
|
||||
limit=None).AndReturn(samples)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
ret_list = api.ceilometer.sample_list(self.request,
|
||||
|
@ -165,6 +165,14 @@ def normalize_series_by_unit(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):
|
||||
def __init__(self, request, date_from, date_to,
|
||||
period=None, additional_query=[]):
|
||||
@ -196,11 +204,7 @@ class ProjectAggregatesQuery(object):
|
||||
self.queries[tenant.name] = tenant_query
|
||||
|
||||
def query(self, meter):
|
||||
meter_list = [m for m in api.ceilometer.meter_list(self.request)
|
||||
if m.name == meter]
|
||||
unit = ""
|
||||
if len(meter_list) > 0:
|
||||
unit = meter_list[0].unit
|
||||
unit = get_unit(meter, self.request)
|
||||
ceilometer_usage = api.ceilometer.CeilometerUsage(self.request)
|
||||
resources = ceilometer_usage.resource_aggregates_with_statistics(
|
||||
self.queries, [meter], period=self.period,
|
||||
@ -231,12 +235,7 @@ class MeterQuery(ProjectAggregatesQuery):
|
||||
return True
|
||||
return False
|
||||
|
||||
meter_list = [m for m in api.ceilometer.meter_list(self.request)
|
||||
if m.name == meter]
|
||||
|
||||
unit = ""
|
||||
if len(meter_list) > 0:
|
||||
unit = meter_list[0].unit
|
||||
unit = get_unit(meter, self.request)
|
||||
|
||||
ceilometer_usage = api.ceilometer.CeilometerUsage(self.request)
|
||||
resources = ceilometer_usage.resources_with_statistics(
|
||||
|
Loading…
Reference in New Issue
Block a user