Optimization of "check_for_metric" function 87/6187/6 v4.0.0 v4.0.1
authorgcalvino <guillermo.calvinosanchez@altran.com>
Tue, 22 May 2018 08:00:16 +0000 (10:00 +0200)
committergcalvino <guillermo.calvinosanchez@altran.com>
Tue, 22 May 2018 14:57:58 +0000 (16:57 +0200)
Signed-off-by: gcalvino <guillermo.calvinosanchez@altran.com>
Change-Id: I1beb698c564b21f3340d9e67947fe1031f57dfe7

osm_mon/plugins/OpenStack/Aodh/alarming.py
osm_mon/test/OpenStack/unit/test_alarming.py

index 13b6541..e52b12f 100644 (file)
@@ -432,31 +432,17 @@ class Alarming(object):
     def check_for_metric(self, auth_token, metric_endpoint, m_name, r_id):
         """Check for the alarm metric."""
         try:
-            url = "{}/v1/metric?sort=name:asc".format(metric_endpoint)
+            url = "{}/v1/resource/generic/{}".format(metric_endpoint, r_id)
             result = Common.perform_request(
                 url, auth_token, req_type="get")
-            metric_list = []
-            metrics_partial = json.loads(result.text)
-            for metric in metrics_partial:
-                metric_list.append(metric)
-
-            while len(json.loads(result.text)) > 0:
-                last_metric_id = metrics_partial[-1]['id']
-                url = "{}/v1/metric?sort=name:asc&marker={}".format(metric_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:
-                        metric_list.append(metric)
-            metric_id = None
-            for metric in metric_list:
-                name = metric['name']
-                resource = metric['resource_id']
-                if name == METRIC_MAPPINGS[m_name] and resource == r_id:
-                    metric_id = metric['id']
-            log.info("The required metric exists, an alarm will be created.")
+            resource = json.loads(result.text)
+            metric_list = resource['metrics']
+            if metric_list.get(METRIC_MAPPINGS[m_name]):
+                metric_id = metric_list[METRIC_MAPPINGS[m_name]]
+            else:
+                metric_id = None
+                log.info("Desired Gnocchi metric not found")
             return metric_id
         except Exception as exc:
             log.info("Desired Gnocchi metric not found:%s", exc)
-        return None
+        return None
\ No newline at end of file
index c45c052..a46e1f3 100644 (file)
@@ -287,4 +287,4 @@ class TestAlarming(unittest.TestCase):
         self.alarming.check_for_metric(auth_token, metric_endpoint, "metric_name", "r_id")
 
         perf_req.assert_called_with(
-            "metric_endpoint/v1/metric?sort=name:asc", auth_token, req_type="get")
+            "metric_endpoint/v1/resource/generic/r_id", auth_token, req_type="get")