diff --git a/flagging_site/blueprints/flagging.py b/flagging_site/blueprints/flagging.py index 880bbb5d..95c97cbd 100644 --- a/flagging_site/blueprints/flagging.py +++ b/flagging_site/blueprints/flagging.py @@ -68,15 +68,18 @@ def add_to_dict(models, df, reach) -> None: @bp.route('/') def index() -> str: """ - Retrieves data from hobolink and usgs and processes data, then displays data - on `index_model.html` + Retrieves data from database, + then displays data on `index_model.html` returns: render model on index.html """ + + df = latest_model_outputs() + df = df.set_index('reach') flags = { key: val['safe'] for key, val - in latest_model_outputs().items() + in df.to_dict(orient='index').items() } return render_template('index.html', flags=flags) @@ -113,27 +116,20 @@ def output_model() -> str: # Look at no more than 72 hours. hours = min(max(hours, 1), 72) - df = get_data() + df = latest_model_outputs(hours) - reach_model_mapping = { - 2: reach_2_model, - 3: reach_3_model, - 4: reach_4_model, - 5: reach_5_model - } - - if reach in reach_model_mapping: - reach_func = reach_model_mapping[int(reach)] - reach_html_tables = { - reach: stylize_model_output(reach_func(df, rows=hours)) - } - else: - reach_html_tables = { - reach: stylize_model_output(reach_func(df, rows=hours)) - for reach, reach_func - in reach_model_mapping.items() - } + # reach_html_tables is a dict where the index is the reach number + # and the values are HTML code for the table of data to display for + # that particular reach + reach_html_tables = {} + # loop through each reach in df + # extract the subset from the df for that reach + # then convert that df subset to HTML code + # and then add that HTML subset to reach_html_tables + for i in df.reach.unique(): + reach_html_tables[i] = stylize_model_output( df.loc[df['reach'] == i ] ) + return render_template('output_model.html', tables=reach_html_tables) @@ -174,4 +170,4 @@ def get(self): return self.model_api() -api.add_resource(ReachApi, '/api/v1/model') +api.add_resource(ReachApi, '/api/v1/model') \ No newline at end of file diff --git a/flagging_site/data/database.py b/flagging_site/data/database.py index fec31e51..4151ff47 100644 --- a/flagging_site/data/database.py +++ b/flagging_site/data/database.py @@ -61,7 +61,7 @@ def update_database(): # Populate the `hobolink` table. from .hobolink import get_live_hobolink_data - df_hobolink = get_live_hobolink_data() + df_hobolink = get_live_hobolink_data('code_for_boston_export_21d') df_hobolink.to_sql('hobolink', **options) from .model import process_data diff --git a/flagging_site/data/model.py b/flagging_site/data/model.py index adbb5ce0..a1542389 100644 --- a/flagging_site/data/model.py +++ b/flagging_site/data/model.py @@ -145,7 +145,7 @@ def process_data( return df -def reach_2_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: +def reach_2_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame: """Model params: a- rainfall sum 0-24 hrs d- Days since last rain @@ -172,7 +172,7 @@ def reach_2_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: return df[['reach', 'time', 'log_odds', 'probability', 'safe']] -def reach_3_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: +def reach_3_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame: """ a- rainfall sum 0-24 hrs b- rainfall sum 24-48 hr @@ -201,7 +201,7 @@ def reach_3_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: return df[['reach', 'time', 'log_odds', 'probability', 'safe']] -def reach_4_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: +def reach_4_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame: """ a- rainfall sum 0-24 hrs b- rainfall sum 24-48 hr @@ -230,7 +230,7 @@ def reach_4_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: return df[['reach', 'time', 'log_odds', 'probability', 'safe']] -def reach_5_model(df: pd.DataFrame, rows: int = 24) -> pd.DataFrame: +def reach_5_model(df: pd.DataFrame, rows: int = 48) -> pd.DataFrame: """ c- rainfall sum 0-48 hr d- Days since last rain @@ -269,9 +269,17 @@ def all_models(df: pd.DataFrame, *args, **kwargs): def latest_model_outputs(hours: int = 1) -> dict: - if hours != 1: - raise NotImplementedError('Need to work on this!') from .database import execute_sql_from_file - df = execute_sql_from_file('latest_model.sql') - df = df.set_index('reach') - return df.to_dict(orient='index') + + if hours == 1: + df = execute_sql_from_file('return_1_hour_of_model_outputs.sql') + elif hours > 1: + df = execute_sql_from_file('return_48_hours_of_model_outputs.sql') # pull out 48 hours of model outputs + latest_time = max(df['time']) # find most recent timestamp + time_interval = pd.Timedelta(str(hours) + ' hours') # create pandas Timedelta, based on input parameter hours + df = df[ latest_time - df['time']< time_interval ] # reset df to exclude anything from before time_interval ago + + else: + raise ValueError('hours of data to pull cannot be less than one') + + return df \ No newline at end of file diff --git a/flagging_site/data/queries/latest_model.sql b/flagging_site/data/queries/return_1_hour_of_model_outputs.sql similarity index 100% rename from flagging_site/data/queries/latest_model.sql rename to flagging_site/data/queries/return_1_hour_of_model_outputs.sql diff --git a/flagging_site/data/queries/return_48_hours_of_model_outputs.sql b/flagging_site/data/queries/return_48_hours_of_model_outputs.sql new file mode 100644 index 00000000..9e1cd8fd --- /dev/null +++ b/flagging_site/data/queries/return_48_hours_of_model_outputs.sql @@ -0,0 +1,8 @@ +-- This query returns up to 48 hours of the latest data + +SELECT * +FROM model_outputs +WHERE time BETWEEN + (SELECT MAX(time) - interval '47 hours' FROM model_outputs) + AND + (SELECT MAX(time) FROM model_outputs) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 56d11b6d..878c58b8 100644 Binary files a/requirements.txt and b/requirements.txt differ