|
| 1 | +import pandas as pd |
| 2 | +from uuid import UUID |
| 3 | +from typing import Optional |
| 4 | +from sempy._utils._log import log |
| 5 | +from sempy_labs._helper_functions import ( |
| 6 | + _base_api, |
| 7 | + _create_dataframe, |
| 8 | + resolve_item_id, |
| 9 | + resolve_workspace_id, |
| 10 | + resolve_item_name_and_id, |
| 11 | + resolve_workspace_name_and_id, |
| 12 | +) |
| 13 | +import sempy_labs._icons as icons |
| 14 | + |
| 15 | + |
| 16 | +@log |
| 17 | +def list_graph_models(workspace: Optional[str | UUID] = None) -> pd.DataFrame: |
| 18 | + """ |
| 19 | + Shows the graph models within a workspace. |
| 20 | +
|
| 21 | + This is a wrapper function for the following API: `Items - List Graph Models <https://learn.microsoft.com/rest/api/fabric/graphmodel/items/list-graph-models>`_. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + workspace : str | uuid.UUID, default=None |
| 26 | + The Fabric workspace name or ID. |
| 27 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 28 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + pandas.DataFrame |
| 33 | + A pandas dataframe showing the graph models within a workspace. |
| 34 | + """ |
| 35 | + |
| 36 | + columns = { |
| 37 | + "Graph Model Name": "string", |
| 38 | + "Graph Model Id": "string", |
| 39 | + "Description": "string", |
| 40 | + "OneLake Root Path": "string", |
| 41 | + } |
| 42 | + df = _create_dataframe(columns=columns) |
| 43 | + |
| 44 | + workspace_id = resolve_workspace_id(workspace) |
| 45 | + |
| 46 | + responses = _base_api( |
| 47 | + request=f"/v1/workspaces/{workspace_id}/GraphModels", |
| 48 | + uses_pagination=True, |
| 49 | + ) |
| 50 | + |
| 51 | + rows = [] |
| 52 | + for r in responses: |
| 53 | + for v in r.get("value", []): |
| 54 | + rows.append( |
| 55 | + { |
| 56 | + "Graph Model Name": v.get("displayName"), |
| 57 | + "Graph Model Id": v.get("id"), |
| 58 | + "Description": v.get("description"), |
| 59 | + "OneLake Root Path": v.get("properties", {}).get("oneLakeRootPath"), |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + if rows: |
| 64 | + df = pd.DataFrame(rows, columns=list(columns.keys())) |
| 65 | + |
| 66 | + return df |
| 67 | + |
| 68 | + |
| 69 | +@log |
| 70 | +def execute_query( |
| 71 | + graph_model: str | UUID, query: str, workspace: Optional[str | UUID] = None |
| 72 | +) -> dict: |
| 73 | + """ |
| 74 | + Executes a query on the specified graph model. |
| 75 | +
|
| 76 | + This is a wrapper function for the following API: `Items - ExecuteQuery <https://learn.microsoft.com/rest/api/fabric/graphmodel/items/execute-query(preview)>`_. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + graph_model : str | uuid.UUID |
| 81 | + The graph model name or ID. |
| 82 | + query : str |
| 83 | + The query string. |
| 84 | + workspace : str | uuid.UUID, default=None |
| 85 | + The Fabric workspace name or ID. |
| 86 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 87 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 88 | +
|
| 89 | + Returns |
| 90 | + ------- |
| 91 | + dict |
| 92 | + The response from the API. |
| 93 | + """ |
| 94 | + |
| 95 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 96 | + (item_name, item_id) = resolve_item_name_and_id( |
| 97 | + item=graph_model, type="GraphModel", workspace=workspace_id |
| 98 | + ) |
| 99 | + |
| 100 | + payload = { |
| 101 | + "query": query, |
| 102 | + } |
| 103 | + response = _base_api( |
| 104 | + request=f"/v1/workspaces/{workspace_id}/GraphModels/{item_id}/executeQuery?preview=True", |
| 105 | + method="post", |
| 106 | + payload=payload, |
| 107 | + ) |
| 108 | + |
| 109 | + print( |
| 110 | + f"{icons.green_dot} Executed query on Graph Model '{item_name}' in workspace '{workspace_name}' successfully." |
| 111 | + ) |
| 112 | + |
| 113 | + return response.json() |
| 114 | + |
| 115 | + |
| 116 | +@log |
| 117 | +def get_queryable_graph_type( |
| 118 | + graph_model: str | UUID, workspace: Optional[str | UUID] = None |
| 119 | +) -> dict: |
| 120 | + """ |
| 121 | + Gets the current queryable graph type. |
| 122 | +
|
| 123 | + This is a wrapper function for the following API: `Items - GetQueryableGraphType <https://learn.microsoft.com/rest/api/fabric/graphmodel/items/get-queryable-graph-type(preview)>`_. |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + graph_model : str | uuid.UUID |
| 128 | + The graph model name or ID. |
| 129 | + workspace : str | uuid.UUID, default=None |
| 130 | + The Fabric workspace name or ID. |
| 131 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 132 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 133 | +
|
| 134 | + Returns |
| 135 | + ------- |
| 136 | + dict |
| 137 | + A dictionary showing the current queryable graph type. |
| 138 | + """ |
| 139 | + |
| 140 | + workspace_id = resolve_workspace_id(workspace) |
| 141 | + item_id = resolve_item_id( |
| 142 | + item=graph_model, type="GraphModel", workspace=workspace_id |
| 143 | + ) |
| 144 | + |
| 145 | + response = _base_api( |
| 146 | + request=f"/v1/workspaces/{workspace_id}/GraphModels/{item_id}/getQueryableGraphType?preview=True" |
| 147 | + ) |
| 148 | + |
| 149 | + return response.json() |
0 commit comments