Skip to content

Commit a8510ce

Browse files
authored
29736 - highlight potential issues - status tab (bcgov#3669)
* restructured cell highlighting functions and update fetch colin_extract logic * remove extra blank line at the end of code cells
1 parent ef96fe9 commit a8510ce

File tree

1 file changed

+86
-22
lines changed

1 file changed

+86
-22
lines changed

data-tool/notebooks/corps_onboarding_process_flow/migration_status_tracking.ipynb

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@
270270
" c.corp_type_cd AS \"{COLUMN_NAMES['corp_type']}\",\n",
271271
" CASE\n",
272272
" WHEN cp.processed_status = 'COMPLETED' THEN 'Migrated'\n",
273+
" WHEN cp.processed_status = 'FAILED' THEN 'Failed'\n",
273274
" WHEN cp.processed_status IS NULL THEN 'Pending'\n",
275+
" ELSE 'Pending'\n",
274276
" END AS \"{COLUMN_NAMES['status']}\",\n",
275277
" cp.create_date::date AS \"{COLUMN_NAMES['date']}\"\n",
276278
"FROM\n",
@@ -291,14 +293,16 @@
291293
" g.id IN ({mig_group_ids})\n",
292294
" AND (\n",
293295
" (cp.processed_status = 'COMPLETED' AND cp.environment = 'prod')\n",
296+
" OR (cp.processed_status = 'FAILED' AND cp.environment = 'prod')\n",
294297
" OR cp.processed_status IS NULL\n",
295298
" )\n",
296299
"ORDER BY\n",
297300
" g.display_name, \n",
298301
" b.display_name,\n",
299302
" CASE\n",
300303
" WHEN cp.processed_status = 'COMPLETED' THEN 0\n",
301-
" ELSE 1\n",
304+
" WHEN cp.processed_status = 'FAILED' THEN 1\n",
305+
" ELSE 2\n",
302306
" END, \n",
303307
" cp.create_date DESC,\n",
304308
" cn.corp_name;\n",
@@ -319,7 +323,7 @@
319323
"\n",
320324
"# Display results\n",
321325
"with pd.option_context('display.max_rows', None):\n",
322-
" display(colin_extract_df)\n"
326+
" display(colin_extract_df)"
323327
]
324328
},
325329
{
@@ -431,7 +435,7 @@
431435
"\n",
432436
"# Display results\n",
433437
"with pd.option_context('display.max_rows', None):\n",
434-
" display(lear_combined_df)\n"
438+
" display(lear_combined_df)"
435439
]
436440
},
437441
{
@@ -548,7 +552,7 @@
548552
"\n",
549553
"# Display results\n",
550554
"with pd.option_context('display.max_rows', None):\n",
551-
" display(colin_oracle_combined_df)\n"
555+
" display(colin_oracle_combined_df)"
552556
]
553557
},
554558
{
@@ -660,7 +664,7 @@
660664
" raise\n",
661665
"\n",
662666
"with pd.option_context('display.max_rows', None):\n",
663-
" display(batch_summary_df)\n"
667+
" display(batch_summary_df)"
664668
]
665669
},
666670
{
@@ -672,6 +676,37 @@
672676
"Generate formatted Excel file with the merged migration tracking data."
673677
]
674678
},
679+
{
680+
"cell_type": "code",
681+
"execution_count": null,
682+
"metadata": {},
683+
"outputs": [],
684+
"source": [
685+
"# Define highlighting rules\n",
686+
"HIGHLIGHTING_RULES = [\n",
687+
" {\n",
688+
" 'column_name': COLUMN_NAMES['affiliated'],\n",
689+
" 'condition_value': 'N',\n",
690+
" 'fill_color': CONFIG['excel_export']['filled_color']\n",
691+
" },\n",
692+
" {\n",
693+
" 'column_name': COLUMN_NAMES['banner_updated_in_colin'], \n",
694+
" 'condition_value': 'N',\n",
695+
" 'fill_color': CONFIG['excel_export']['filled_color']\n",
696+
" },\n",
697+
" {\n",
698+
" 'column_name': COLUMN_NAMES['status'],\n",
699+
" 'condition_value': 'Failed', \n",
700+
" 'fill_color': CONFIG['excel_export']['filled_color']\n",
701+
" },\n",
702+
" {\n",
703+
" 'column_name': COLUMN_NAMES['frozen_in_colin'],\n",
704+
" 'condition_value': 'N',\n",
705+
" 'fill_color': CONFIG['excel_export']['filled_color']\n",
706+
" }\n",
707+
"]"
708+
]
709+
},
675710
{
676711
"cell_type": "code",
677712
"execution_count": null,
@@ -680,22 +715,54 @@
680715
"source": [
681716
"from openpyxl.styles import Font, PatternFill, Alignment\n",
682717
"\n",
718+
"def apply_cell_highlighting(worksheet, highlighting_rules):\n",
719+
" \"\"\"\n",
720+
" Apply conditional highlighting to worksheet cells based on rules.\n",
721+
" \n",
722+
" Args:\n",
723+
" worksheet: The openpyxl worksheet\n",
724+
" highlighting_rules: List of dicts with column_name, condition_value, fill_color\n",
725+
" \n",
726+
" Returns:\n",
727+
" int: Total number of cells highlighted\n",
728+
" \"\"\"\n",
729+
" highlighted_count = 0\n",
730+
" \n",
731+
" # Find column indices for all highlighting rules\n",
732+
" column_indices = {}\n",
733+
" for col_idx, cell in enumerate(worksheet[1], 1):\n",
734+
" for rule in highlighting_rules:\n",
735+
" if cell.value == rule['column_name']:\n",
736+
" column_indices[rule['column_name']] = col_idx\n",
737+
" break\n",
738+
" \n",
739+
" # Apply highlighting based on rules\n",
740+
" for row_num, row in enumerate(worksheet.iter_rows(), 1):\n",
741+
" if row_num == 1: # Skip header row\n",
742+
" continue\n",
743+
" \n",
744+
" for col_idx, cell in enumerate(row, 1):\n",
745+
" # Check each highlighting rule\n",
746+
" for rule in highlighting_rules:\n",
747+
" if col_idx == column_indices.get(rule['column_name']) and cell.value == rule['condition_value']:\n",
748+
" fill = PatternFill(start_color=rule['fill_color'], end_color=rule['fill_color'], fill_type='solid')\n",
749+
" cell.fill = fill\n",
750+
" highlighted_count += 1\n",
751+
" \n",
752+
" return highlighted_count\n",
753+
"\n",
754+
"\n",
683755
"def format_worksheet(worksheet) -> None:\n",
684756
" \"\"\"Format the given worksheet.\"\"\"\n",
757+
" \n",
685758
" # Define display styles\n",
686759
" header_font = Font(size=CONFIG['excel_export']['font_size'], bold=True)\n",
687760
" normal_font = Font(size=CONFIG['excel_export']['font_size'])\n",
688-
" red_fill = PatternFill(start_color=CONFIG['excel_export']['filled_color'], end_color=CONFIG['excel_export']['filled_color'], fill_type='solid')\n",
689761
"\n",
690-
" # Find the Affiliated column index\n",
691-
" affiliated_col_idx = None\n",
692-
" for col_idx, cell in enumerate(worksheet[1], 1):\n",
693-
" if cell.value == COLUMN_NAMES['affiliated']:\n",
694-
" affiliated_col_idx = col_idx\n",
695-
" break\n",
762+
" # Apply cell highlighting\n",
763+
" highlighted_count = apply_cell_highlighting(worksheet, HIGHLIGHTING_RULES)\n",
696764
"\n",
697-
" # Format and highlight rows\n",
698-
" highlighted_count = 0\n",
765+
" # Format rows (excluding highlighting which is now handled separately)\n",
699766
" for row_num, row in enumerate(worksheet.iter_rows(), 1):\n",
700767
" for col_idx, cell in enumerate(row, 1):\n",
701768
" if row_num == 1:\n",
@@ -705,11 +772,6 @@
705772
" # Data rows\n",
706773
" cell.font = normal_font\n",
707774
" cell.alignment = Alignment(horizontal='left')\n",
708-
" \n",
709-
" # Highlight Affiliated column if value is 'N'\n",
710-
" if col_idx == affiliated_col_idx and cell.value == 'N':\n",
711-
" cell.fill = red_fill\n",
712-
" highlighted_count += 1\n",
713775
" \n",
714776
" # Freeze header row\n",
715777
" worksheet.freeze_panes = 'A2'\n",
@@ -737,7 +799,9 @@
737799
" adjusted_width = min(max_length + 10, CONFIG['excel_export']['max_column_width'])\n",
738800
" worksheet.column_dimensions[column_letter].width = adjusted_width\n",
739801
"\n",
740-
" print(f\"Red highlighting applied to {highlighted_count} cells in '{COLUMN_NAMES['affiliated']}' column with 'N'\")"
802+
" # Print highlighting summary\n",
803+
" rule_names = [rule['column_name'] for rule in HIGHLIGHTING_RULES]\n",
804+
" print(f\"In {worksheet.title} tab:\\n Red highlighting applied to {highlighted_count} cells across columns: {', '.join(rule_names)}\")"
741805
]
742806
},
743807
{
@@ -798,7 +862,7 @@
798862
],
799863
"metadata": {
800864
"kernelspec": {
801-
"display_name": "3.8.17",
865+
"display_name": "Python 3",
802866
"language": "python",
803867
"name": "python3"
804868
},
@@ -812,7 +876,7 @@
812876
"name": "python",
813877
"nbconvert_exporter": "python",
814878
"pygments_lexer": "ipython3",
815-
"version": "3.8.17"
879+
"version": "3.10.12"
816880
}
817881
},
818882
"nbformat": 4,

0 commit comments

Comments
 (0)