Skip to content

Commit c1896e0

Browse files
committed
Merge branch 'm-kovalsky/jobinstancefix'
2 parents b232542 + 2714fdf commit c1896e0

File tree

2 files changed

+139
-4
lines changed

2 files changed

+139
-4
lines changed

src/sempy_labs/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from sempy_labs._job_scheduler import list_item_job_instances
1+
from sempy_labs._job_scheduler import (
2+
list_item_job_instances,
3+
list_item_schedules,
4+
)
25
from sempy_labs._gateways import (
36
list_gateway_members,
47
list_gateway_role_assigments,
@@ -470,4 +473,5 @@
470473
"bind_semantic_model_to_gateway",
471474
"list_semantic_model_errors",
472475
"list_item_job_instances",
476+
"list_item_schedules",
473477
]

src/sempy_labs/_job_scheduler.py

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
resolve_workspace_name_and_id,
66
resolve_item_name_and_id,
77
pagination,
8+
lro,
89
)
910
from sempy.fabric.exceptions import FabricHTTPException
1011
from uuid import UUID
12+
import sempy_labs._icons as icons
1113

1214

1315
def list_item_job_instances(
@@ -23,7 +25,7 @@ def list_item_job_instances(
2325
item : str | uuid.UUID
2426
The item name or ID
2527
type : str, default=None
26-
The item type. If specifying the item name as the item, the item type is required.
28+
The item `type <https://learn.microsoft.com/rest/api/fabric/core/items/list-items?tabs=HTTP#itemtype>`_. If specifying the item name as the item, the item type is required.
2729
workspace : str | uuid.UUID, default=None
2830
The Fabric workspace name or ID used by the lakehouse.
2931
Defaults to None which resolves to the workspace of the attached lakehouse
@@ -57,7 +59,8 @@ def list_item_job_instances(
5759
"Job Type",
5860
"Invoke Type",
5961
"Status",
60-
"Root Activity Id" "Start Time UTC",
62+
"Root Activity Id",
63+
"Start Time UTC",
6164
"End Time UTC",
6265
"Failure Reason",
6366
]
@@ -71,6 +74,7 @@ def list_item_job_instances(
7174
dfs = []
7275
for r in responses:
7376
for v in r.get("value", []):
77+
fail = v.get("failureReason", {})
7478
new_data = {
7579
"Job Instance Id": v.get("id"),
7680
"Item Name": item_name,
@@ -82,11 +86,138 @@ def list_item_job_instances(
8286
"Root Activity Id": v.get("rootActivityId"),
8387
"Start Time UTC": v.get("startTimeUtc"),
8488
"End Time UTC": v.get("endTimeUtc"),
85-
"Failure Reason": v.get("failureReason"),
89+
"Error Message": fail.get("message") if fail is not None else "",
8690
}
8791
dfs.append(pd.DataFrame(new_data, index=[0]))
8892

8993
if dfs:
9094
df = pd.concat(dfs, ignore_index=True)
9195

9296
return df
97+
98+
99+
def list_item_schedules(
100+
item: str | UUID,
101+
type: Optional[str] = None,
102+
job_type: str = "DefaultJob",
103+
workspace: Optional[str | UUID] = None,
104+
) -> pd.DataFrame:
105+
"""
106+
Get scheduling settings for one specific item.
107+
108+
This is a wrapper function for the following API: `Job Scheduler - List Item Schedules <https://learn.microsoft.com/rest/api/fabric/core/job-scheduler/list-item-schedules>`_.
109+
110+
Parameters
111+
----------
112+
item : str | uuid.UUID
113+
The item name or ID
114+
type : str, default=None
115+
The item `type <https://learn.microsoft.com/rest/api/fabric/core/items/list-items?tabs=HTTP#itemtype>`_. If specifying the item name as the item, the item type is required.
116+
job_type : str, default="DefaultJob"
117+
The job type.
118+
workspace : str | uuid.UUID, default=None
119+
The Fabric workspace name or ID used by the lakehouse.
120+
Defaults to None which resolves to the workspace of the attached lakehouse
121+
or if no lakehouse attached, resolves to the workspace of the notebook.
122+
123+
Returns
124+
-------
125+
pandas.DataFrame
126+
Shows a list of scheduling settings for one specific item.
127+
"""
128+
129+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
130+
(item_name, item_id) = resolve_item_name_and_id(
131+
item=item, type=type, workspace=workspace
132+
)
133+
134+
df = pd.DataFrame(
135+
columns=[
136+
"Job Schedule Id",
137+
"Enabled",
138+
"Created Date Time",
139+
"Start Date Time",
140+
"End Date Time",
141+
"Local Time Zone Id",
142+
"Type",
143+
"Interval",
144+
"Weekdays",
145+
"Times",
146+
"Owner Id",
147+
"Owner Type",
148+
]
149+
)
150+
151+
client = fabric.FabricRestClient()
152+
response = client.get(
153+
f"v1/workspaces/{workspace_id}/items/{item_id}/jobs/{job_type}/schedules"
154+
)
155+
156+
if response.status_code != 200:
157+
raise FabricHTTPException(response)
158+
159+
for v in response.json().get("value", []):
160+
config = v.get("configuration", {})
161+
own = v.get("owner", {})
162+
new_data = {
163+
"Job Schedule Id": v.get("id"),
164+
"Enabled": v.get("enabled"),
165+
"Created Date Time": v.get("createdDateTime"),
166+
"Start Date Time": config.get("startDateTime"),
167+
"End Date Time": config.get("endDateTime"),
168+
"Local Time Zone Id": config.get("localTimeZoneId"),
169+
"Type": config.get("type"),
170+
"Interval": config.get("interval"),
171+
"Weekdays": config.get("weekdays"),
172+
"Times": config.get("times"),
173+
"Owner Id": own.get("id"),
174+
"Owner Type": own.get("type"),
175+
}
176+
177+
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
178+
179+
df["Enabled"] = df["Enabled"].astype(bool)
180+
df["Created Date Time"] = pd.to_datetime(df["Created Date Time"])
181+
df["Start Date Time"] = pd.to_datetime(df["Start Date Time"])
182+
183+
return df
184+
185+
186+
def run_on_demand_item_job(
187+
item: str | UUID,
188+
type: Optional[str] = None,
189+
job_type: str = "DefaultJob",
190+
workspace: Optional[str | UUID] = None,
191+
):
192+
"""
193+
Run on-demand item job instance.
194+
195+
This is a wrapper function for the following API: `Job Scheduler - Run On Demand Item Job <https://learn.microsoft.com/rest/api/fabric/core/job-scheduler/run-on-demand-item-job>`_.
196+
197+
Parameters
198+
----------
199+
item : str | uuid.UUID
200+
The item name or ID
201+
type : str, default=None
202+
The item `type <https://learn.microsoft.com/rest/api/fabric/core/items/list-items?tabs=HTTP#itemtype>`_. If specifying the item name as the item, the item type is required.
203+
job_type : str, default="DefaultJob"
204+
The job type.
205+
workspace : str | uuid.UUID, default=None
206+
The Fabric workspace name or ID used by the lakehouse.
207+
Defaults to None which resolves to the workspace of the attached lakehouse
208+
or if no lakehouse attached, resolves to the workspace of the notebook.
209+
"""
210+
211+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
212+
(item_name, item_id) = resolve_item_name_and_id(
213+
item=item, type=type, workspace=workspace
214+
)
215+
216+
client = fabric.FabricRestClient()
217+
response = client.post(
218+
f"v1/workspaces/{workspace_id}/items/{item_id}/jobs/instances?jobType={job_type}"
219+
)
220+
221+
lro(client, response, return_status_code=True)
222+
223+
print(f"{icons.green_dot} The '{item_name}' {type.lower()} has been executed.")

0 commit comments

Comments
 (0)