]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
heat engine : Add show_watch_metric RPC action
authorSteven Hardy <shardy@redhat.com>
Wed, 22 Aug 2012 13:11:52 +0000 (14:11 +0100)
committerSteven Hardy <shardy@redhat.com>
Fri, 24 Aug 2012 10:29:36 +0000 (11:29 +0100)
Adds show_watch_metric, which allow alarm/watch
metric data to be retrieved from the engine

Change-Id: Ie69691dbf781016e6262bbbabbe57f9c3396a5d5
Signed-off-by: Steven Hardy <shardy@redhat.com>
heat/engine/api.py
heat/engine/manager.py
heat/engine/rpcapi.py

index 8fe522108599f319518a4cec2985684bf15d8844..95f346779ec3b8b9456b134b65856be916605ca5 100644 (file)
@@ -252,3 +252,35 @@ def format_watch(watch):
     }
 
     return result
+
+
+WATCH_DATA_KEYS = (
+    WATCH_DATA_ALARM, WATCH_DATA_METRIC, WATCH_DATA_TIME,
+    WATCH_DATA_NAMESPACE, WATCH_DATA
+    ) = (
+    'watch_name', 'metric_name', 'timestamp',
+    'namespace', 'data')
+
+
+def format_watch_data(wd):
+
+    # Demangle DB format data into something more easily used in the API
+    # We are expecting a dict with exactly two items, Namespace and
+    # a metric key
+    namespace = wd.data['Namespace']
+    metric = [(k, v) for k, v in wd.data.items() if k != 'Namespace']
+    if len(metric) == 1:
+        metric_name, metric_data = metric[0]
+    else:
+        logger.error("Unexpected number of keys in watch_data.data!")
+        return
+
+    result = {
+        WATCH_DATA_ALARM: wd.watch_rule.name,
+        WATCH_DATA_METRIC: metric_name,
+        WATCH_DATA_TIME: heat_utils.strtime(wd.created_at),
+        WATCH_DATA_NAMESPACE: namespace,
+        WATCH_DATA: metric_data
+    }
+
+    return result
index 1af5f98712ff9f55159b23ac44343e62f7e1ffcb..480fa2cf1cf8810a9e394fad82f02ccb6c9ed290 100644 (file)
@@ -502,3 +502,27 @@ class EngineManager(manager.Manager):
 
         result = [api.format_watch(w) for w in wrs]
         return result
+
+    def show_watch_metric(self, context, namespace=None, metric_name=None):
+        '''
+        The show_watch method returns the datapoints for a metric
+        arg1 -> RPC context.
+        arg2 -> Name of the namespace you want to see, or None to see all
+        arg3 -> Name of the metric you want to see, or None to see all
+        '''
+
+        # DB API and schema does not yet allow us to easily query by
+        # namespace/metric, but we will want this at some point
+        # for now, the API can query all metric data and filter locally
+        if namespace != None or metric_name != None:
+            logger.error("Filtering by namespace/metric not yet supported")
+            return
+
+        try:
+            wds = db_api.watch_data_get_all(context)
+        except Exception as ex:
+            logger.warn('show_metric (all) db error %s' % str(ex))
+            return
+
+        result = [api.format_watch_data(w) for w in wds]
+        return result
index c786f3234be3a3b6a8830179000fd1d9cb3ce930..691a31fb16121427dd8b51be1c444b789f37529b 100644 (file)
@@ -232,3 +232,18 @@ class EngineAPI(heat.openstack.common.rpc.proxy.RpcProxy):
         return self.call(ctxt, self.make_msg('show_watch',
                          watch_name=watch_name),
                          topic=_engine_topic(self.topic, ctxt, None))
+
+    def show_watch_metric(self, ctxt, namespace=None, metric_name=None):
+        """
+        The show_watch_metric method returns the datapoints associated
+        with a specified metric, or all metrics if no metric_name is passed
+
+        :param ctxt: RPC context.
+        :param namespace: Name of the namespace you want to see,
+                           or None to see all
+        :param metric_name: Name of the metric you want to see,
+                           or None to see all
+        """
+        return self.call(ctxt, self.make_msg('show_watch_metric',
+                         namespace=namespace, metric_name=metric_name),
+                         topic=_engine_topic(self.topic, ctxt, None))