Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

job instance - root act id #374

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/sempy_labs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from sempy_labs._job_scheduler import list_item_job_instances
from sempy_labs._job_scheduler import (
list_item_job_instances,
list_item_schedules,
)
from sempy_labs._gateways import (
list_gateway_members,
list_gateway_role_assigments,
Expand Down Expand Up @@ -470,4 +473,5 @@
"bind_semantic_model_to_gateway",
"list_semantic_model_errors",
"list_item_job_instances",
"list_item_schedules",
]
135 changes: 132 additions & 3 deletions src/sempy_labs/_job_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
resolve_workspace_name_and_id,
resolve_item_name_and_id,
pagination,
lro,
)
from sempy.fabric.exceptions import FabricHTTPException
from uuid import UUID
import sempy_labs._icons as icons


def list_item_job_instances(
Expand All @@ -23,7 +25,7 @@ def list_item_job_instances(
item : str | uuid.UUID
The item name or ID
type : str, default=None
The item type. If specifying the item name as the item, the item type is required.
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.
workspace : str | uuid.UUID, default=None
The Fabric workspace name or ID used by the lakehouse.
Defaults to None which resolves to the workspace of the attached lakehouse
Expand Down Expand Up @@ -57,7 +59,8 @@ def list_item_job_instances(
"Job Type",
"Invoke Type",
"Status",
"Root Activity Id" "Start Time UTC",
"Root Activity Id",
"Start Time UTC",
"End Time UTC",
"Failure Reason",
]
Expand All @@ -71,6 +74,7 @@ def list_item_job_instances(
dfs = []
for r in responses:
for v in r.get("value", []):
fail = v.get("failureReason", {})
new_data = {
"Job Instance Id": v.get("id"),
"Item Name": item_name,
Expand All @@ -82,11 +86,136 @@ def list_item_job_instances(
"Root Activity Id": v.get("rootActivityId"),
"Start Time UTC": v.get("startTimeUtc"),
"End Time UTC": v.get("endTimeUtc"),
"Failure Reason": v.get("failureReason"),
"Error Message": fail.get("message") if fail is not None else "",
}
dfs.append(pd.DataFrame(new_data, index=[0]))

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

return df


def list_item_schedules(
item: str | UUID,
type: Optional[str] = None,
job_type: str = "DefaultJob",
workspace: Optional[str | UUID] = None,
) -> pd.DataFrame:
"""
Get scheduling settings for one specific item.

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>`_.

Parameters
----------
item : str | uuid.UUID
The item name or ID
type : str, default=None
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.
job_type : str, default="DefaultJob"
The job type.
workspace : str | uuid.UUID, default=None
The Fabric workspace name or ID used by the lakehouse.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.

Returns
-------
pandas.DataFrame
Shows a list of scheduling settings for one specific item.
"""

(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
(item_name, item_id) = resolve_item_name_and_id(
item=item, type=type, workspace=workspace
)

df = pd.DataFrame(
columns=[
"Job Schedule Id",
"Enabled",
"Created Date Time",
"Start Date Time",
"End Date Time",
"Local Time Zone Id",
"Type",
"Interval",
"Weekdays",
"Times",
"Owner Id",
"Owner Type",
]
)

client = fabric.FabricRestClient()
response = client.get(
f"v1/workspaces/{workspace_id}/items/{item_id}/jobs/{job_type}/schedules"
)

if response.status_code != 200:
raise FabricHTTPException(response)

for v in response.json().get("value", []):
config = v.get("configuration", {})
own = v.get("owner", {})
new_data = {
"Job Schedule Id": v.get("id"),
"Enabled": v.get("enabled"),
"Created Date Time": v.get("createdDateTime"),
"Start Date Time": config.get("startDateTime"),
"End Date Time": config.get("endDateTime"),
"Local Time Zone Id": config.get("localTimeZoneId"),
"Type": config.get("type"),
"Interval": config.get("interval"),
"Weekdays": config.get("weekdays"),
"Times": config.get("times"),
"Owner Id": own.get("id"),
"Owner Type": own.get("type"),
}

df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)

df["Enabled"] = df["Enabled"].astype(bool)

return df


def run_on_demand_item_job(
item: str | UUID,
type: Optional[str] = None,
job_type: str = "DefaultJob",
workspace: Optional[str | UUID] = None,
):
"""
Run on-demand item job instance.

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>`_.

Parameters
----------
item : str | uuid.UUID
The item name or ID
type : str, default=None
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.
job_type : str, default="DefaultJob"
The job type.
workspace : str | uuid.UUID, default=None
The Fabric workspace name or ID used by the lakehouse.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
"""

(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
(item_name, item_id) = resolve_item_name_and_id(
item=item, type=type, workspace=workspace
)

client = fabric.FabricRestClient()
response = client.post(
f"v1/workspaces/{workspace_id}/items/{item_id}/jobs/instances?jobType={job_type}"
)

lro(client, response, return_status_code=True)

print(f"{icons.green_dot} The '{item_name}' {type.lower()} has been executed.")
Loading