Arbitrary number of config files + tag is unit or cat if present + unit keyword + empty string in config file means inherit + unit 1 is not given or empty

This commit is contained in:
l_samenv
2024-09-03 11:02:50 +02:00
parent 9eb0d4d97d
commit a73fe1653e
7 changed files with 58 additions and 35 deletions

View File

@ -17,9 +17,11 @@ class ChartConfig:
pieces = argument.split(":")
if len(pieces) == 2:
keyword_mode = True
if pieces[1] != "":
config[pieces[0]] = pieces[1]
else:
if not keyword_mode: #everything is going well
if pieces[0] != "":
config[positionnal[i]] = pieces[0]
else: #we cannot have a positionnal argument after a keyword argument
return None

2
config/generic.ini Normal file
View File

@ -0,0 +1,2 @@
[chart]
T_stat=K_2

4
config/lab4.ini Normal file
View File

@ -0,0 +1,4 @@
[chart]
T_stat=K_3
T_stat.raw=unit
T_nvd.raw=unit

View File

@ -58,7 +58,7 @@ class InfluxDataGetter:
# ----- PUBLIC METHODS
def get_available_variables_at_time(self, times, chart_config = None):
def get_available_variables_at_time(self, times, chart_configs = None):
"""
Gets the available variables (those that we can have a value for since the device has been installed on the instrument) at the given point in time (times[1]).
Here, a variable means : SECOP module name + parameter. By default, this method returns the parameters "value" and "target", unless the config file used in chart_config indicates other directives.
@ -75,7 +75,10 @@ class InfluxDataGetter:
all_setup_info = self._get_all_setup_info_as_dict(times)
available_variables = self._extract_variables(all_setup_info)
if not chart_configs == None:
for chart_config in chart_configs:
available_variables = self._filter_params_with_config(available_variables, chart_config)
available_variables = self._remove_variables_params_not_displayed(available_variables)
available_variables = self._remove_variables_params_wihout_param_float_and_split(available_variables, times)
res = self._group_variables_by_cat_unit(available_variables)
@ -344,18 +347,19 @@ class InfluxDataGetter:
if content[0] != "nicos.devices.secop.devices.SecopDevice":
name = self._transform_secop_module_name_to_influx(content[1]["secop_module"])
if name not in added_names:
value_unit = "1" if (not "unit" in content[1].keys() or content[1]["unit"] == "") else content[1]["unit"]
variable = {
"name":name,
"label":content[1]["secop_module"],
"params":{"value":{"cat":"", "color":"", "unit":content[1]["unit"]}} # main value
"params":{"value":{"cat":"unit", "color":"", "unit":value_unit}} # main value, shown by default
}
for param_name, param_content in content[1]["params_cfg"].items():
param_unit = "1" if (not "unit" in param_content.keys() or param_content["unit"] == "") else param_content["unit"]
variable["params"][param_name] = {
"cat":"",
"cat":"unit" if param_name == "target" else "None", # target is also shown by default, not the other parameters
"color":"",
"unit":param_content["unit"] if "unit" in param_content.keys() else "" #unit might not be there
"unit":param_unit
}
available_varirables.append(variable)
added_names.append(name)
@ -374,15 +378,15 @@ class InfluxDataGetter:
"""
return self._influx_instrument_config["measurement_prefix"] + secop_module_name.lower()
def _filter_params_with_config(self, available_variables, chart_config = None):
def _filter_params_with_config(self, available_variables, chart_config):
"""
Removes and updates (cat, color, unit) the parameters according to the chart_config object if given.
If not given, only value and target parameters are kept.
Updates (cat, color, unit) the parameters according to the chart_config object.
Parameters:
available_variables ([{"name":(str), "label":(str), "params":{(str):{"cat":(str), "color":(str), "unit":(str)}}}]) : an array of dictionnaries, each containing the Influx name of the corresponding variable out of the setup_info dict,
the label to display in the Web GUI, and a dictionnary of parameters (including value), which consist of dictionnares with the category (empty for the moment), the color (same) and the unit (if available), indexed by the name of the parameter.
chart_config (ChartConfig | None) : the object holding a configuration file for the chart.
the label to display in the Web GUI, and a dictionnary of parameters (including value), which consist of dictionnaries with the category ("unit" for value and target or "None"), the color (empty for the moment) and the unit,
indexed by the name of the parameter.
chart_config (ChartConfig) : the object holding a configuration file for the chart.
Returns :
[{"name":(str), "label":(str), "params":{(str):{"cat":(str), "color":(str), "unit":(str)}}}] : the available_variables parameter, updated
@ -392,16 +396,30 @@ class InfluxDataGetter:
params = list(variable["params"].keys())
for param_key in params:
key = variable["label"] if param_key == "value" else variable["label"]+"."+param_key
param_config = chart_config.get_variable_parameter_config(key) if chart_config else None
if param_config == None : # no entries were found or there is no chart_config
if param_key != "target" and param_key != "value": # and the current param is not value or target which are displayed by default
del variable["params"][param_key]
else:
if "cat" in param_config.keys() and param_config["cat"] == "None": # cat might not have been indicated
del variable["params"][param_key]
else:
param_config = chart_config.get_variable_parameter_config(key)
if param_config != None :
for key, value in param_config.items():
variable["params"][param_key][key] = value
return available_variables
def _remove_variables_params_not_displayed(self, available_variables):
"""
Removes the parameters if their category is None.
Parameters:
available_variables ([{"name":(str), "label":(str), "params":{(str):{"cat":(str), "color":(str), "unit":(str)}}}]) : an array of dictionnaries, each containing the Influx name of the corresponding variable out of the setup_info dict,
the label to display in the Web GUI, and a dictionnary of parameters (including value), which consist of dictionnaries with the category, the color and the unit, indexed by the name of the parameter.
Returns :
[{"name":(str), "label":(str), "params":{(str):{"cat":(str), "color":(str), "unit":(str)}}}] : the available_variables parameter, updated
"""
for variable in available_variables:
params = list(variable["params"].keys())
for param_key in params:
if variable["params"][param_key]["cat"] == "None":
del variable["params"][param_key]
return available_variables
def _remove_variables_params_wihout_param_float_and_split(self, available_variables, times):
@ -448,13 +466,14 @@ class InfluxDataGetter:
Returns :
[{"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 category and unit (in tag)
a block of curves with their name, their label and their color to display, grouped by their tag, which is the unit or the category if given and not "unit".
"""
groups = {}
for available_variable in available_variables:
key = available_variable["cat"]+"_"+available_variable["unit"] if available_variable["cat"] != "" else available_variable["unit"]
key = available_variable["unit"]
if available_variable["cat"] != "unit":
key = available_variable["cat"]
if key not in groups.keys():
groups[key] = {"tag":key, "unit":available_variable["unit"], "curves":[]}
groups[key]["curves"].append({

View File

@ -28,9 +28,9 @@ class InfluxGraph:
ACTUAL = 1
LIVE = 2
def __init__(self, influx_data_getter):
def __init__(self, influx_data_getter, instrument):
self.influx_data_getter = influx_data_getter
self.chart_config = ChartConfig("./variables_config.ini")
self.chart_configs = [ChartConfig("./config/generic.ini"), ChartConfig(f"./config/{instrument}.ini")]
self.livemode = self.HISTORICAL
self.end_query = 0
self.lastvalues = {}
@ -131,7 +131,7 @@ class InfluxGraph:
start_time = int(self.get_abs_time(time)[0])
end_time = int(self.get_abs_time(time)[-1])
blocks = self.influx_data_getter.get_available_variables_at_time([start_time, end_time], self.chart_config)
blocks = self.influx_data_getter.get_available_variables_at_time([start_time, end_time], self.chart_configs)
device_name = self.influx_data_getter.get_device_name(end_time)
# updates the self.variables attribute to keep track of the available variables
self.variables = {variable["name"]:variable["label"] for block in blocks for variable in block["curves"]}

View File

@ -765,12 +765,12 @@ class SeaClient(SeaParams, SeaGraph):
class SeaInfluxClient(SeaParams, InfluxGraph):
def __init__(self):
SeaParams.__init__(self)
InfluxGraph.__init__(self, instrument.influx_data_getter)
InfluxGraph.__init__(self, instrument.influx_data_getter, instrument.title)
class InfluxClient(InfluxParams, InfluxGraph):
def __init__(self):
InfluxParams.__init__(self)
InfluxGraph.__init__(self, instrument.influx_data_getter)
InfluxGraph.__init__(self, instrument.influx_data_getter, instrument.title)
class DummyClient(SeaGraph):

View File

@ -1,4 +0,0 @@
[chart]
T_stat=other
T_stat.raw=
T_nvd.raw=