fix mechanism to strip overlapping data for live update

fix bug when when there was no last value yet
This commit is contained in:
l_samenv
2024-09-25 07:46:28 +02:00
parent 638f77a047
commit 4e27d66d36

View File

@ -190,7 +190,7 @@ class InfluxGraph:
""" """
Polls the last known values for all the available variables, and returns only those whose polled values are more recent than the most recent displayed one. Polls the last known values for all the available variables, and returns only those whose polled values are more recent than the most recent displayed one.
Every plain minute, all the variables are returned with a point having their last known value at the current timestamp to synchronize all the curves on the GUI. Every plain minute, all the variables are returned with a point having their last known value at the current timestamp to synchronize all the curves on the GUI.
Returns : Returns :
{"type":"graph-update", "time":(int), "graph":{(str):[[(int),(float)]]}} | None : a dictionnary with its "graph-update" type {"type":"graph-update", "time":(int), "graph":{(str):[[(int),(float)]]}} | None : a dictionnary with its "graph-update" type
(so it can be processed by the client), and a "graph" dictionnary with the variable names as key, and an array of points, which are an array containing the timestamp (so it can be processed by the client), and a "graph" dictionnary with the variable names as key, and an array of points, which are an array containing the timestamp
@ -201,21 +201,15 @@ class InfluxGraph:
now, = self.get_abs_time([0]) now, = self.get_abs_time([0])
result = self.influx_data_getter.poll_last_values(list(self.variables.keys()), self.lastvalues, now) result = self.influx_data_getter.poll_last_values(list(self.variables.keys()), self.lastvalues, now)
for variable in self.lastvalues.keys(): for variable, values in list(result.items()):
if variable in result.keys(): tlast = self.lastvalues.get(variable, (0,))[0]
# removes points older than the last known point (queries are in seconds and might return points already displayed)
# removes points older than the last known point (queries are in seconds and might return points already displayed) while values and values[0][0] <= tlast:
while len(result[variable]) > 0: values.pop(0)
if result[variable][0][0] <= self.lastvalues[variable][0]: if values and values[-1][0] > tlast:
result[variable].pop(0) self.lastvalues[variable] = values[-1]
else: else:
break del result[variable]
if len(result[variable]) > 0 and result[variable][-1][0] > self.lastvalues[variable][0]:
self.lastvalues[variable] = (result[variable][-1][0], result[variable][-1][1])
else:
del result[variable]
if int(now / 60) != int(self.end_query / 60): if int(now / 60) != int(self.end_query / 60):
# Update unchanged values every plain minute # Update unchanged values every plain minute
for var, (_, lastx) in self.lastvalues.items(): for var, (_, lastx) in self.lastvalues.items():
@ -224,4 +218,4 @@ class InfluxGraph:
self.end_query = now self.end_query = now
if len(result) > 0: if len(result) > 0:
return dict(type='graph-update', time=now, graph=result) return dict(type='graph-update', time=now, graph=result)
return None return None