diff --git a/influxdb.py b/influxdb.py index f775cfc..29e48d6 100644 --- a/influxdb.py +++ b/influxdb.py @@ -142,7 +142,7 @@ class InfluxDataGetter: components = self._get_device_name_components(time) return "/".join(components) - def get_curves_data_frame(self, variables, times, interval): + def get_curves_data_frame(self, variables, times, interval, variables_name_label_map=None): """ Gets the curves for the given variables within a timerange times, as a pandas dataframe. All curves are on a single common time axis. @@ -156,7 +156,7 @@ class InfluxDataGetter: variables ([(str)]) : an array of variable names (Influx) to get the curves for. times ([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 seconds). Allows data binning. - + variables_name_label_map ({(str):(str)} | None) : a dictionnary containing curve labels, indexed by INFLUX names. The corresponding label will be used in the TSV header for each variable if found, else the influx name. Returns : pandas.DataFrame : the curves in a single pandas DataFrame """ @@ -213,25 +213,27 @@ class InfluxDataGetter: if data_frame.empty: continue + variable_df_column_name = variables_name_label_map.get(variable, variable) if not variables_name_label_map == None else variable + data_frame.rename(columns={variable_name_for_query : variable_df_column_name}, inplace=True) data_frame.drop(["result", "table"], axis=1, inplace=True) data_frame.sort_values(by=["timestamp"], inplace=True) data_frame.reset_index() - variables_info[variable] = {} - variables_info[variable]["expired_ranges"] = [] + variables_info[variable_df_column_name] = {} + variables_info[variable_df_column_name]["expired_ranges"] = [] # Identify time windows for which the curve is expired for index, row in data_frame.iterrows(): if row["expired"] == "True": - data_frame.loc[index, variable] = NaN - variables_info[variable]["expired_ranges"].append([row["timestamp"]]) + data_frame.loc[index, variable_df_column_name] = NaN + variables_info[variable_df_column_name]["expired_ranges"].append([row["timestamp"]]) elif row["expired"] == "False": - if len(variables_info[variable]["expired_ranges"]) > 0 and len(variables_info[variable]["expired_ranges"][-1]) == 1: - variables_info[variable]["expired_ranges"][-1].append(row["timestamp"]) + if len(variables_info[variable_df_column_name]["expired_ranges"]) > 0 and len(variables_info[variable_df_column_name]["expired_ranges"][-1]) == 1: + variables_info[variable_df_column_name]["expired_ranges"][-1].append(row["timestamp"]) data_frame.reset_index() data_frame.drop(["expired"], axis=1, inplace=True) - variables_info[variable]["df"] = data_frame + variables_info[variable_df_column_name]["df"] = data_frame res = None non_empty_variables = list(variables_info.keys()) diff --git a/influxgraph.py b/influxgraph.py index 3e954df..1a5e32c 100644 --- a/influxgraph.py +++ b/influxgraph.py @@ -32,7 +32,7 @@ class InfluxGraph: self.livemode = self.HISTORICAL self.end_query = 0 self.lastvalues = {} - self.variables = [] + self.variables = {} # name:label def milliseconds_to_nano(self, milliseconds): """ @@ -141,7 +141,7 @@ class InfluxGraph: blocks = self.influx_data_getter.get_available_variables_at_time([start_time, end_time]) 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"] for block in blocks for variable in block["curves"]] + self.variables = {variable["name"]:variable["label"] for block in blocks for variable in block["curves"]} assign_colors_to_curves(blocks) result = dict(type='var_list') @@ -185,7 +185,7 @@ class InfluxGraph: queried_variables = variables.split(',') if interval != "None" : interval = int(interval) - df = self.influx_data_getter.get_curves_data_frame(queried_variables, [start, end], interval) + df = self.influx_data_getter.get_curves_data_frame(queried_variables, [start, end], interval, self.variables) mem = io.BytesIO() df.to_csv(mem, sep="\t", index=False, float_format="%.15g", na_rep=nan) @@ -207,7 +207,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_values(list(self.variables.keys()), self.lastvalues, now) for variable in self.lastvalues.keys(): if variable in result.keys():