Added live data functionnality + some docs in Python and JS code

This commit is contained in:
l_samenv
2024-07-24 11:41:24 +02:00
parent aa29d89a18
commit 5a82601e8e
6 changed files with 294 additions and 63 deletions

View File

@ -32,6 +32,7 @@ class InfluxGraph:
self.livemode = self.HISTORICAL
self.time = [0, 0]
self.lastvalues = {}
self.variables = []
def get_abs_time(self, times):
"""
@ -60,7 +61,7 @@ class InfluxGraph:
# if self.livemode == self.LIVE:
for c in result.values():
while c:
lastt, lastx = c[-1]
lastt, _ = c[-1]
if lastt <= self.time[1]:
break
c.pop()
@ -144,6 +145,10 @@ class InfluxGraph:
end_time = int(self.get_abs_time(time)[-1])
blocks = self.influx_data_getter.get_available_variables_at_time(end_time)
# updates the self.variables attribute to keep track of the available variables
self.variables = [variable["name"] for block in blocks for variable in block["curves"]]
assign_colors_to_curves(blocks)
result = dict(type='var_list')
result['blocks'] = blocks
@ -151,19 +156,48 @@ class InfluxGraph:
def w_updategraph(self):
"""
TODO : method needed for live data update. See example in seaweb.py : SeaGraph
OLD : update live values - seems not to work
Sets the current visualisation mode to LIVE.
Sets the current visualisation mode to LIVE if not in HISTORICAL mode.
Called when the route /updategraph is reached.
Returns :
{"type":"accept-graph", "live": bool} : a dict with its "accept-graph" type and a "live" value telling if the server could change its visualization mode to live
"""
return dict(type='accept-graph', live=False)
logging.info("UPD GRAPH %d", self.livemode)
if self.livemode == self.HISTORICAL:
return dict(type='accept-graph', live=False)
else:
self.livemode = self.LIVE
return dict(type='accept-graph', live=True)
def graphpoll(self):
"""
TODO : method needed for live data update. See example in seaweb.py : SeaGraph
Polls the last known values for all the available variables, and returns only those whose polled value is more recent than the nost 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.
Returns :
{"type":"graph-update", "reduced":(bool), "time":(int), "graph":{(str):[[(int),(float)]]}} | None : a dictionnary with its "graph-update" type
(so it can be processed by the client), a "reduced" value indicating if the data is reduced or not (meaning the data is sampled to be lighter
for data viewing), and a "graph" dictionnary with the variable names as key, and an array of points, which are an array containing the timestamp
as their first value, and the y-value in float as their second one
"""
if self.livemode == self.LIVE:
self.time[1], = self.get_abs_time([0])
else:
self.time[1] = self.time[0] # Do not update (the current requested value is the last)
if self.time[1] > self.time[0]:
result = self.influx_data_getter.poll_last_value(self.variables, self.time)
for variable in self.lastvalues.keys():
if 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(self.time[1] / 60) != int(self.time[0] / 60):
# Update unchanged values every plain minute
for var, (_, lastx) in self.lastvalues.items():
if var not in result:
result[var] = [(self.time[1], lastx)]
self.time[0] = self.time[1]
if len(result) > 0:
return dict(type='graph-update', reduced=False, time=self.time[1], graph=result)
return None