]> git.ipfire.org Git - collecty.git/commitdiff
Commit all collected data in the write cache to disk when generating a graph
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Dec 2015 23:27:25 +0000 (00:27 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Dec 2015 23:27:25 +0000 (00:27 +0100)
Therefore we will always get the latest data drawn
in the graph image even when writing data to disk
is happening delayed.

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

index d4e9b4a3e1b8d3424efcc5811e4d90ccd3fec198..695ca7f96f2e201f36a30c75ed23d458ae1fc7a9 100644 (file)
@@ -381,6 +381,33 @@ class WriteQueue(threading.Thread):
                        self.log.critical(_("Could not update RRD database %s: %s") \
                                % (filename, e))
 
+       def commit_file(self, filename):
+               """
+                       Commits all data that is in the write queue for the given
+                       RRD database.
+               """
+               results, others = [], []
+
+               # We will have to walk through the entire queue since we cannot
+               # ready any items selectively. Everything that belongs to our
+               # transaction is kept. Everything else will be put back into the
+               # queue.
+               while not self._queue.empty():
+                       result = self._queue.get()
+
+                       if result.file == filename:
+                               results.append(result)
+                       else:
+                               others.append(result)
+
+               # Put back all items that did not match
+               for result in others:
+                       self._queue.put(result)
+
+               # Write everything else to disk
+               if results:
+                       self._commit_file(filename, results)
+
 
 class QueueObject(object):
        def __init__(self, file, time, data):
index 04207c7594b0755e5ab74ad9cd4d3e526375099f..a4d2d72c4984382a9ed58524e918f75c866d9ce6 100644 (file)
@@ -454,6 +454,9 @@ class Object(object):
                # Make sure that the RRD database has been created
                self.create()
 
+               # Write everything to disk that is in the write queue
+               self.collecty.write_queue.commit_file(self.file)
+
 
 class GraphTemplate(object):
        # A unique name to identify this graph template.
@@ -578,6 +581,11 @@ class GraphTemplate(object):
                return files
 
        def generate_graph(self, interval=None, **kwargs):
+               # Make sure that all collected data is in the database
+               # to get a recent graph image
+               if self.object:
+                       self.object.commit()
+
                args = self._make_command_line(interval, **kwargs)
 
                self.log.info(_("Generating graph %s") % self)