From b459e2ebc7c6ddc3675b0b2585d6163a129b4a02 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 11 Nov 2024 09:57:33 +0200 Subject: [PATCH] fixed issue 266 --- src/sempy_labs/_helper_functions.py | 2 +- src/sempy_labs/_model_bpa_bulk.py | 44 +++++++++++++++-------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/sempy_labs/_helper_functions.py b/src/sempy_labs/_helper_functions.py index d5360cf9..ba571df3 100644 --- a/src/sempy_labs/_helper_functions.py +++ b/src/sempy_labs/_helper_functions.py @@ -1144,7 +1144,7 @@ def _get_max_run_id(lakehouse: str, table_name: str) -> int: spark = SparkSession.builder.getOrCreate() query = f"SELECT MAX(RunId) FROM {lakehouse}.{table_name}" dfSpark = spark.sql(query) - max_run_id = dfSpark.collect()[0][0] + max_run_id = dfSpark.collect()[0][0] or 0 return max_run_id diff --git a/src/sempy_labs/_model_bpa_bulk.py b/src/sempy_labs/_model_bpa_bulk.py index 233902f4..e73d4463 100644 --- a/src/sempy_labs/_model_bpa_bulk.py +++ b/src/sempy_labs/_model_bpa_bulk.py @@ -73,7 +73,6 @@ def run_model_bpa_bulk( ) lakeT = get_lakehouse_tables(lakehouse=lakehouse, workspace=lakehouse_workspace) lakeT_filt = lakeT[lakeT["Table Name"] == output_table] - # query = f"SELECT MAX(RunId) FROM {lakehouse}.{output_table}" if len(lakeT_filt) == 0: runId = 1 else: @@ -151,28 +150,31 @@ def run_model_bpa_bulk( ) print(e) - df["Severity"].replace(icons.severity_mapping) + if len(df) == 0: + print(f"{icons.yellow_dot} No BPA results to save for the '{wksp}' workspace.") + else: + df["Severity"].replace(icons.severity_mapping) - # Append save results individually for each workspace (so as not to create a giant dataframe) - print( - f"{icons.in_progress} Saving the Model BPA results of the '{wksp}' workspace to the '{output_table}' within the '{lakehouse}' lakehouse within the '{lakehouse_workspace}' workspace..." - ) + # Append save results individually for each workspace (so as not to create a giant dataframe) + print( + f"{icons.in_progress} Saving the Model BPA results of the '{wksp}' workspace to the '{output_table}' within the '{lakehouse}' lakehouse within the '{lakehouse_workspace}' workspace..." + ) - schema = { - key.replace(" ", "_"): value - for key, value in icons.bpa_schema.items() - } - - save_as_delta_table( - dataframe=df, - delta_table_name=output_table, - write_mode="append", - schema=schema, - merge_schema=True, - ) - print( - f"{icons.green_dot} Saved BPA results to the '{output_table}' delta table." - ) + schema = { + key.replace(" ", "_"): value + for key, value in icons.bpa_schema.items() + } + + save_as_delta_table( + dataframe=df, + delta_table_name=output_table, + write_mode="append", + schema=schema, + merge_schema=True, + ) + print( + f"{icons.green_dot} Saved BPA results to the '{output_table}' delta table." + ) print(f"{icons.green_dot} Bulk BPA scan complete.")