diff --git a/app.py b/app.py index 7b7fe68..30b634b 100644 --- a/app.py +++ b/app.py @@ -14,6 +14,7 @@ from dashboard.components.y_axis_select import YAxisSelect from dashboard.graphs import plot_connections_statistics, plot_pressure_bar_chart, plot_sensors from dashboard.queries import ROW_LIMIT, BigQuery +from dashboard.utils import get_cleaned_sensor_column_names logger = logging.getLogger(__name__) @@ -291,13 +292,14 @@ def plot_sensors_graph( @cache.memoize(timeout=0) def get_pressure_profiles_for_time_window(installation_reference, node_id, start_datetime, finish_datetime): - """Get pressure profiles for the given node during the given time window. + """Get pressure profiles for the given node during the given time window along with the minimum and maximum + pressures over all the sensors over that window. :param str installation_reference: :param str node_id: :param datetime.datetime start_datetime: :param datetime.datetime finish_datetime: - :return pandas.DataFrame: + :return (pandas.DataFrame, float, float): the pressure profiles, minimum pressure, and maximum pressure for the time window """ df, _ = BigQuery().get_sensor_data( installation_reference=installation_reference, @@ -314,7 +316,9 @@ def get_pressure_profiles_for_time_window(installation_reference, node_id, start finish_datetime.isoformat(), ) - return df + sensor_column_names, _ = get_cleaned_sensor_column_names(df) + df_with_sensors_only = df[sensor_column_names] + return (df, df_with_sensors_only.min().min(), df_with_sensors_only.max().max()) @app.callback( @@ -334,7 +338,7 @@ def plot_pressure_profile_graph(installation_reference, node_id, date, hour, min initial_datetime = dt.datetime.combine(date=dt.date.fromisoformat(date), time=dt.time(hour, minute, second)) - df = get_pressure_profiles_for_time_window( + df, minimum, maximum = get_pressure_profiles_for_time_window( installation_reference=installation_reference, node_id=node_id, start_datetime=initial_datetime, @@ -349,7 +353,7 @@ def plot_pressure_profile_graph(installation_reference, node_id, date, hour, min ] logger.debug("Filtered pressure profile time window for single datetime.") - return plot_pressure_bar_chart(df) + return plot_pressure_bar_chart(df, minimum, maximum) @app.callback( diff --git a/dashboard/graphs.py b/dashboard/graphs.py index f2faa7e..efff4d2 100644 --- a/dashboard/graphs.py +++ b/dashboard/graphs.py @@ -1,9 +1,7 @@ -import re - from plotly import express as px from dashboard.queries import BigQuery -from dashboard.utils import generate_time_range +from dashboard.utils import generate_time_range, get_cleaned_sensor_column_names def plot_connections_statistics( @@ -39,7 +37,7 @@ def plot_sensors(installation_reference, node_id, sensor_name, time_range, custo finish=finish, ) - original_sensor_names, cleaned_sensor_names = _get_cleaned_sensor_column_names(df) + original_sensor_names, cleaned_sensor_names = get_cleaned_sensor_column_names(df) df.rename( columns={ @@ -54,8 +52,8 @@ def plot_sensors(installation_reference, node_id, sensor_name, time_range, custo return (figure, data_limit_applied) -def plot_pressure_bar_chart(df): - original_sensor_names, cleaned_sensor_names = _get_cleaned_sensor_column_names(df) +def plot_pressure_bar_chart(df, minimum, maximum): + original_sensor_names, cleaned_sensor_names = get_cleaned_sensor_column_names(df) df_transposed = df[original_sensor_names].transpose() df_transposed["Barometer number"] = cleaned_sensor_names @@ -66,17 +64,5 @@ def plot_pressure_bar_chart(df): figure = px.line(df_transposed, x="Barometer number", y="Raw value") figure.add_bar(x=df_transposed["Barometer number"], y=df_transposed["Raw value"]) - figure.update_layout(showlegend=False) + figure.update_layout(showlegend=False, yaxis_range=[minimum, maximum]) return figure - - -def _get_cleaned_sensor_column_names(dataframe): - """Get cleaned sensor column names for a dataframe when the columns are named like "f0_", "f1_"... "fn_" for `n` - sensors. - - :param pandas.DataFrame dataframe: a dataframe containing columns of sensor data named like "f0_", "f1_"... - :return (list, list): the uncleaned and cleaned sensor names - """ - original_names = [column for column in dataframe.columns if column.startswith("f") and column.endswith("_")] - cleaned_names = [re.findall(r"f(\d+)_", sensor_name)[0] for sensor_name in original_names] - return original_names, cleaned_names diff --git a/dashboard/utils.py b/dashboard/utils.py index ae542c4..5473bab 100644 --- a/dashboard/utils.py +++ b/dashboard/utils.py @@ -1,4 +1,5 @@ import datetime +import re TIME_RANGE_OPTIONS = { @@ -36,3 +37,15 @@ def generate_time_range(time_range, custom_start_date=None, custom_end_date=None finish = datetime.datetime.now() start = finish - TIME_RANGE_OPTIONS[time_range] return start, finish + + +def get_cleaned_sensor_column_names(dataframe): + """Get cleaned sensor column names for a dataframe when the columns are named like "f0_", "f1_"... "fn_" for `n` + sensors. + + :param pandas.DataFrame dataframe: a dataframe containing columns of sensor data named like "f0_", "f1_"... + :return (list, list): the uncleaned and cleaned sensor names + """ + original_names = [column for column in dataframe.columns if column.startswith("f") and column.endswith("_")] + cleaned_names = [re.findall(r"f(\d+)_", sensor_name)[0] for sensor_name in original_names] + return original_names, cleaned_names