Add graph info functionality
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 26 Oct 2015 23:57:58 +0000 (00:57 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 26 Oct 2015 23:57:58 +0000 (00:57 +0100)
The GraphInfo interface will return some basic information about
a graph (like title, etc.) and can be extended in the future.

This patch also changed the return value of the GenerateGraph
interface which is now a dictionary which is more extensible
and comes with some metrics about the graph now.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/collecty/bus.py
src/collecty/client.py
src/collecty/daemon.py
src/collecty/plugins/base.py

index 463f422..fe22fea 100644 (file)
@@ -78,14 +78,25 @@ class GraphGenerator(dbus.service.Object):
 
                self.collecty = collecty
 
-       @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="ay")
+       @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="a{sv}")
        def GenerateGraph(self, template_name, kwargs):
                """
                        Returns a graph generated from the given template and object.
                """
                graph = self.collecty.generate_graph(template_name, **kwargs)
 
-               return dbus.ByteArray(graph or [])
+               # Convert the graph back to normal Python format
+               if graph:
+                       graph["image"] = dbus.ByteArray(graph["image"] or [])
+
+               return graph
+
+       @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="a{sv}")
+       def GraphInfo(self, template_name, kwargs):
+               """
+                       Returns a dictionary with information about the graph.
+               """
+               return self.collecty.graph_info(template_name, **kwargs)
 
        @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="as")
        def ListTemplates(self):
index 7b3c3ec..7989d11 100644 (file)
@@ -48,13 +48,21 @@ class CollectyClient(object):
                for t in sorted(templates):
                        print(t)
 
+       def graph_info(self, template_name, **kwargs):
+               graph_info = self.proxy.GraphInfo(template_name, kwargs,
+                       signature="sa{sv}")
+
+               return dict(graph_info)
+
        def generate_graph(self, template_name, **kwargs):
-               byte_array = self.proxy.GenerateGraph(template_name, kwargs,
+               graph = self.proxy.GenerateGraph(template_name, kwargs,
                        signature="sa{sv}")
 
                # Convert the byte array into a byte string again
-               if byte_array:
-                       return bytes(byte_array)
+               if graph:
+                       graph["image"] = bytes(graph["image"])
+
+               return graph
 
        def generate_graph_cli(self, ns):
                kwargs = {
@@ -79,9 +87,17 @@ class CollectyClient(object):
                # Generate the graph image
                graph = self.generate_graph(ns.template, **kwargs)
 
+               # Add some useful information
+               info = self.graph_info(ns.template, **kwargs)
+               if info:
+                       graph.update(info)
+
                # Write file to disk
                with open(ns.filename, "wb") as f:
-                       f.write(graph)
+                       f.write(graph["image"])
+
+               print(_("Title      : %(title)s (%(template)s - %(object_id)s)") % graph)
+               print(_("Image size : %(image_width)sx%(image_height)spx") % graph)
 
        def version_cli(self, args):
                daemon_version = self.proxy.Version()
index 27c587f..e8d402e 100644 (file)
@@ -196,6 +196,13 @@ class Collecty(object):
 
                return plugin.generate_graph(template_name, *args, **kwargs)
 
+       def graph_info(self, template_name, *args, **kwargs):
+               plugin = self.get_plugin_from_template(template_name)
+               if not plugin:
+                       raise RuntimeError("Could not find template %s" % template_name)
+
+               return plugin.graph_info(template_name, *args, **kwargs)
+
        def create_worker_threads(self, num=None):
                """
                        Creates a number of worker threads
index c9e815c..ca98ef7 100644 (file)
@@ -255,6 +255,15 @@ class Plugin(object, metaclass=PluginRegistration):
 
                return graph
 
+       def graph_info(self, template_name, object_id="default",
+                       timezone=None, locale=None, **kwargs):
+               template = self.get_template(template_name, object_id=object_id,
+                       timezone=timezone, locale=locale)
+               if not template:
+                       raise RuntimeError("Could not find template %s" % template_name)
+
+               return template.graph_info()
+
 
 class Object(object):
        # The schema of the RRD database.
@@ -550,4 +559,19 @@ class GraphTemplate(object):
                with Environment(self.timezone, self.locale.lang):
                        graph = rrdtool.graphv("-", *args)
 
-               return graph.get("image")
+               return {
+                       "image"        : graph.get("image"),
+                       "image_height" : graph.get("image_height"),
+                       "image_width"  : graph.get("image_width"),
+               }
+
+       def graph_info(self):
+               """
+                       Returns a dictionary with useful information
+                       about this graph.
+               """
+               return {
+                       "title"        : self.graph_title,
+                       "object_id"    : self.object_id or "",
+                       "template"     : self.name,
+               }