|
| 1 | +import sempy.fabric as fabric |
| 2 | +import pandas as pd |
| 3 | +import sempy_labs._icons as icons |
| 4 | +from typing import Optional |
| 5 | +from sempy_labs._helper_functions import ( |
| 6 | + resolve_workspace_name_and_id, |
| 7 | + _base_api, |
| 8 | + _create_dataframe, |
| 9 | +) |
| 10 | +from uuid import UUID |
| 11 | +import requests |
| 12 | +import json |
| 13 | + |
| 14 | + |
| 15 | +def list_kql_dashboards(workspace: Optional[str | UUID] = None) -> pd.DataFrame: |
| 16 | + """ |
| 17 | + Shows the KQL dashboards within a workspace. |
| 18 | +
|
| 19 | + This is a wrapper function for the following API: `Items - List KQL Dashboards <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/list-kql-dashboards>`_. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + workspace : str | uuid.UUID, default=None |
| 24 | + The Fabric workspace name or ID. |
| 25 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 26 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 27 | +
|
| 28 | + Returns |
| 29 | + ------- |
| 30 | + pandas.DataFrame |
| 31 | + A pandas dataframe showing the KQL dashboards within a workspace. |
| 32 | + """ |
| 33 | + |
| 34 | + columns = { |
| 35 | + "KQL Dashboard Name": "string", |
| 36 | + "KQL Dashboard Id": "string", |
| 37 | + "Description": "string", |
| 38 | + } |
| 39 | + df = _create_dataframe(columns=columns) |
| 40 | + |
| 41 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 42 | + |
| 43 | + responses = _base_api( |
| 44 | + request=f"v1/workspaces/{workspace_id}/kqlDashboards", uses_pagination=True |
| 45 | + ) |
| 46 | + |
| 47 | + for r in responses: |
| 48 | + for v in r.get("value", []): |
| 49 | + new_data = { |
| 50 | + "KQL Dashboard Name": v.get("displayName"), |
| 51 | + "KQL Dashboard Id": v.get("id"), |
| 52 | + "Description": v.get("description"), |
| 53 | + } |
| 54 | + df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True) |
| 55 | + |
| 56 | + return df |
| 57 | + |
| 58 | + |
| 59 | +def create_kql_dashboard( |
| 60 | + name: str, |
| 61 | + content: Optional[str] = None, |
| 62 | + description: Optional[str] = None, |
| 63 | + workspace: Optional[str | UUID] = None, |
| 64 | +): |
| 65 | + """ |
| 66 | + Creates a KQL dashboard. |
| 67 | +
|
| 68 | + This is a wrapper function for the following API: `Items - Create KQL Dashboard <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/create-kql-dashboard>`_. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + name: str |
| 73 | + Name of the KQL dashboard. |
| 74 | + content: Optional[dict], default=None |
| 75 | + The RealTimeDashboard.json content. |
| 76 | + description : str, default=None |
| 77 | + A description of the environment. |
| 78 | + workspace : str | uuid.UUID, default=None |
| 79 | + The Fabric workspace name or ID. |
| 80 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 81 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 82 | + """ |
| 83 | + |
| 84 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 85 | + |
| 86 | + payload = {"displayName": name} |
| 87 | + |
| 88 | + if description: |
| 89 | + payload["description"] = description |
| 90 | + |
| 91 | + if content: |
| 92 | + # platform_payload = '' |
| 93 | + payload["definition"] = { |
| 94 | + "format": None, |
| 95 | + "parts": [ |
| 96 | + { |
| 97 | + "path": "RealTimeDashboard.json", |
| 98 | + "payload": content, |
| 99 | + "payloadType": "InlineBase64", |
| 100 | + }, |
| 101 | + # { |
| 102 | + # "path": ".platform", |
| 103 | + # "payload": platform_payload, |
| 104 | + # "payloadType": "InlineBase64" |
| 105 | + # } |
| 106 | + ], |
| 107 | + } |
| 108 | + |
| 109 | + _base_api( |
| 110 | + request=f"v1/workspaces/{workspace_id}/kqlDashboards", |
| 111 | + method="post", |
| 112 | + payload=payload, |
| 113 | + status_codes=201, |
| 114 | + ) |
| 115 | + |
| 116 | + print( |
| 117 | + f"{icons.green_dot} The '{name}' KQL dashboard has been created within the '{workspace_name}' workspace." |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def create_workspace_montiring_dashboard( |
| 122 | + name: str = "Fabric Workspace Monitoring Dashboard", |
| 123 | + workspace: Optional[str | UUID] = None, |
| 124 | +): |
| 125 | + """ |
| 126 | + Creates a workspace monitoring dashboard. |
| 127 | +
|
| 128 | + This is a wrapper function for the following API: `Items - Create Workspace Monitoring Dashboard <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/create-workspace-monitoring-dashboard>`_. |
| 129 | +
|
| 130 | + Parameters |
| 131 | + ---------- |
| 132 | + name : str, default="Fabric Workspace Monitoring Dashboard" |
| 133 | + The name of the workspace monitoring dashboard. |
| 134 | + workspace : str | uuid.UUID, default=None |
| 135 | + The Fabric workspace name or ID. |
| 136 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 137 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 138 | + """ |
| 139 | + from sempy_labs._kql_databases import list_kql_databases |
| 140 | + |
| 141 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 142 | + db_name = "Monitoring KQL database" |
| 143 | + |
| 144 | + url = "https://raw.githubusercontent.com/microsoft/fabric-toolbox/refs/heads/main/monitoring/workspace-monitoring-dashboards/Fabric%20Workspace%20Monitoring%20Dashboard.json" |
| 145 | + response = requests.get(url) |
| 146 | + content = json.loads(response.content) |
| 147 | + |
| 148 | + # Resolve the cluster URI and database ID |
| 149 | + dfK = list_kql_databases(workspace=workspace) |
| 150 | + dfK_filt = dfK[dfK["KQL Database Name"] == db_name] |
| 151 | + if dfK_filt.empty: |
| 152 | + raise ValueError( |
| 153 | + f"{icons.red_dot} Workspace monitoring is not set up for the '{workspace_name}' workspace." |
| 154 | + ) |
| 155 | + cluster_uri = dfK_filt["Query Service URI"].iloc[0] |
| 156 | + database_id = dfK_filt["KQL Database Id"].iloc[0] |
| 157 | + |
| 158 | + content["dataSources"] = [ |
| 159 | + { |
| 160 | + "kind": "kusto-trident", |
| 161 | + "scopeId": "kusto-trident", |
| 162 | + "clusterUri": cluster_uri, |
| 163 | + "database": database_id, |
| 164 | + "name": db_name, |
| 165 | + "id": "b7d7ce56-c612-4d4f-ab1a-1a6f6212efd0", |
| 166 | + "workspace": workspace_id, |
| 167 | + } |
| 168 | + ] |
| 169 | + |
| 170 | + create_kql_dashboard(name=name, content=content, workspace=workspace) |
| 171 | + |
| 172 | + |
| 173 | +def delete_kql_dashboard( |
| 174 | + kql_dashboard: str | UUID, workspace: Optional[str | UUID] = None |
| 175 | +): |
| 176 | + """ |
| 177 | + Deletes a KQL database. |
| 178 | +
|
| 179 | + This is a wrapper function for the following API: `Items - Delete KQL Dashboard <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/delete-kql-dashboard>`_. |
| 180 | +
|
| 181 | + Parameters |
| 182 | + ---------- |
| 183 | + kql_dashboard: str | uuid.UUID |
| 184 | + Name or ID of the KQL dashboard. |
| 185 | + workspace : str | uuid.UUID, default=None |
| 186 | + The Fabric workspace name or ID. |
| 187 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 188 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 189 | + """ |
| 190 | + |
| 191 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 192 | + item_id = fabric.resolve_item_id( |
| 193 | + item_name=kql_dashboard, type="KQLDashboard", workspace=workspace_id |
| 194 | + ) |
| 195 | + |
| 196 | + fabric.delete_item(item_id=item_id, workspace=workspace) |
| 197 | + print( |
| 198 | + f"{icons.green_dot} The '{kql_dashboard}' KQL database within the '{workspace_name}' workspace has been deleted." |
| 199 | + ) |
0 commit comments