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

@@ -85,11 +85,41 @@ class InfluxDataGetter:
"""
res = {}
for variable in variables:
if variable.endswith(".target"):
variable_name_for_query = variable[:-len(".target")]
res[variable] = self._get_curve(variable_name_for_query, True, time)
else:
res[variable] = self._get_curve(variable, False, time)
variable_name_for_query = variable
is_target = False
if variable_name_for_query.endswith(".target"):
variable_name_for_query = variable_name_for_query[:-len(".target")]
is_target = True
curve = self._get_curve(variable_name_for_query, is_target, time)
if len(curve) > 0:
res[variable] = curve
return res
def poll_last_value(self, variables, time):
"""
Polls the last value for the given variables that are not more recent that time[1].
Parameters :
variables ([(str)]) : an array of variable names (Influx) to get the last known values for
time ([int]) : the current asked timerange in the calling polling function (only second value used). It consists of two values which are Unix timestamps in seconds, first included, second excluded.
Returns :
{(str):[[(int), (float)]]} : a dictionnary of points. The key is the name of the influx variable, and the value is an array of one pair (also array), the first value being the Unix timestamp in second (x), the seconds being the value (y).
"""
res = {}
for variable in variables:
variable_name_for_query = variable
is_target = False
if variable_name_for_query.endswith(".target"):
variable_name_for_query = variable_name_for_query[:-len(".target")]
is_target = True
point = self._get_last_value(variable_name_for_query, is_target, time)
res[variable] = point
return res
# ----- PRIVATE METHODS
@@ -219,7 +249,8 @@ class InfluxDataGetter:
Returns :
[{"tag":(str), "unit":(str), "curves":[{"name":(str), }]]
[{"tag":(str), "unit":(str), "curves":[{"name":(str), "label":(str), "color":(str)}]] : a list of dictionnaries, each one representing
a block of curves with their name, their label and their color to display, grouped by their tag (which can be the unit augmented with an index) and their unit.
"""
groups = {}
@@ -299,10 +330,10 @@ class InfluxDataGetter:
except:
value = None
res.append([t, value])
return self._insert_last_know_value(variable, is_target, res, time)
return self._insert_last_known_value(variable, is_target, res, time)
def _insert_last_know_value(self, variable, is_target, curve, time):
def _insert_last_known_value(self, variable, is_target, curve, time):
"""
Adds the last known value as the first point in the curve if the last known value is outside the viewing window.
@@ -313,7 +344,7 @@ class InfluxDataGetter:
time ([(int)]) : the timerange we want the values in. It consists of two values which are Unix timestamps in seconds, first included, second excluded.
Returns :
[[(int), (float)]] : the curve parameter, updated with a potential new first point
[[(int), (float)]] : the curve of the parameter, updated with a potential new first point
"""
if len(curve) == 0 or curve[0][0] != time[0]:
@@ -333,4 +364,41 @@ class InfluxDataGetter:
except:
value = None
curve.insert(0, [time[0], value])
return curve
return curve
def _get_last_value(self, variable, is_target, time):
"""
Gets the last value for the given variable that are not more recent that time[1].
Parameters :
variable (str) : the name (Influx) of the variable we want the last value of.
is_target (bool) : tells if the given variable is a target, or not (if variable is "nicos/se_t_chip.target", then is_target has to be set to True)
time ([int]) : the current asked timerange in the calling polling function (only second value used). It consists of two values which are Unix timestamps in seconds, first included, second excluded.
Returns :
[[(int), (float)]] : a point encapsuled in an array. 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: 0, stop: {time[1] + 1})
|> filter(fn : (r) => r._measurement == "{variable}")
|> filter(fn : (r) => r._field == "{"target_float" if is_target else "value_float"}")
|> last()
|> 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 = int(datetime.timestamp(record.get_time()))
value = record.get_value()
try:
value = PrettyFloat(value)
except:
value = None
res.append([t, value])
return res