From 1053b7a4eff2b3f3c8dc6f190c5fd510b017ebeb Mon Sep 17 00:00:00 2001 From: Umar Farooq Ghumman Date: Wed, 12 Jun 2024 20:59:25 -0500 Subject: [PATCH 1/2] QueryProfiler UI Main Page - Added dropdown for schema and key --- project/ui/qprof_main.ipynb | 107 +++++++++++++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 7 deletions(-) diff --git a/project/ui/qprof_main.ipynb b/project/ui/qprof_main.ipynb index babd8c32..03830879 100644 --- a/project/ui/qprof_main.ipynb +++ b/project/ui/qprof_main.ipynb @@ -211,6 +211,68 @@ "____" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "b1c447e3-0a23-41ff-bcf5-0aa2f83b4141", + "metadata": {}, + "outputs": [], + "source": [ + "# Function to update the options of the key dropdown based on the selection of the schema dropdown\n", + "def update_key_dropdown_options(change):\n", + " # Get the selected schema from the first dropdown\n", + " selected_schema = change.new\n", + " \n", + " # Query for the table names based on the selected schema\n", + " query = f\"SELECT DISTINCT table_name FROM tables WHERE table_name LIKE '%_dc_explain_plans_%' AND table_schema = '{selected_schema}' ORDER BY 1;\"\n", + " vdf = vp.vDataFrame(query)\n", + " \n", + " # Manipulate table names\n", + " length_to_remove = len(selected_schema) + len(\"_dc_explain_plans_\") + 1\n", + " vdf['table_name'] = vdf['table_name'].apply(f\"SUBSTR(table_name, {length_to_remove})\")\n", + " \n", + " # Update options of the second dropdown\n", + " key_dropdown.options = [i[0] for i in vdf.to_list()]\n", + "\n", + "# Function to observe the value of the key and target_schema widget and disable both dropdowns if there is any user value\n", + "def observe_value_change(change):\n", + " # If the key widget or target_schema widget has any user value, disable both dropdowns\n", + " if key.value or target_schema.value:\n", + " schema_dropdown.disabled = True\n", + " key_dropdown.disabled = True\n", + " else:\n", + " schema_dropdown.disabled = False\n", + " key_dropdown.disabled = False\n", + "\n", + "\n", + "# Query for the options of the first dropdown\n", + "query = \"SELECT DISTINCT table_schema FROM tables WHERE table_name LIKE '%_dc_explain_plans_%' ORDER BY 1;\"\n", + "vdf = vp.vDataFrame(query)\n", + "schema_dropdown_options = [i[0] for i in vdf.to_list()]\n", + "\n", + "# Create the first dropdown\n", + "schema_dropdown = widgets.Dropdown(\n", + " options=schema_dropdown_options,\n", + " value = None,\n", + " description='Schema List',\n", + " disabled=False,\n", + " layout={'width': \"250px\"}\n", + ")\n", + "schema_dropdown.style.description_width = '100px'\n", + "\n", + "# Create the second dropdown with initial options\n", + "key_dropdown = widgets.Dropdown(\n", + " options=[], # Initial options, will be updated dynamically\n", + " description='Key List',\n", + " disabled=False,\n", + " layout={'width': \"250px\"}\n", + ")\n", + "key_dropdown.style.description_width = '100px'\n", + "\n", + "# Observe changes in the first dropdown and trigger the update function\n", + "schema_dropdown.observe(update_key_dropdown_options, names='value')" + ] + }, { "cell_type": "code", "execution_count": null, @@ -228,8 +290,8 @@ " global key_val, target_schema_val\n", " output_2.clear_output(wait=True)\n", " with output_2:\n", - " key_val = key.value\n", - " target_schema_val = target_schema.value\n", + " key_val = key.value if key.value else key_dropdown.value\n", + " target_schema_val = target_schema.value if target_schema.value else schema_dropwdown.value\n", " logging.info(f\"[Query Profile Main Page] For load from schema and key option, saved key value as :{key_val}, target schema as {target_schema_val}. \")\n", " reset_values(key_val = key_val, target_schema_val = target_schema_val)\n", "\n", @@ -244,8 +306,8 @@ " global key_val, target_schema_val, file_val\n", " output_3.clear_output(wait=True)\n", " with output_3:\n", - " key_val = key.value\n", - " target_schema_val = target_schema.value\n", + " key_val = key.value if key.value else key_dropdown.value\n", + " target_schema_val = target_schema.value if target_schema.value else schema_dropdown.value\n", " file_val = uploader.value\n", " logging.info(f\"[Query Profile Main Page] For load from file option, saved key value as :{key_val}, target schema as {target_schema_val}, file name as {file_val}.\")\n", " if target_schema_val == \"\" or file_val == \"\":\n", @@ -289,6 +351,34 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "18fa4233-c940-47e0-89a2-ca2937642916", + "metadata": {}, + "outputs": [], + "source": [ + "html_arrow = \"\"\"\n", + "\n", + "\n", + "
\n", + " Or select from a list\n", + " \n", + "
\n", + "\"\"\"" + ] + }, { "cell_type": "code", "execution_count": null, @@ -304,10 +394,13 @@ "uploader_combo = widgets.HBox([uploader, create_tooltip(\"Select the tar file that you obtained after exporting the query profile object.\")])\n", "from ipyfilechooser import FileChooser\n", "\n", + "# Observe changes in the key and target_schema widget to disable both dropdowns if there is any user value\n", + "key.observe(observe_value_change, names='value')\n", + "target_schema.observe(observe_value_change, names='value')\n", "\n", "tab = widgets.Tab()\n", - "target_schema_combo = widgets.HBox([target_schema, create_tooltip(\"Enter the target schema that you used to save the QueryProfiler object. This will not work if you have not saved your query profile already.\")])\n", - "key_combo = widgets.HBox([key, create_tooltip(\"Enter the unique key that you used to save the QueryProfiler object. This will not work if you have not saved your query profile already.\")])\n", + "target_schema_combo = widgets.HBox([target_schema,widgets.HTML(html_arrow), schema_dropdown, create_tooltip(\"Enter the target schema that you used to save the QueryProfiler object. This will not work if you have not saved your query profile already.\")])\n", + "key_combo = widgets.HBox([key, widgets.HTML(html_arrow), key_dropdown, create_tooltip(\"Enter the unique key that you used to save the QueryProfiler object. This will not work if you have not saved your query profile already.\")])\n", "child_1 = widgets.VBox([target_schema_combo,key_combo,button_2, output_2])\n", "child_2 = widgets.VBox([target_schema_combo,key_combo,uploader_combo,button_3, output_3])\n", "tab.children = [child_1, child_2]\n", @@ -660,7 +753,7 @@ "\n", "\n", "
\n", - " Page version update date: 6/10/2024\n", + " Page version update date: 6/12/2024\n", "
\n", "\n", "" From e12f89e363910b03fe76a597be54c7e9c2199d25 Mon Sep 17 00:00:00 2001 From: Umar Farooq Ghumman Date: Wed, 12 Jun 2024 21:25:26 -0500 Subject: [PATCH 2/2] Added check for schemas. --- project/ui/qprof_main.ipynb | 69 ++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/project/ui/qprof_main.ipynb b/project/ui/qprof_main.ipynb index 03830879..04720596 100644 --- a/project/ui/qprof_main.ipynb +++ b/project/ui/qprof_main.ipynb @@ -273,6 +273,31 @@ "schema_dropdown.observe(update_key_dropdown_options, names='value')" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc6abb5f-e563-470c-9348-f309bf3c0501", + "metadata": {}, + "outputs": [], + "source": [ + "schema_name = \"alu\"\n", + "query = f\"SELECT schema_name FROM v_catalog.schemata WHERE schema_name = '{schema_name}';\"\n", + "vdf = vp.vDataFrame(query)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d3612bc2-cc28-45ca-a77c-f30dd05660cd", + "metadata": {}, + "outputs": [], + "source": [ + "def schema_exists(schema_name):\n", + " query = f\"SELECT schema_name FROM v_catalog.schemata WHERE schema_name = '{schema_name}';\"\n", + " vdf = vp.vDataFrame(query)\n", + " return vdf.shape()[0] > 0" + ] + }, { "cell_type": "code", "execution_count": null, @@ -292,8 +317,11 @@ " with output_2:\n", " key_val = key.value if key.value else key_dropdown.value\n", " target_schema_val = target_schema.value if target_schema.value else schema_dropwdown.value\n", - " logging.info(f\"[Query Profile Main Page] For load from schema and key option, saved key value as :{key_val}, target schema as {target_schema_val}. \")\n", - " reset_values(key_val = key_val, target_schema_val = target_schema_val)\n", + " if not schema_exists(target_schema_val):\n", + " print(\"\\033[91m\\033[1m Error! The schema does not exist. \\033[0m\\033[0m\")\n", + " else:\n", + " logging.info(f\"[Query Profile Main Page] For load from schema and key option, saved key value as :{key_val}, target schema as {target_schema_val}. \")\n", + " reset_values(key_val = key_val, target_schema_val = target_schema_val)\n", "\n", "button_2.on_click(on_button_clicked_2)\n", "\n", @@ -516,11 +544,14 @@ " global target_schema_val_2, key_val_2, statement_val, transaction_val\n", " with output_1:\n", " target_schema_val_2 = target_schema_2.value\n", - " key_val_2 = key_2.value\n", - " transaction_val = transaction.value\n", - " statement_val = statement.value\n", - " logging.info(f\"[Query Profile Main Page] For create option, saved key value as :{key_val_2}, target schema as {target_schema_val_2}, transaction id as {transaction_val}, statement id as {statement_val}.\")\n", - " reset_values(statement_val = statement_val, transaction_val = transaction_val, target_schema_val_2 = target_schema_val_2, key_val_2 = key_val_2)\n", + " if not schema_exists(target_schema_val_2):\n", + " print(\"\\033[91m\\033[1m Error! The schema does not exist. \\033[0m\\033[0m\")\n", + " else:\n", + " key_val_2 = key_2.value\n", + " transaction_val = transaction.value\n", + " statement_val = statement.value\n", + " logging.info(f\"[Query Profile Main Page] For create option, saved key value as :{key_val_2}, target schema as {target_schema_val_2}, transaction id as {transaction_val}, statement id as {statement_val}.\")\n", + " reset_values(statement_val = statement_val, transaction_val = transaction_val, target_schema_val_2 = target_schema_val_2, key_val_2 = key_val_2)\n", "\n", "button_1.on_click(on_button_clicked_1)\n", "create_0 = widgets.VBox([target_schema_2_combo, key_2_combo,transaction_combo,statement_combo, button_1, output_1])" @@ -557,10 +588,13 @@ " global query_val, target_schema_val_2, key_val_2\n", " with output_0:\n", " target_schema_val_2 = target_schema_2.value\n", - " key_val_2 = key_2.value\n", - " query_val = query.value\n", - " logging.info(f\"\"\"[Query Profile Main Page] For create using query option, saved key value as :{key_val_2}, target schema as {target_schema_val_2}, query as \"{query_val}\".\"\"\")\n", - " reset_values(query_val = query_val, target_schema_val_2 = target_schema_val_2, key_val_2 = key_val_2)\n", + " if not schema_exists(target_schema_val_2):\n", + " print(\"\\033[91m\\033[1m Error! The schema does not exist. \\033[0m\\033[0m\")\n", + " else:\n", + " key_val_2 = key_2.value\n", + " query_val = query.value\n", + " logging.info(f\"\"\"[Query Profile Main Page] For create using query option, saved key value as :{key_val_2}, target schema as {target_schema_val_2}, query as \"{query_val}\".\"\"\")\n", + " reset_values(query_val = query_val, target_schema_val_2 = target_schema_val_2, key_val_2 = key_val_2)\n", "\n", "button.on_click(on_button_clicked_0)\n", "create_1 = widgets.VBox([target_schema_2_combo, key_2_combo, query, button, output_0])\n" @@ -596,11 +630,14 @@ " print(\"\\033[91m\\033[1m Error \\033[0m: Please check the input syntax. List of tuples is expected.\")\n", " else:\n", " target_schema_val_2 = target_schema_2.value\n", - " key_val_2 = key_2.value\n", - " transaction_statement_list_val = transaction_statement_list.value\n", - " transaction_statement_list_val.replace(\" \", \"\")\n", - " logging.info(f\"[Query Profile Main Page] For create using multiple transaction ids and keys, saved key value as :{key_val_2}, target schema as {target_schema_val_2}, List of transactions and statements as {transaction_statement_list_val}.\")\n", - " reset_values(transaction_statement_list_val = transaction_statement_list_val, target_schema_val_2 = target_schema_val_2, key_val_2 = key_val_2)\n", + " if not schema_exists(target_schema_val_2):\n", + " print(\"\\033[91m\\033[1m Error! The schema does not exist. \\033[0m\\033[0m\")\n", + " else:\n", + " key_val_2 = key_2.value\n", + " transaction_statement_list_val = transaction_statement_list.value\n", + " transaction_statement_list_val.replace(\" \", \"\")\n", + " logging.info(f\"[Query Profile Main Page] For create using multiple transaction ids and keys, saved key value as :{key_val_2}, target schema as {target_schema_val_2}, List of transactions and statements as {transaction_statement_list_val}.\")\n", + " reset_values(transaction_statement_list_val = transaction_statement_list_val, target_schema_val_2 = target_schema_val_2, key_val_2 = key_val_2)\n", "\n", "button_4.on_click(on_button_clicked_4)\n", "create_list_transaction_statement = widgets.VBox([target_schema_2_combo, key_2_combo,transaction_statement_list_combo, button_4, output_4])"