From: Michael Tremer Date: Mon, 26 Oct 2015 23:57:58 +0000 (+0100) Subject: Add graph info functionality X-Git-Tag: 004~17 X-Git-Url: http://git.ipfire.org/?p=collecty.git;a=commitdiff_plain;h=a386481247af3ce6f554162a4c3632afe2513b89;ds=sidebyside Add graph info functionality 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 --- diff --git a/src/collecty/bus.py b/src/collecty/bus.py index 463f422..fe22fea 100644 --- a/src/collecty/bus.py +++ b/src/collecty/bus.py @@ -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): diff --git a/src/collecty/client.py b/src/collecty/client.py index 7b3c3ec..7989d11 100644 --- a/src/collecty/client.py +++ b/src/collecty/client.py @@ -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() diff --git a/src/collecty/daemon.py b/src/collecty/daemon.py index 27c587f..e8d402e 100644 --- a/src/collecty/daemon.py +++ b/src/collecty/daemon.py @@ -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 diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py index c9e815c..ca98ef7 100644 --- a/src/collecty/plugins/base.py +++ b/src/collecty/plugins/base.py @@ -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, + }