From: Michael Tremer Date: Mon, 29 Jun 2015 20:44:18 +0000 (+0000) Subject: plugins: Automatically replace None by NaN X-Git-Tag: 004~39 X-Git-Url: http://git.ipfire.org/?p=collecty.git;a=commitdiff_plain;h=a9af411f0703eac939e0df5d5f75b46d35f531bc plugins: Automatically replace None by NaN rrdtool uses NaN to represent no value. Python uses None. This patch automatically translates from None to NaN. Signed-off-by: Michael Tremer --- diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py index bed461f..cf9c3b4 100644 --- a/src/collecty/plugins/base.py +++ b/src/collecty/plugins/base.py @@ -147,8 +147,7 @@ class Plugin(object, metaclass=PluginRegistration): try: result = o.collect() - if isinstance(result, tuple) or isinstance(result, list): - result = ":".join(("%s" % e for e in result)) + result = self._format_result(result) except: self.log.warning(_("Unhandled exception in %s.collect()") % o, exc_info=True) continue @@ -170,6 +169,25 @@ class Plugin(object, metaclass=PluginRegistration): if delay >= 60: self.log.warning(_("A worker thread was stalled for %.4fs") % delay) + @staticmethod + def _format_result(result): + if not isinstance(result, tuple) and not isinstance(result, list): + return result + + # Replace all Nones by NaN + s = [] + + for e in result: + if e is None: + e = "NaN" + + # Format as string + e = "%s" % e + + s.append(e) + + return ":".join(s) + def get_object(self, id): for object in self.objects: if not object.id == id: