diff --git a/.github/workflows/scenarios-ui-deploy.yml b/.github/workflows/scenarios-ui-deploy.yml index 2a3ba47..8b28c5e 100644 --- a/.github/workflows/scenarios-ui-deploy.yml +++ b/.github/workflows/scenarios-ui-deploy.yml @@ -29,7 +29,7 @@ env: jobs: deploy: - name: Testing + name: deploy runs-on: ubuntu-latest steps: - name: Set inputs diff --git a/pages/components/display.py b/pages/components/display.py index e08c950..a772284 100644 --- a/pages/components/display.py +++ b/pages/components/display.py @@ -5,6 +5,11 @@ import pydeck as pdk import plotly.express as px import geopandas +from streamlit import column_config + +from modules.logging import get_session_logger + +logger = get_session_logger() class View: ''' @@ -46,7 +51,8 @@ class DataframeView(View): display_cols : list[str] The names of the columns to display. By default displays all the columns. ''' - def __init__(self, data=None, selectable=False, display_cols=None, hide_index=True): + def __init__(self, data=None, selectable=False, display_cols=None, hide_index=True, + column_config=None): if data is None: data = pd.DataFrame(columns=display_cols) self.data = data @@ -60,12 +66,19 @@ def __init__(self, data=None, selectable=False, display_cols=None, hide_index=Tr self.display_cols = display_cols self.column_config = {col_name: None for col_name in data.columns} + + if column_config is not None: + self.column_config.update(column_config) + for c in display_cols: + if self.column_config.get(c, None) is not None: + continue if 'TIV' in c: self.column_config[c] = st.column_config.TextColumn(c) else: self.column_config[c] = st.column_config.TextColumn(self.format_column_heading(c)) + def display(self, max_rows=1000, key=None): ''' Show the dataframe. @@ -119,6 +132,7 @@ def colour_status(status): data_styled = data_styled.style.format(format_status, subset=['status']) data_styled = data_styled.map(colour_status, subset=['status']) + logger.info(args) ret = st.dataframe(data_styled, **args) if n_rows > max_rows: diff --git a/pages/components/process.py b/pages/components/process.py index 2fc67e8..136d8d1 100644 --- a/pages/components/process.py +++ b/pages/components/process.py @@ -1,5 +1,12 @@ from oasis_data_manager.errors import OasisException import pandas as pd +from requests.models import HTTPError +import streamlit as st + +from modules.client import ClientInterface +from modules.logging import get_session_logger + +logger = get_session_logger() def number_rows(portfolio_ids, client_interface, filename='location_file', col_name='number_rows'): data = {'id': [], col_name: []} @@ -51,7 +58,10 @@ def enrich_analyses_with_portfolios(analyses, portfolios): return analyses def enrich_analyses_with_models(analyses, models): - models = models[['id', 'model_id', 'supplier_id']].rename(columns={'supplier_id': 'model_supplier'}) + cols = ['id', 'model_id', 'supplier_id'] + if 'model_name' in models: + cols += ['model_name'] + models = models[cols].rename(columns={'supplier_id': 'model_supplier'}) analyses = analyses.set_index('model').join(models.set_index('id')).reset_index(names='model') return analyses @@ -66,3 +76,33 @@ def enrich_analyses(analyses, portfolios=None, models=None): analyses = enrich_analyses_with_models(analyses, models) return analyses + + +@st.cache_data(ttl="1d", hash_funcs={ClientInterface: lambda ci: ci.client.api.tkn_access}) +def add_model_names_to_models_cached(models: pd.DataFrame, + ci: ClientInterface, + col_name='model_name'): + return add_model_names_to_models(models, ci, col_name) + + +def add_model_names_to_models(models, ci, col_name='model_name'): + '''Add the model names to models. Note the model `id` should be the index of the models. + + Args: + models (DataFrame): dataframe containing models endpoint output + ci (ClientInterface): intialised client interface + col_name (str) : column name for model names + ''' + models[col_name] = '' + for model_id, _ in models.iterrows(): + logger.info(model_id) + try: + models.at[model_id, col_name] = ci.models.settings.get(model_id).get('name', None) + except HTTPError as _: + models.at[model_id, col_name] = None + logger.warning(f'No settings for model_id: {model_id}') + + models[col_name] = models[col_name].fillna(models['model_id']) + + return models + diff --git a/pages/scenarios.py b/pages/scenarios.py index eb3a39b..0ae05ab 100644 --- a/pages/scenarios.py +++ b/pages/scenarios.py @@ -5,6 +5,7 @@ from pathlib import Path from requests.exceptions import HTTPError import streamlit as st +from modules.logging import get_session_logger from modules.nav import SidebarNav from modules.config import retrieve_ui_config from modules.rerun import RefreshHandler @@ -17,13 +18,11 @@ import time from json import JSONDecodeError import json -from modules.client import ClientInterface -import logging from pages.components.output import generate_eltcalc_fragment, generate_leccalc_fragment, generate_pltcalc_fragment, model_summary, summarise_inputs, generate_aalcalc_fragment -from pages.components.process import enrich_analyses, enrich_portfolios +from pages.components.process import add_model_names_to_models, add_model_names_to_models_cached, enrich_analyses, enrich_portfolios -logger = logging.getLogger(__name__) +logger = get_session_logger() ########################################################################################## # Header @@ -82,9 +81,15 @@ 'Select an event scenarios by clicking the grey box on the left side of the table.' models = client_interface.models.get(df=True) - display_cols = [ 'model_id', 'supplier_id' ] - - model_view = DataframeView(models, selectable='single', display_cols=display_cols) + models = models.set_index('id', drop=False) + models = add_model_names_to_models_cached(models, client_interface) + display_cols = [ 'model_name', 'supplier_id' ] + column_config = { + 'model_name': 'Scenarios Footprint', + 'supplier_id': 'Supplier' + } + + model_view = DataframeView(models, selectable='single', display_cols=display_cols, column_config=column_config) selected_model = model_view.display() 'Currently, it is not possible to add your own scenarios directly; please describe any scenario you would like to add as a Github issue [here](https://github.com/OasisLMF/OasisPythonUI/issues).' @@ -203,6 +208,8 @@ def analysis_fragment(): analyses = client_interface.analyses.get(df=True) portfolios = client_interface.portfolios.get(df=True) models = client_interface.models.get(df=True) + models = models.set_index('id', drop=False) + models = add_model_names_to_models_cached(models, client_interface) completed_statuses = ['RUN_COMPLETED', 'RUN_CANCELLED', 'RUN_ERROR'] running_statuses = ['RUN_QUEUED', 'RUN_STARTED'] @@ -216,9 +223,16 @@ def analysis_fragment(): analyses = analyses[analyses['status'].isin(valid_statuses)] analyses = enrich_analyses(analyses, portfolios, models).sort_values('id', ascending=False) - display_cols = ['name', 'portfolio_name', 'model_id', 'model_supplier', 'status'] + display_cols = ['name', 'status', 'portfolio_name', 'model_name', 'model_supplier'] + - analyses_view = DataframeView(analyses, display_cols=display_cols, selectable='single') + column_config = { + 'name': 'Analysis Name', + 'model_name': 'Scenarios Footprint', + 'supplier_id': 'Supplier' + } + analyses_view = DataframeView(analyses, display_cols=display_cols, selectable='single', + column_config=column_config) selected = analyses_view.display() oed_group = st.pills("Group output by:", diff --git a/scenarios/deploy_scenarios_ui.sh b/scenarios/deploy_scenarios_ui.sh index 0642db6..0225663 100644 --- a/scenarios/deploy_scenarios_ui.sh +++ b/scenarios/deploy_scenarios_ui.sh @@ -8,7 +8,7 @@ echo "UPDATE_SCENARIOS: $UPDATE_SCENARIOS" echo "MODELS_PATH: $MODELS_PATH" # clear up space on device -docker system prune --volumes +docker system prune --volumes -f if [[ "$WIPE" = 'true' ]] && [[ "$DEPLOY_ALL" = 'false' ]]; then echo "WIPE without DEPLOY_ALL not allowed." @@ -25,7 +25,7 @@ fi if [[ "$DEPLOY_UI" = 'true' ]] && [[ "$DEPLOY_ALL" != 'true' ]]; then git -C "$UI_PATH" pull docker image pull coreoasis/oasis_scenarios - docker compose -f "$UI_PATH/oasis-scenarios-ui.yml" up -d + docker compose -f "$UI_PATH/oasis-scenarios-ui.yml" up -d --no-build fi if [[ "$DEPLOY_ALL" = 'true' ]]; then @@ -47,4 +47,4 @@ if [[ "$DEPLOY_ALL" = 'true' ]]; then fi # run cleanup -docker system prune --volumes +docker system prune --volumes -f