1 id + locks + live : more frequent, last known points, already sent points + ms points + fixed jump time

This commit is contained in:
l_samenv
2024-08-16 09:55:30 +02:00
parent f4cf8fd9a5
commit 9e08f47916
5 changed files with 83 additions and 73 deletions

View File

@ -30,19 +30,19 @@ class InfluxGraph:
self.db = InfluxDB()
self.influx_data_getter = InfluxDataGetter(self.db, json.load(open("./graphs/lab4.json", "r"))["influx"])
self.livemode = self.HISTORICAL
self.time = [0, 0]
self.end_query = 0
self.lastvalues = {}
self.variables = []
def seconds_to_nano(self, seconds):
def milliseconds_to_nano(self, milliseconds):
"""
Converts seconds to nanoseconds
Converts milliseconds to nanoseconds
Parameters:
seconds (int)
milliseconds (int)
Returns :
int
"""
return seconds*1000000000
return milliseconds*1000000
def get_abs_time(self, times):
"""
@ -59,23 +59,6 @@ class InfluxGraph:
oneyear = 365 * 24 * 3600
return [t + now if t < oneyear else t for t in times]
def strip_future(self, result):
"""
OLD : strip future points (happens only on dummy test_day)
Removes points more recent that the last requested point in time
Parameters :
result ({(str):[[(int),(float)]]}) : a 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:
for c in result.values():
while c:
lastt, _ = c[-1]
if lastt <= self.time[1]:
break
c.pop()
def complete_to_end(self, result, endtime):
"""
Completes the data until the last requested point in time by adding the last known y-value at the end point.
@ -90,7 +73,7 @@ class InfluxGraph:
lastt, lastx = c[-1]
if lastt < endtime:
c.append((endtime, lastx))
self.lastvalues[var] = (endtime, lastx)
self.lastvalues[var] = (lastt, lastx)
def w_graph(self, variables, time="-1800,0", interval=None):
"""
@ -110,17 +93,15 @@ class InfluxGraph:
time = [float(t) for t in time.split(',')]
start, end, now = self.get_abs_time(time + [0])
start, end, now = int(start), int(end), int(now)
self.time = [start, end]
self.variables = variables.split(',')
self.livemode = self.ACTUAL if end >= now else self.HISTORICAL
queried_time_range = [start, end]
queried_variables = variables.split(',')
self.livemode = self.ACTUAL if end+10 >= now else self.HISTORICAL
logging.info('LIVE %g %g %d %d', end, now, end >= now, self.livemode)
if interval : interval = self.milliseconds_to_nano(int(interval))
if interval : interval = self.seconds_to_nano(int(interval))
result = self.influx_data_getter.get_curves_in_timerange(self.variables, self.time, interval)
self.strip_future(result)
result = self.influx_data_getter.get_curves_in_timerange(queried_variables, queried_time_range, interval)
self.complete_to_end(result, min(end, now))
self.time[0] = self.time[1]
self.end_query = end
# reduction not yet implemented
return dict(type='graph-draw', reduced=False, graph=result)
@ -192,25 +173,32 @@ class InfluxGraph:
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_values(self.variables, self.time)
for variable in self.lastvalues.keys():
if variable in result.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 self.livemode != self.LIVE:
return None
now, = self.get_abs_time([0])
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)
result = self.influx_data_getter.poll_last_values(self.variables, self.lastvalues, now)
for variable in self.lastvalues.keys():
if variable in result.keys():
# removes points older than the last known point (queries are in seconds and might return points already displayed)
while len(result[variable]) > 0:
if result[variable][0][0] <= self.lastvalues[variable][0]:
result[variable].pop(0)
else:
break
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):
# Update unchanged values every plain minute
for var, (_, lastx) in self.lastvalues.items():
if var not in result:
result[var] = [(now, lastx)]
self.end_query = now
if len(result) > 0:
return dict(type='graph-update', reduced=False, time=now, graph=result)
return None