Skip to content
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
3 changes: 3 additions & 0 deletions tableauserverclient/models/task_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _parse_element(cls, element, ns):
last_run_at = None
workbook_element = element.find(".//t:workbook", namespaces=ns)
datasource_element = element.find(".//t:datasource", namespaces=ns)
flow_element = element.find(".//t:flow", namespaces=ns)
last_run_at_element = element.find(".//t:lastRunAt", namespaces=ns)

schedule_item_list = ScheduleItem.from_element(element, ns)
Expand All @@ -105,6 +106,8 @@ def _parse_element(cls, element, ns):
if datasource_element is not None:
datasource_id = datasource_element.get("id", None)
target = Target(datasource_id, "datasource")
if flow_element is not None:
target = Target(flow_element.get("id", None), "flow")
if last_run_at_element is not None:
last_run_at = parse_datetime(last_run_at_element.text)

Expand Down
81 changes: 79 additions & 2 deletions tableauserverclient/server/endpoint/schedules_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
from typing import TYPE_CHECKING, Any, Callable, Literal, overload

from .endpoint import Endpoint, api, parameter_added_in
from .exceptions import MissingRequiredFieldError
from .exceptions import MissingRequiredFieldError, ServerResponseError
from tableauserverclient.server import RequestFactory
from tableauserverclient.models import PaginationItem, ScheduleItem, TaskItem, ExtractItem
from tableauserverclient.server.pager import Pager
from tableauserverclient.server.request_options import RequestOptions
from tableauserverclient.models import ExtractItem, PaginationItem, ScheduleItem, SubscriptionItem, TaskItem
from tableauserverclient.models.schedule_item import parse_batch_schedule_state

from tableauserverclient.helpers.logging import logger

AddResponse = namedtuple("AddResponse", ("result", "error", "warnings", "task_created"))
ScheduleTasks = namedtuple("ScheduleTasks", ("extracts", "subscriptions", "flow_tasks"))
OK = AddResponse(result=True, error=None, warnings=None, task_created=None)

