Generic (for parameters) get curve

This commit is contained in:
l_samenv
2024-08-19 11:23:50 +02:00
parent 9d032be0d2
commit fff86095d1
3 changed files with 100 additions and 2 deletions

View File

@ -125,6 +125,30 @@ class InfluxDataGetter:
return res
def get_curves_in_timerange2(self, variables, time, interval = None):
"""
Gets the curves for the given variables within a timerange.
Parameters :
variables ([(str)]) : an array of variable names (Influx) to get the curves for
time ([int]) : the timerange we want the values in. It consists of two values which are Unix timestamps in seconds, first included, second excluded.
interval (int) : the interval (resolution) of the values to get (in nanoseconds)
Returns :
{(str):[[(int), (float)]]} : a dictionnary of curves. The key is the name of the influx variable, and the value is an array of pairs (also arrays), 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]
curve = self._get_curve2(variable_name_for_query, parameter, time, interval)
if len(curve) > 0:
res[variable] = curve
return res
def poll_last_values(self, variables, lastvalues, end_time):
"""
Polls the lastest values for the given variables since their last known point to end_time.
@ -508,6 +532,43 @@ class InfluxDataGetter:
return self._insert_last_known_value(variable, is_target, res, time)
def _get_curve2(self, variable, parameter, time, interval=None):
"""
Gets the points (curve) within a timerange for the given variable.
Parameters :
variable (str) : the name (Influx) of the variable we want the values of.
parameter (str) : the parameter of the variable to get the values from
time ([(int)]) : the timerange we want the values in. It consists of two values which are Unix timestamps in seconds, first included, second excluded.
interval (int) : the interval (resolution) of the values to get (in nanoseconds)
Returns :
[[(int), (float)]] : an array of pairs (also arrays), the first value being the Unix timestamp in second (x), the seconds being the value (y)
"""
res = []
query = f"""
from(bucket: "{self._bucket}")
|> range(start: {time[0]}, stop: {time[1] + 1})
|> filter(fn : (r) => r._measurement == "{variable}")
|> filter(fn : (r) => r._field == "{parameter+"_float"}")
{"|> aggregateWindow(every: duration(v:"+str(interval)+"), fn: last, createEmpty:false)" if interval else ""}
|> keep(columns: ["_time","_value"])
|> yield(name: "res")
"""
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 self._insert_last_known_value2(variable, parameter, res, 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.
@ -543,6 +604,42 @@ class InfluxDataGetter:
value = None
curve.insert(0, [time[0], value])
return curve
def _insert_last_known_value2(self, variable, parameter, curve, time):
"""
Adds the last known value as the first point in the curve if the last known value is outside the viewing window.
Parameters :
variable (str) : the name (Influx) of the variable we want the values of.
parameter (str) : the parameter of the variable to get the values from
curve ([[(int), (float)]]) : an array of pairs (arrays), the first value being the Unix timestamp in second (x), the seconds being the value (y)
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 of the parameter, updated with a potential new first point
"""
if len(curve) == 0 or curve[0][0] != time[0]:
query = f"""
from(bucket: "{self._bucket}")
|> range(start: 0, stop: {time[0]+1})
|> filter(fn : (r) => r._measurement == "{variable}")
|> filter(fn : (r) => r._field == "{parameter+"_float"}")
|> last()
|> keep(columns: ["_value"])
|> yield(name: "res")
"""
tables = self._db.query(query)
for table in tables:
for record in table.records:
value = record.get_value()
try:
value = PrettyFloat(value)
except:
value = None
curve.insert(0, [time[0], value])
return curve
def _get_last_values(self, variable, is_target, start_time, end_time):
"""

View File

@ -99,7 +99,7 @@ class InfluxGraph:
logging.info('LIVE %g %g %d %d', end, now, end >= now, self.livemode)
if interval : interval = self.milliseconds_to_nano(int(interval))
result = self.influx_data_getter.get_curves_in_timerange(queried_variables, queried_time_range, interval)
result = self.influx_data_getter.get_curves_in_timerange2(queried_variables, queried_time_range, interval)
self.complete_to_end(result, min(end, now))
self.end_query = end
# reduction not yet implemented

View File

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