Skip to content

Commit f5038db

Browse files
committed
implement incremental refresh
add sample that runs 'refresh now'
1 parent e3f1e22 commit f5038db

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

samples/create_extract_task.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def main():
5353
monthly_interval,
5454
)
5555

56-
# Default to using first workbook found in server
56+
# Default to using first workbook found in server - note that this workbook may not be valid
5757
all_workbook_items, pagination_item = server.workbooks.get()
5858
my_workbook: TSC.WorkbookItem = all_workbook_items[0]
5959

@@ -62,9 +62,13 @@ def main():
6262
"workbook", # alternatively can be "datasource"
6363
)
6464

65-
extract_item = TSC.TaskItem(
65+
refresh_type = "FullRefresh"
66+
if args.incremental:
67+
refresh_type = "Incremental"
68+
69+
scheduled_extract_item = TSC.TaskItem(
6670
None,
67-
"FullRefresh",
71+
refresh_type,
6872
None,
6973
None,
7074
None,
@@ -74,11 +78,14 @@ def main():
7478
)
7579

7680
try:
77-
response = server.tasks.create(extract_item)
81+
response = server.tasks.create(scheduled_extract_item)
7882
print(response)
7983
except Exception as e:
8084
print(e)
8185

8286

87+
88+
89+
8390
if __name__ == "__main__":
8491
main()

samples/extracts.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ def main():
2525
help="desired logging level (set to error by default)",
2626
)
2727
# Options specific to this sample
28-
parser.add_argument("--delete")
29-
parser.add_argument("--create")
28+
parser.add_argument("--create", action="store_true")
29+
parser.add_argument("--delete", action="store_true")
30+
parser.add_argument("--refresh", action="store_true")
31+
parser.add_argument("--workbook", required=False)
32+
parser.add_argument("--datasource", required=False)
3033
args = parser.parse_args()
3134

3235
# Set logging level based on user input, or error by default
@@ -39,19 +42,45 @@ def main():
3942
server.add_http_options({"verify": False})
4043
server.use_server_version()
4144
with server.auth.sign_in(tableau_auth):
42-
# Gets all workbook items
43-
all_workbooks, pagination_item = server.workbooks.get()
44-
print(f"\nThere are {pagination_item.total_available} workbooks on site: ")
45-
print([workbook.name for workbook in all_workbooks])
4645

47-
if all_workbooks:
48-
# Pick one workbook from the list
49-
wb = all_workbooks[3]
46+
wb = None
47+
ds = None
48+
if args.workbook:
49+
wb = server.workbooks.get_by_id(args.workbook)
50+
if wb is None:
51+
raise ValueError(f"Workbook not found for id {args.workbook}")
52+
elif args.datasource:
53+
ds = server.datasources.get_by_id(args.datasource)
54+
if ds is None:
55+
raise ValueError(f"Datasource not found for id {args.datasource}")
56+
else:
57+
# Gets all workbook items
58+
all_workbooks, pagination_item = server.workbooks.get()
59+
print(f"\nThere are {pagination_item.total_available} workbooks on site: ")
60+
print([workbook.name for workbook in all_workbooks])
61+
62+
if all_workbooks:
63+
# Pick one workbook from the list
64+
wb = all_workbooks[3]
5065

5166
if args.create:
5267
print("create extract on wb ", wb.name)
5368
extract_job = server.workbooks.create_extract(wb, includeAll=True)
5469
print(extract_job)
70+
71+
if args.refresh:
72+
extract_job = None
73+
if ds is not None:
74+
print(f"refresh extract on datasource {ds.name}")
75+
extract_job = server.datasources.refresh(ds, includeAll=True, incremental=True)
76+
elif wb is not None:
77+
print(f"refresh extract on workbook {wb.name}")
78+
extract_job = server.workbooks.refresh(wb)
79+
else:
80+
print("no content item selected to refresh")
81+
82+
print(extract_job)
83+
5584

5685
if args.delete:
5786
print("delete extract on wb ", wb.name)

samples/publish_workbook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def main():
5555

5656
# Step 1: Sign in to server.
5757
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
58-
server = TSC.Server(args.server, use_server_version=True)
58+
server = TSC.Server(args.server, use_server_version=True, http_options={'verify': False})
5959
with server.auth.sign_in(tableau_auth):
6060
# Step2: Retrieve the project id, if a project name was passed
6161
if args.project is not None:

tableauserverclient/server/endpoint/datasources_endpoint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ def update_connection(
187187
return connection
188188

189189
@api(version="2.8")
190-
def refresh(self, datasource_item: DatasourceItem) -> JobItem:
190+
def refresh(self, datasource_item: DatasourceItem, incremental: bool=False) -> JobItem:
191191
id_ = getattr(datasource_item, "id", datasource_item)
192192
url = f"{self.baseurl}/{id_}/refresh"
193-
empty_req = RequestFactory.Empty.empty_req()
194-
server_response = self.post_request(url, empty_req)
193+
refresh_req = RequestFactory.Task.refresh_req(incremental)
194+
server_response = self.post_request(url, refresh_req)
195195
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]
196196
return new_job
197197

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,16 @@ def get_by_id(self, workbook_id: str) -> WorkbookItem:
118118
return WorkbookItem.from_response(server_response.content, self.parent_srv.namespace)[0]
119119

120120
@api(version="2.8")
121-
def refresh(self, workbook_item: Union[WorkbookItem, str]) -> JobItem:
121+
def refresh(self, workbook_item: Union[WorkbookItem, str], incremental: bool = False) -> JobItem:
122122
"""
123123
Refreshes the extract of an existing workbook.
124124
125125
Parameters
126126
----------
127127
workbook_item : WorkbookItem | str
128128
The workbook item or workbook ID.
129+
incremental: bool
130+
Whether to do a full refresh or incremental refresh of the extract data
129131
130132
Returns
131133
-------
@@ -134,8 +136,8 @@ def refresh(self, workbook_item: Union[WorkbookItem, str]) -> JobItem:
134136
"""
135137
id_ = getattr(workbook_item, "id", workbook_item)
136138
url = f"{self.baseurl}/{id_}/refresh"
137-
empty_req = RequestFactory.Empty.empty_req()
138-
server_response = self.post_request(url, empty_req)
139+
refresh_req = RequestFactory.Task.refresh_req(incremental)
140+
server_response = self.post_request(url, refresh_req)
139141
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]
140142
return new_job
141143

tableauserverclient/server/request_factory.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,13 @@ def run_req(self, xml_request: ET.Element, task_item: Any) -> None:
11171117
# Send an empty tsRequest
11181118
pass
11191119

1120+
@_tsrequest_wrapped
1121+
def refresh_req(self, xml_request: ET.Element, incremental: bool = False) -> bytes:
1122+
task_element = ET.SubElement(xml_request, 'extractRefresh')
1123+
if incremental:
1124+
task_element.attrib['incremental'] = "true"
1125+
return ET.tostring(xml_request)
1126+
11201127
@_tsrequest_wrapped
11211128
def create_extract_req(self, xml_request: ET.Element, extract_item: "TaskItem") -> bytes:
11221129
extract_element = ET.SubElement(xml_request, "extractRefresh")

0 commit comments

Comments
 (0)