diff --git a/tableauserverclient/models/task_item.py b/tableauserverclient/models/task_item.py index b343f3c22..bdd7cb390 100644 --- a/tableauserverclient/models/task_item.py +++ b/tableauserverclient/models/task_item.py @@ -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) @@ -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) diff --git a/tableauserverclient/server/endpoint/schedules_endpoint.py b/tableauserverclient/server/endpoint/schedules_endpoint.py index 321d0120b..c2fe34604 100644 --- a/tableauserverclient/server/endpoint/schedules_endpoint.py +++ b/tableauserverclient/server/endpoint/schedules_endpoint.py @@ -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: @@ -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, diff --git a/test/test_schedule.py b/test/test_schedule.py index 823a87607..5f1b09452 100644 --- a/test/test_schedule.py +++ b/test/test_schedule.py @@ -492,3 +492,78 @@ def test_batch_update_state_all(server: TSC.Server) -> None: history = m.request_history[0] assert history.text == "" + + +SUBSCRIPTIONS_XML = """ + + + + + + + + + + + + + + + +""" + +FLOW_TASKS_XML = """ + + + + + + + + + + + + + + + + + +""" + +FLOW_MISSING_XML = """ + + + Resource Not Found + Method not found + + +""" + + +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") == []