if TYPE_CHECKING:
Expand Down Expand Up @@ -282,6 +285,80 @@ def get_extract_refresh_tasks(

return extract_items, pagination_item

def _collect_pages(self, fetch: Callable[..., tuple[list, PaginationItem]]) -> list:
options = RequestOptions(pagesize=1000)
collected: list = []
while True:
batch, pagination = fetch(options)
collected.extend(batch)
if (
not batch
or pagination.total_available is None
or pagination.page_number is None
or pagination.page_size is None
or pagination.page_size < 1
or pagination.page_number < 1
or pagination.page_size * pagination.page_number >= pagination.total_available
):
return collected
options.pagenumber = pagination.page_number + 1
options.pagesize = pagination.page_size

@api(version="3.2")
def get_subscriptions_by_schedule(self, schedule_id: str) -> list[SubscriptionItem]:
"""
Return the Tableau Server subscriptions on this schedule.

The REST API has no ``/schedules/{id}/subscriptions`` method. Subscriptions
are listed for the site, and each Tableau Server subscription carries the
schedule id. Tableau Cloud subscriptions use a frequency instead of a
server schedule id, so they are not included.
"""
if not schedule_id:
raise ValueError("Schedule ID undefined")
matches = []
for subscription in Pager(self.parent_srv.subscriptions):
if subscription.schedule_id == schedule_id:
matches.append(subscription)
return matches

@api(version="3.3")
def get_flow_tasks_by_schedule(self, schedule_id: str) -> list[TaskItem]:
"""
Return flow run tasks on this server schedule.

Flow tasks are listed for the site and kept when their schedule id
matches. A server that does not have the flow-task method yields an
empty list.
"""
if not schedule_id:
raise ValueError("Schedule ID undefined")
try:
tasks = list(Pager(self.parent_srv.tasks.get, task_type=TaskItem.Type.RunFlow))
except ServerResponseError as error:
if str(error.code).startswith("404"):
logger.info("Flow tasks are not available on this server: %s", error.code)
return []
raise
return [task for task in tasks if task.schedule_id == schedule_id]

@api(version="2.3")
def get_tasks(self, schedule_id: str):
"""
List the extract refreshes, subscriptions, and flow tasks on a schedule.

Extract refreshes come from ``GET .../schedules/{id}/extracts``.
Subscriptions and flow tasks are filtered from the site lists, because
those task types have no schedule-scoped query. Tableau Cloud
subscriptions, which have no server schedule id, are left out.
"""
if not schedule_id:
raise ValueError("Schedule ID undefined")
extracts = self._collect_pages(lambda options: self.get_extract_refresh_tasks(schedule_id, options))
subscriptions = self.get_subscriptions_by_schedule(schedule_id)
flow_tasks = self.get_flow_tasks_by_schedule(schedule_id)
return ScheduleTasks(extracts, subscriptions, flow_tasks)

@overload
def batch_update_state(
self,
Expand Down
75 changes: 75 additions & 0 deletions test/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,78 @@ def test_batch_update_state_all(server: TSC.Server) -> None:
history = m.request_history[0]

assert history.text == "<tsRequest />"


SUBSCRIPTIONS_XML = """<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api">
<pagination pageNumber="1" pageSize="100" totalAvailable="2"/>
<subscriptions>
<subscription id="sub-on-schedule" subject="On the schedule">
<content id="view-1" type="View"/>
<schedule id="sched-1" name="Nightly"/>
<user id="user-1" name="ada"/>
</subscription>
<subscription id="sub-cloud" subject="Cloud frequency">
<content id="view-2" type="View"/>
<schedule frequency="Weekly" nextRunAt="2023-06-08T17:05:00-0700"/>
<user id="user-1" name="ada"/>
</subscription>
</subscriptions>
</tsResponse>
"""

FLOW_TASKS_XML = """<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api">
<pagination pageNumber="1" pageSize="100" totalAvailable="2"/>
<tasks>
<task>
<runFlow id="flow-task-1" type="RunFlowTask" priority="50">
<schedule id="sched-1" name="Nightly" type="Flow"/>
<flow id="flow-1"/>
</runFlow>
</task>
<task>
<runFlow id="flow-task-2" type="RunFlowTask" priority="40">
<schedule id="sched-other" name="Weekly" type="Flow"/>
<flow id="flow-2"/>
</runFlow>
</task>
</tasks>
</tsResponse>
"""

FLOW_MISSING_XML = """<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api">
<error code="404001">
<summary>Resource Not Found</summary>
<detail>Method not found</detail>
</error>
</tsResponse>
"""


def test_get_tasks_on_schedule(server: TSC.Server) -> None:
server.version = "3.10"
schedule_id = "sched-1"
with requests_mock.mock() as m:
m.get(f"{server.schedules.siteurl}/{schedule_id}/extracts", text=GET_EXTRACT_TASKS_XML.read_text())
m.get(f"{server.baseurl}/sites/{server.site_id}/subscriptions", text=SUBSCRIPTIONS_XML)
m.get(f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", text=FLOW_TASKS_XML)
tasks = server.schedules.get_tasks(schedule_id)

assert [item.id for item in tasks.extracts] == ["task1", "task2"]
assert [item.id for item in tasks.subscriptions] == ["sub-on-schedule"]
assert [item.id for item in tasks.flow_tasks] == ["flow-task-1"]
assert tasks.flow_tasks[0].target.id == "flow-1"
assert tasks.flow_tasks[0].target.type == "flow"


def test_flow_tasks_missing_on_older_server(server: TSC.Server) -> None:
server.version = "3.10"
with requests_mock.mock() as m:
m.get(
f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow",
status_code=404,
text=FLOW_MISSING_XML,
)
assert server.schedules.get_flow_tasks_by_schedule("sched-1") == []