This repository was archived by the owner on Nov 2, 2020. It is now read-only.
forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterialize_workbooks.py
More file actions
342 lines (276 loc) · 15.1 KB
/
materialize_workbooks.py
File metadata and controls
342 lines (276 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import argparse
import getpass
import logging
import os
import tableauserverclient as TSC
from collections import defaultdict
def main():
parser = argparse.ArgumentParser(description='Materialized views settings for sites/workbooks.')
parser.add_argument('--server', '-s', required=True, help='Tableau server address')
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
parser.add_argument('--password', '-p', required=False, help='password to sign into server')
parser.add_argument('--mode', '-m', required=False, choices=['disable', 'enable', 'enable_all', 'enable_selective'],
help='enable/disable materialized views for sites/workbooks')
parser.add_argument('--status', '-st', required=False, action='store_true',
help='show materialized views enabled sites/workbooks')
parser.add_argument('--site-id', '-si', required=False,
help='set to Default site by default')
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
help='desired logging level (set to error by default)')
parser.add_argument('--type', '-t', required=False, choices=['site', 'workbook', 'project_name', 'project_path'],
help='type of content you want to update materialized views settings on')
parser.add_argument('--path-list', '-pl', required=False, help='path to a list of workbook paths')
parser.add_argument('--name-list', '-nl', required=False, help='path to a list of workbook names')
parser.add_argument('--project-name', '-pn', required=False, help='name of the project')
parser.add_argument('--project-path', '-pp', required=False, help="path of the project")
parser.add_argument('--materialize-now', '-mn', required=False, action='store_true',
help='create materialized views for workbooks immediately')
args = parser.parse_args()
if args.password:
password = args.password
else:
password = getpass.getpass("Password: ")
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# site content url is the TSC term for site id
site_content_url = args.site_id if args.site_id is not None else ""
if not assert_options_valid(args):
return
materialized_views_config = create_materialized_views_config(args)
# enable/disable materialized views for site
if args.type == 'site':
if not update_site(args, password, site_content_url):
return
# enable/disable materialized views for workbook
# works only when the site the workbooks belong to are enabled too
elif args.type == 'workbook':
if not update_workbook(args, materialized_views_config, password, site_content_url):
return
# enable/disable materialized views for project by project name
# will show possible projects when project name is not unique
elif args.type == 'project_name':
if not update_project_by_name(args, materialized_views_config, password, site_content_url):
return
# enable/disable materialized views for project by project path, for example: project1/project2
elif args.type == 'project_path':
if not update_project_by_path(args, materialized_views_config, password, site_content_url):
return
# show enabled sites and workbooks
if args.status:
show_materialized_views_status(args, password, site_content_url)
def find_project_path(project, all_projects, path):
# project stores the id of it's parent
# this method is to run recursively to find the path from root project to given project
path = project.name if len(path) == 0 else project.name + '/' + path
if project.parent_id is None:
return path
else:
return find_project_path(all_projects[project.parent_id], all_projects, path)
def get_project_paths(server, projects):
# most likely user won't have too many projects so we store them in a dict to search
all_projects = {project.id: project for project in TSC.Pager(server.projects)}
result = dict()
for project in projects:
result[find_project_path(project, all_projects, "")] = project
return result
def print_paths(paths):
for path in paths.keys():
print(path)
def show_materialized_views_status(args, password, site_content_url):
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
server = TSC.Server(args.server, use_server_version=True)
enabled_sites = set()
with server.auth.sign_in(tableau_auth):
# For server admin, this will prints all the materialized views enabled sites
# For other users, this only prints the status of the site they belong to
print("Materialized views is enabled on sites:")
# only server admins can get all the sites in the server
# other users can only get the site they are in
for site in TSC.Pager(server.sites):
if site.materialized_views_mode != "disable":
enabled_sites.add(site)
print("Site name: {}".format(site.name))
print('\n')
print("Materialized views is enabled on workbooks:")
# Individual workbooks can be enabled only when the sites they belong to are enabled too
for site in enabled_sites:
site_auth = TSC.TableauAuth(args.username, password, site.content_url)
with server.auth.sign_in(site_auth):
for workbook in TSC.Pager(server.workbooks):
if workbook.materialized_views_config['materialized_views_enabled']:
print("Workbook: {} from site: {}".format(workbook.name, site.name))
def update_project_by_path(args, materialized_views_config, password, site_content_url):
if args.project_path is None:
print("Use --project_path <project path> to specify the path of the project")
return False
tableau_auth = TSC.TableauAuth(args.username, password, site_content_url)
server = TSC.Server(args.server, use_server_version=True)
project_name = args.project_path.split('/')[-1]
with server.auth.sign_in(tableau_auth):
if not assert_site_enabled_for_materialized_views(server, site_content_url):
return False
projects = [project for project in TSC.Pager(server.projects) if project.name == project_name]
if not assert_project_valid(args.project_path, projects):
return False
possible_paths = get_project_paths(server, projects)
update_project(possible_paths[args.project_path], server, materialized_views_config)
return True
def update_project_by_name(args, materialized_views_config, password, site_content_url):
if args.project_name is None:
print("Use --project-name <project name> to specify the name of the project")
return False
tableau_auth = TSC.TableauAuth(args.username, password, site_content_url)
server = TSC.Server(args.server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
if not assert_site_enabled_for_materialized_views(server, site_content_url):
return False
# get all projects with given name
projects = [project for project in TSC.Pager(server.projects) if project.name == args.project_name]
if not assert_project_valid(args.project_name, projects):
return False
if len(projects) > 1:
possible_paths = get_project_paths(server, projects)
print("Project name is not unique, use '--project_path <path>'")
print("Possible project paths:")
print_paths(possible_paths)
print('\n')
return False
else:
update_project(projects[0], server, materialized_views_config)
return True
def update_project(project, server, materialized_views_config):
all_projects = list(TSC.Pager(server.projects))
project_ids = find_project_ids_to_update(all_projects, project)
for workbook in TSC.Pager(server.workbooks):
if workbook.project_id in project_ids:
workbook.materialized_views_config = materialized_views_config
server.workbooks.update(workbook)
print("Updated materialized views settings for project: {}".format(project.name))
print('\n')
def find_project_ids_to_update(all_projects, project):
projects_to_update = []
find_projects_to_update(project, all_projects, projects_to_update)
return set([project_to_update.id for project_to_update in projects_to_update])
def parse_workbook_path(file_path):
# parse the list of project path of workbooks
workbook_paths = sanitize_workbook_list(file_path, "path")
workbook_path_mapping = defaultdict(list)
for workbook_path in workbook_paths:
workbook_project = workbook_path.rstrip().split('/')
workbook_path_mapping[workbook_project[-1]].append('/'.join(workbook_project[:-1]))
return workbook_path_mapping
def update_workbook(args, materialized_views_config, password, site_content_url):
if args.path_list is None and args.name_list is None:
print("Use '--path-list <filename>' or '--name-list <filename>' to specify the path of a list of workbooks")
print('\n')
return False
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
server = TSC.Server(args.server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
if not assert_site_enabled_for_materialized_views(server, site_content_url):
return False
if args.path_list is not None:
workbook_path_mapping = parse_workbook_path(args.path_list)
all_projects = {project.id: project for project in TSC.Pager(server.projects)}
update_workbooks_by_paths(all_projects, materialized_views_config, server, workbook_path_mapping)
elif args.name_list is not None:
update_workbooks_by_names(args.name_list, server, materialized_views_config)
return True
def update_workbooks_by_paths(all_projects, materialized_views_config, server, workbook_path_mapping):
for workbook_name, workbook_paths in workbook_path_mapping.items():
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals,
workbook_name))
workbooks = list(TSC.Pager(server.workbooks, req_option))
all_paths = set(workbook_paths[:])
for workbook in workbooks:
path = find_project_path(all_projects[workbook.project_id], all_projects, "")
if path in workbook_paths:
all_paths.remove(path)
workbook.materialized_views_config = materialized_views_config
server.workbooks.update(workbook)
print("Updated materialized views settings for workbook: {}".format(path + '/' + workbook.name))
for path in all_paths:
print("Cannot find workbook path: {}, each line should only contain one workbook path"
.format(path + '/' + workbook_name))
print('\n')
def update_workbooks_by_names(name_list, server, materialized_views_config):
workbook_names = sanitize_workbook_list(name_list, "name")
for workbook_name in workbook_names:
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals,
workbook_name.rstrip()))
workbooks = list(TSC.Pager(server.workbooks, req_option))
if len(workbooks) == 0:
print("Cannot find workbook name: {}, each line should only contain one workbook name"
.format(workbook_name))
for workbook in workbooks:
workbook.materialized_views_config = materialized_views_config
server.workbooks.update(workbook)
print("Updated materialized views settings for workbook: {}".format(workbook.name))
print('\n')
def update_site(args, password, site_content_url):
if not assert_site_options_valid(args):
return False
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
server = TSC.Server(args.server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
site_to_update = server.sites.get_by_content_url(site_content_url)
site_to_update.materialized_views_mode = args.mode
server.sites.update(site_to_update)
print("Updated materialized views settings for site: {}".format(site_to_update.name))
print('\n')
return True
def create_materialized_views_config(args):
materialized_views_config = dict()
materialized_views_config['materialized_views_enabled'] = args.mode == "enable"
materialized_views_config['run_materialization_now'] = True if args.materialize_now else False
return materialized_views_config
def assert_site_options_valid(args):
if args.materialize_now:
print('"--materialize-now" only applies to workbook/project type')
return False
if args.mode == 'enable':
print('For site type please choose from "disable", "enable_all", or "enable_selective"')
return False
return True
def assert_options_valid(args):
if args.type != "site" and args.mode in ("enable_all", "enable_selective"):
print('"enable_all" and "enable_selective" do not apply to workbook/project type')
return False
if (args.type is None) != (args.mode is None):
print("Use '--type <content type> --mode <mode>' to update materialized views settings.")
return False
return True
def assert_site_enabled_for_materialized_views(server, site_content_url):
parent_site = server.sites.get_by_content_url(site_content_url)
if parent_site.materialized_views_mode == "disable":
print('Cannot update workbook/project because site is disabled for materialized views')
return False
return True
def assert_project_valid(project_name, projects):
if len(projects) == 0:
print("Cannot find project: {}".format(project_name))
return False
return True
def find_projects_to_update(project, all_projects, projects_to_update):
# Use recursion to find all the sub-projects and enable/disable the workbooks in them
projects_to_update.append(project)
children_projects = [child for child in all_projects if child.parent_id == project.id]
if len(children_projects) == 0:
return
for child in children_projects:
find_projects_to_update(child, all_projects, projects_to_update)
def sanitize_workbook_list(file_name, file_type):
if not os.path.isfile(file_name):
print("Invalid file name '{}'".format(file_name))
return []
file_list = open(file_name, "r")
if file_type == "name":
return [workbook.rstrip() for workbook in file_list if not workbook.isspace()]
if file_type == "path":
return [workbook.rstrip() for workbook in file_list if not workbook.isspace()]
if __name__ == "__main__":
main()