Generic live polling for parameters

This commit is contained in:
l_samenv
2024-08-19 13:31:41 +02:00
parent fff86095d1
commit fa88f374fd
3 changed files with 66 additions and 2 deletions

View File

@ -175,6 +175,30 @@ class InfluxDataGetter:
res[variable] = points
return res
def poll_last_values2(self, variables, lastvalues, end_time):
"""
Polls the lastest values for the given variables since their last known point to end_time.
Parameters :
variables ([(str)]) : an array of variable names (Influx) to get the last known values for
end_time (int) : the Unix timestamp in seconds of the last point in time to include the values in
Returns :
{(str):[[(int), (float)]]} : a dictionnary of points. The key is the name of the influx variable, and the value is an array of pairs (also array), the first value being the Unix timestamp in second (x), the seconds being the value (y).
"""
res = {}
for variable in variables:
var_param = variable.split(".")
variable_name_for_query = var_param[0]
parameter = "value" if len(var_param) == 1 else var_param[1]
start_time = int(lastvalues[variable_name_for_query][0]) if variable_name_for_query in lastvalues.keys() else None
start_time = start_time if start_time != None and start_time <= end_time else None # to prevent self.lastvalues being changed by w_graph between entrance of graphpoll and poll_last_values
points = self._get_last_values2(variable_name_for_query,parameter,start_time, end_time)
if len(points) > 0 :
res[variable] = points
return res
# ----- PRIVATE METHODS
def _get_all_setup_info_as_dict(self, times, all=False):
@ -678,3 +702,42 @@ class InfluxDataGetter:
value = None
res.append([t, value])
return res
def _get_last_values2(self, variable, parameter, start_time, end_time):
"""
Gets the lastest values for the given variable that are in [start_time, end_time].
Parameters :
variable (str) : the name (Influx) of the variable we want the last value of.
parameter (str) : the parameter of the variable to get the values from
start_time (int|None) : the start of time range (Unix timestamp in seconds) to include the values in
end_time (int) : the end of time range (Unix timestamp in seconds) to include the values in
Returns :
[[(int), (float)]] : an array of points (also arrays). The first value is the Unix timestamp in second (x), the seconds is the value (y)
"""
res = []
query = f"""
from(bucket: "{self._bucket}")
|> range(start: {start_time if start_time != None else 0}, stop: {end_time+1})
|> filter(fn : (r) => r._measurement == "{variable}")
|> filter(fn : (r) => r._field == "{parameter+ "_float"}")
{"|> last()" if start_time == None else ""}
|> keep(columns: ["_time","_value"])
|> yield(name: "res")
"""
# this loop might be simplified, but it has to be kept to catch the case when there is unavailable data
tables = self._db.query(query)
for table in tables:
for record in table.records:
t = round(datetime.timestamp(record.get_time()), 3)
value = record.get_value()
try:
value = PrettyFloat(value)
except:
value = None
res.append([t, value])
return res

View File

@ -177,7 +177,7 @@ class InfluxGraph:
return None
now, = self.get_abs_time([0])
result = self.influx_data_getter.poll_last_values(self.variables, self.lastvalues, now)
result = self.influx_data_getter.poll_last_values2(self.variables, self.lastvalues, now)
for variable in self.lastvalues.keys():
if variable in result.keys():

View File

@ -1,2 +1,3 @@
[chart]
T_stat.raw=unit:K
T_stat.raw=
T_nvd.raw=