Add lastupdate()
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 23 Nov 2015 03:19:04 +0000 (03:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 23 Nov 2015 03:19:04 +0000 (03:19 +0000)
Allow to request the latest dataset of the database so
that sensory data can be shown at various other places, too.

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 fe22fea..7c84285 100644 (file)
@@ -98,6 +98,19 @@ class GraphGenerator(dbus.service.Object):
                """
                return self.collecty.graph_info(template_name, **kwargs)
 
+       @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="a{sv}")
+       def LastUpdate(self, template_name, kwargs):
+               """
+                       Returns a graph generated from the given template and object.
+               """
+               last_update = self.collecty.last_update(template_name, **kwargs)
+
+               # Serialise datetime as string
+               if last_update:
+                       last_update["timestamp"] = last_update["timestamp"].isoformat()
+
+               return last_update
+
        @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="as")
        def ListTemplates(self):
                """
index 7989d11..be898bb 100644 (file)
@@ -20,6 +20,7 @@
 ###############################################################################
 
 import argparse
+import datetime
 import dbus
 import os
 import platform
@@ -37,6 +38,23 @@ class CollectyClient(object):
 
                self.proxy = self.bus.get_object(BUS_DOMAIN, "/GraphGenerator")
 
+       def last_update(self, template_name, **kwargs):
+               last_update = self.proxy.LastUpdate(template_name, kwargs)
+
+               if last_update:
+                       last_update["timestamp"] = datetime.datetime.strptime(last_update["timestamp"], "%Y-%m-%dT%H:%M:%S")
+
+               return last_update
+
+       def last_update_cli(self, ns):
+               last_update = self.last_update(ns.template, object_id=ns.object)
+
+               print(_("Last update: %s") % last_update.get("timestamp"))
+
+               dataset = last_update.get("dataset")
+               for k, v in dataset.items():
+                       print("%16s = %s" % (k, v))
+
        def list_templates(self):
                templates = self.proxy.ListTemplates()
 
@@ -136,6 +154,15 @@ class CollectyClient(object):
                parser_generate_graph.add_argument("--width", type=int, default=0,
                        help=_("Width of the generated image"))
 
+               # last-update
+               parser_last_update = subparsers.add_parser("last-update",
+                       help=_("Fetch the last dataset in the database"))
+               parser_last_update.add_argument("--template",
+                       help=_("The graph template identifier"), required=True)
+               parser_last_update.add_argument("--object",
+                       help=_("Object identifier"), default="default")
+               parser_last_update.set_defaults(func=self.last_update_cli)
+
                # list-templates
                parser_list_templates = subparsers.add_parser("list-templates",
                        help=_("Lists all graph templates"))
index e8d402e..d4e9b4a 100644 (file)
@@ -203,6 +203,13 @@ class Collecty(object):
 
                return plugin.graph_info(template_name, *args, **kwargs)
 
+       def last_update(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.last_update(*args, **kwargs)
+
        def create_worker_threads(self, num=None):
                """
                        Creates a number of worker threads
index ca98ef7..04207c7 100644 (file)
@@ -264,6 +264,13 @@ class Plugin(object, metaclass=PluginRegistration):
 
                return template.graph_info()
 
+       def last_update(self, object_id="default"):
+               object = self.get_object(object_id)
+               if not object:
+                       raise RuntimeError("Could not find object %s" % object_id)
+
+               return object.last_update()
+
 
 class Object(object):
        # The schema of the RRD database.
@@ -359,6 +366,39 @@ class Object(object):
        def info(self):
                return rrdtool.info(self.file)
 
+       def last_update(self):
+               """
+                       Returns a dictionary with the timestamp and
+                       data set of the last database update.
+               """
+               return {
+                       "dataset"   : self.last_dataset,
+                       "timestamp" : self.last_updated,
+               }
+
+       def _last_update(self):
+               return rrdtool.lastupdate(self.file)
+
+       @property
+       def last_updated(self):
+               """
+                       Returns the timestamp when this database was last updated
+               """
+               lu = self._last_update()
+
+               if lu:
+                       return lu.get("date")
+
+       @property
+       def last_dataset(self):
+               """
+                       Returns the latest dataset in the database
+               """
+               lu = self._last_update()
+
+               if lu:
+                       return lu.get("ds")
+
        @property
        def stepsize(self):
                return self.plugin.interval