Take last 30s values for live update (influx), added doc in Js Graphics
This commit is contained in:
20
influxdb.py
20
influxdb.py
@@ -98,16 +98,16 @@ class InfluxDataGetter:
|
||||
|
||||
return res
|
||||
|
||||
def poll_last_value(self, variables, time):
|
||||
def poll_last_values(self, variables, time):
|
||||
"""
|
||||
Polls the last value for the given variables that are not more recent that time[1].
|
||||
Polls the lastest values for the given variables that are in the range [time[1]-30s, 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).
|
||||
{(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:
|
||||
@@ -118,8 +118,9 @@ class InfluxDataGetter:
|
||||
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
|
||||
points = self._get_last_values(variable_name_for_query, is_target, time)
|
||||
if len(points) > 0 :
|
||||
res[variable] = points
|
||||
return res
|
||||
|
||||
# ----- PRIVATE METHODS
|
||||
@@ -366,9 +367,9 @@ class InfluxDataGetter:
|
||||
curve.insert(0, [time[0], value])
|
||||
return curve
|
||||
|
||||
def _get_last_value(self, variable, is_target, time):
|
||||
def _get_last_values(self, variable, is_target, time):
|
||||
"""
|
||||
Gets the last value for the given variable that are not more recent that time[1].
|
||||
Gets the lastest values for the given variable that are in [time[1]-30s, time[1]].
|
||||
|
||||
Parameters :
|
||||
variable (str) : the name (Influx) of the variable we want the last value of.
|
||||
@@ -376,16 +377,15 @@ class InfluxDataGetter:
|
||||
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)
|
||||
[[(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: 0, stop: {time[1] + 1})
|
||||
|> range(start: {time[1]-30}, 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")
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user