diff --git a/openstack_dashboard/api/ceilometer.py b/openstack_dashboard/api/ceilometer.py index bf09b97af0..49dd42006e 100644 --- a/openstack_dashboard/api/ceilometer.py +++ b/openstack_dashboard/api/ceilometer.py @@ -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] diff --git a/openstack_dashboard/dashboards/admin/metering/tests.py b/openstack_dashboard/dashboards/admin/metering/tests.py index 7d63d25008..e4b1ee5a6e 100644 --- a/openstack_dashboard/dashboards/admin/metering/tests.py +++ b/openstack_dashboard/dashboards/admin/metering/tests.py @@ -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()) diff --git a/openstack_dashboard/test/api_tests/ceilometer_tests.py b/openstack_dashboard/test/api_tests/ceilometer_tests.py index 5902c00637..36e553ee21 100644 --- a/openstack_dashboard/test/api_tests/ceilometer_tests.py +++ b/openstack_dashboard/test/api_tests/ceilometer_tests.py @@ -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, diff --git a/openstack_dashboard/utils/metering.py b/openstack_dashboard/utils/metering.py index 9cd42fe61a..e5eaebf0e5 100644 --- a/openstack_dashboard/utils/metering.py +++ b/openstack_dashboard/utils/metering.py @@ -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(