From 2660442c0a1b0e0d1308c6ecbc6d2cfe17ea588e Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Tue, 25 Feb 2025 08:20:51 +0100 Subject: [PATCH] fix bug and add guess for instrument --- influx.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/influx.py b/influx.py index 76a49d7..56bd3c6 100644 --- a/influx.py +++ b/influx.py @@ -403,7 +403,7 @@ class InfluxDBWrapper: except StopIteration: return converters = key_dict = table_properties = None # make IDE happy - for i in range(5): + while 1: header = {} if row[0]: # this is a header header[row[0]] = row @@ -647,17 +647,23 @@ class InfluxDBWrapper: result[stream] = entry.tags.get('instrument', '0') return result - def set_instrument(self, stream, value, ts=None, **tags): + def set_instrument(self, stream, value, ts=None, guess=True, **tags): """set stream and instrument on or off :param stream: the uri of the stream :param value: instrument, "0" when unknown or None when switching to off :param ts: the time or None when now + :param guess: when instrument is undefined, take from previous """ - prev, row = self.get_instrument(stream, ts, **tags) - if row is not None: - if prev in (None, '0') or ts < row[0]: + prev, prevts = self.get_instrument(stream, ts, **tags) + if prevts is not None: + if prev in (None, '0'): ts = prevts + 0.001 + else: + if value == '0' and guess: + value = prev + if ts < prevts: + ts = prevts + 0.001 tags['stream'] = stream if value: tags['instrument'] = value