From 5154baa6fd0a33b46ef5fedb0e954c96f82fae03 Mon Sep 17 00:00:00 2001 From: gcalvino Date: Wed, 23 May 2018 12:20:36 +0200 Subject: [PATCH] Optimization of "list_metrics" function Signed-off-by: gcalvino Change-Id: I815e0f11049d49ef0ed13e4180241923b4a58496 --- osm_mon/plugins/OpenStack/Gnocchi/metrics.py | 109 +++++++++++------- .../test/OpenStack/unit/test_metric_calls.py | 9 +- 2 files changed, 71 insertions(+), 47 deletions(-) diff --git a/osm_mon/plugins/OpenStack/Gnocchi/metrics.py b/osm_mon/plugins/OpenStack/Gnocchi/metrics.py index bb396e5..ea2f2d2 100644 --- a/osm_mon/plugins/OpenStack/Gnocchi/metrics.py +++ b/osm_mon/plugins/OpenStack/Gnocchi/metrics.py @@ -289,9 +289,12 @@ class Metrics(object): # Check for a specified list try: # Check if the metric_name was specified for the list - metric_name = values['metric_name'].lower() - if metric_name not in METRIC_MAPPINGS.keys(): - log.warning("This metric is not supported, won't be listed.") + if values['metric_name']: + metric_name = values['metric_name'].lower() + if metric_name not in METRIC_MAPPINGS.keys(): + log.warning("This metric is not supported, won't be listed.") + metric_name = None + else: metric_name = None except KeyError as exc: log.info("Metric name is not specified: %s", exc) @@ -304,47 +307,73 @@ class Metrics(object): resource = None try: - url = "{}/v1/metric?sort=name:asc".format(endpoint) - result = Common.perform_request( - url, auth_token, req_type="get") - metrics = [] - metrics_partial = json.loads(result.text) - for metric in metrics_partial: - metrics.append(metric) - - while len(json.loads(result.text)) > 0: - last_metric_id = metrics_partial[-1]['id'] - url = "{}/v1/metric?sort=name:asc&marker={}".format(endpoint, last_metric_id) + if resource: + url = "{}/v1/resource/generic/{}".format(endpoint, resource) result = Common.perform_request( url, auth_token, req_type="get") - if len(json.loads(result.text)) > 0: - metrics_partial = json.loads(result.text) - for metric in metrics_partial: - metrics.append(metric) - - if metrics is not None: - # Format the list response - if metric_name is not None and resource is not None: - metric_list = self.response_list( - metrics, metric_name=metric_name, resource=resource) - log.info("Returning an %s resource list for %s metrics", - metric_name, resource) - elif metric_name is not None: - metric_list = self.response_list( - metrics, metric_name=metric_name) - log.info("Returning a list of %s metrics", metric_name) - elif resource is not None: - metric_list = self.response_list( - metrics, resource=resource) - log.info("Return a list of %s resource metrics", resource) + resource_data = json.loads(result.text) + metrics = resource_data['metrics'] + + if metric_name: + if metrics.get(METRIC_MAPPINGS[metric_name]): + metric_id = metrics[METRIC_MAPPINGS[metric_name]] + url = "{}/v1/metric/{}".format(endpoint, metric_id) + result = Common.perform_request( + url, auth_token, req_type="get") + metric_list = json.loads(result.text) + log.info("Returning an %s resource list for %s metrics", + metric_name, resource) + return metric_list + else: + log.info("Metric {} not found for {} resource".format(metric_name, resource)) + return None else: - metric_list = self.response_list(metrics) - log.info("Returning a complete list of metrics") - - return metric_list + metric_list = [] + for k, v in metrics.items(): + url = "{}/v1/metric/{}".format(endpoint, v) + result = Common.perform_request( + url, auth_token, req_type="get") + metric = json.loads(result.text) + metric_list.append(metric) + if metric_list: + log.info("Return a list of %s resource metrics", resource) + return metric_list + + else: + log.info("There are no metrics available") + return [] else: - log.info("There are no metrics available") - return [] + url = "{}/v1/metric?sort=name:asc".format(endpoint) + result = Common.perform_request( + url, auth_token, req_type="get") + metrics = [] + metrics_partial = json.loads(result.text) + for metric in metrics_partial: + metrics.append(metric) + + while len(json.loads(result.text)) > 0: + last_metric_id = metrics_partial[-1]['id'] + url = "{}/v1/metric?sort=name:asc&marker={}".format(endpoint, last_metric_id) + result = Common.perform_request( + url, auth_token, req_type="get") + if len(json.loads(result.text)) > 0: + metrics_partial = json.loads(result.text) + for metric in metrics_partial: + metrics.append(metric) + + if metrics is not None: + # Format the list response + if metric_name is not None: + metric_list = self.response_list( + metrics, metric_name=metric_name) + log.info("Returning a list of %s metrics", metric_name) + else: + metric_list = self.response_list(metrics) + log.info("Returning a complete list of metrics") + return metric_list + else: + log.info("There are no metrics available") + return [] except Exception as exc: log.warning("Failed to generate any metric list. %s", exc) return None diff --git a/osm_mon/test/OpenStack/unit/test_metric_calls.py b/osm_mon/test/OpenStack/unit/test_metric_calls.py index 8785534..5014133 100644 --- a/osm_mon/test/OpenStack/unit/test_metric_calls.py +++ b/osm_mon/test/OpenStack/unit/test_metric_calls.py @@ -179,9 +179,7 @@ class TestMetricCalls(unittest.TestCase): self.metrics.list_metrics(endpoint, auth_token, values) perf_req.assert_any_call( - "/v1/metric?sort=name:asc", auth_token, req_type="get") - resp_list.assert_called_with( - [{u'id': u'test_id'}], resource="resource_id") + "/v1/resource/generic/resource_id", auth_token, req_type="get") @mock.patch.object(metric_req.Metrics, "response_list") @mock.patch.object(Common, "perform_request") @@ -209,10 +207,7 @@ class TestMetricCalls(unittest.TestCase): self.metrics.list_metrics(endpoint, auth_token, values) perf_req.assert_any_call( - "/v1/metric?sort=name:asc", auth_token, req_type="get") - resp_list.assert_called_with( - [{u'id': u'test_id'}], resource="resource_id", - metric_name="packets_sent") + "/v1/resource/generic/resource_id", auth_token, req_type="get") @mock.patch.object(Common, "perform_request") def test_get_metric_id(self, perf_req): -- 2.25.1