|
| 1 | +#### |
| 2 | +# This script demonstrates how to update workbook data freshness policy using the Tableau |
| 3 | +# Server Client. |
| 4 | +# |
| 5 | +# To run the script, you must have installed Python 3.7 or later. |
| 6 | +#### |
| 7 | + |
| 8 | + |
| 9 | +import argparse |
| 10 | +import logging |
| 11 | + |
| 12 | +import tableauserverclient as TSC |
| 13 | +from tableauserverclient import IntervalItem |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + parser = argparse.ArgumentParser(description="Creates sample schedules for each type of frequency.") |
| 18 | + # Common options; please keep those in sync across all samples |
| 19 | + parser.add_argument("--server", "-s", help="server address") |
| 20 | + parser.add_argument("--site", "-S", help="site name") |
| 21 | + parser.add_argument("--token-name", "-p", help="name of the personal access token " "used to sign into the server") |
| 22 | + parser.add_argument( |
| 23 | + "--token-value", "-v", help="value of the personal access token " "used to sign into the server" |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + "--logging-level", |
| 27 | + "-l", |
| 28 | + choices=["debug", "info", "error"], |
| 29 | + default="error", |
| 30 | + help="desired logging level (set to error by default)", |
| 31 | + ) |
| 32 | + # Options specific to this sample: |
| 33 | + # This sample has no additional options, yet. If you add some, please add them here |
| 34 | + |
| 35 | + args = parser.parse_args() |
| 36 | + |
| 37 | + # Set logging level based on user input, or error by default |
| 38 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 39 | + logging.basicConfig(level=logging_level) |
| 40 | + |
| 41 | + tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) |
| 42 | + server = TSC.Server(args.server, use_server_version=False) |
| 43 | + server.add_http_options({"verify": False}) |
| 44 | + server.use_server_version() |
| 45 | + with server.auth.sign_in(tableau_auth): |
| 46 | + # Get workbook |
| 47 | + all_workbooks, pagination_item = server.workbooks.get() |
| 48 | + print("\nThere are {} workbooks on site: ".format(pagination_item.total_available)) |
| 49 | + print([workbook.name for workbook in all_workbooks]) |
| 50 | + |
| 51 | + if all_workbooks: |
| 52 | + # Pick 1 workbook that has live datasource connection. |
| 53 | + # Assuming 1st workbook met the criteria for sample purposes |
| 54 | + # Data Freshness Policy is not available on extract & file-based datasource. |
| 55 | + sample_workbook = all_workbooks[2] |
| 56 | + |
| 57 | + # Get more info from the workbook selected |
| 58 | + # Troubleshoot: if sample_workbook_extended.data_freshness_policy.option returns with AttributeError |
| 59 | + # it could mean the workbook selected does not have live connection, which means it doesn't have |
| 60 | + # data freshness policy. Change to another workbook with live datasource connection. |
| 61 | + sample_workbook_extended = server.workbooks.get_by_id(sample_workbook.id) |
| 62 | + try: |
| 63 | + print( |
| 64 | + "Workbook " |
| 65 | + + sample_workbook.name |
| 66 | + + " has data freshness policy option set to: " |
| 67 | + + sample_workbook_extended.data_freshness_policy.option |
| 68 | + ) |
| 69 | + except AttributeError as e: |
| 70 | + print( |
| 71 | + "Workbook does not have data freshness policy, possibly due to the workbook selected " |
| 72 | + "does not have live connection. Change to another workbook using live datasource connection." |
| 73 | + ) |
| 74 | + |
| 75 | + # Update Workbook Data Freshness Policy to "AlwaysLive" |
| 76 | + sample_workbook.data_freshness_policy = TSC.DataFreshnessPolicyItem( |
| 77 | + TSC.DataFreshnessPolicyItem.Option.AlwaysLive |
| 78 | + ) |
| 79 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 80 | + print( |
| 81 | + "Workbook " |
| 82 | + + updated.name |
| 83 | + + " updated data freshness policy option to: " |
| 84 | + + updated.data_freshness_policy.option |
| 85 | + ) |
| 86 | + |
| 87 | + # Update Workbook Data Freshness Policy to "SiteDefault" |
| 88 | + sample_workbook.data_freshness_policy = TSC.DataFreshnessPolicyItem( |
| 89 | + TSC.DataFreshnessPolicyItem.Option.SiteDefault |
| 90 | + ) |
| 91 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 92 | + print( |
| 93 | + "Workbook " |
| 94 | + + updated.name |
| 95 | + + " updated data freshness policy option to: " |
| 96 | + + updated.data_freshness_policy.option |
| 97 | + ) |
| 98 | + |
| 99 | + # Update Workbook Data Freshness Policy to "FreshEvery" schedule. |
| 100 | + # Set the schedule to be fresh every 10 hours |
| 101 | + # Once the data_freshness_policy is already populated (e.g. due to previous calls), |
| 102 | + # it is possible to directly change the option & other parameters directly like below |
| 103 | + sample_workbook.data_freshness_policy.option = TSC.DataFreshnessPolicyItem.Option.FreshEvery |
| 104 | + fresh_every_ten_hours = TSC.DataFreshnessPolicyItem.FreshEvery( |
| 105 | + TSC.DataFreshnessPolicyItem.FreshEvery.Frequency.Hours, 10 |
| 106 | + ) |
| 107 | + sample_workbook.data_freshness_policy.fresh_every_schedule = fresh_every_ten_hours |
| 108 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 109 | + print( |
| 110 | + "Workbook " |
| 111 | + + updated.name |
| 112 | + + " updated data freshness policy option to: " |
| 113 | + + updated.data_freshness_policy.option |
| 114 | + + " with frequency of " |
| 115 | + + str(updated.data_freshness_policy.fresh_every_schedule.value) |
| 116 | + + " " |
| 117 | + + updated.data_freshness_policy.fresh_every_schedule.frequency |
| 118 | + ) |
| 119 | + |
| 120 | + # Update Workbook Data Freshness Policy to "FreshAt" schedule. |
| 121 | + # Set the schedule to be fresh at 10AM every day |
| 122 | + sample_workbook.data_freshness_policy.option = TSC.DataFreshnessPolicyItem.Option.FreshAt |
| 123 | + fresh_at_ten_daily = TSC.DataFreshnessPolicyItem.FreshAt( |
| 124 | + TSC.DataFreshnessPolicyItem.FreshAt.Frequency.Day, "10:00:00", "America/Los_Angeles" |
| 125 | + ) |
| 126 | + sample_workbook.data_freshness_policy.fresh_at_schedule = fresh_at_ten_daily |
| 127 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 128 | + print( |
| 129 | + "Workbook " |
| 130 | + + updated.name |
| 131 | + + " updated data freshness policy option to: " |
| 132 | + + updated.data_freshness_policy.option |
| 133 | + + " with frequency of " |
| 134 | + + str(updated.data_freshness_policy.fresh_at_schedule.time) |
| 135 | + + " every " |
| 136 | + + updated.data_freshness_policy.fresh_at_schedule.frequency |
| 137 | + ) |
| 138 | + |
| 139 | + # Set the schedule to be fresh at 6PM every week on Wednesday and Sunday |
| 140 | + sample_workbook.data_freshness_policy = TSC.DataFreshnessPolicyItem( |
| 141 | + TSC.DataFreshnessPolicyItem.Option.FreshAt |
| 142 | + ) |
| 143 | + fresh_at_6pm_wed_sun = TSC.DataFreshnessPolicyItem.FreshAt( |
| 144 | + TSC.DataFreshnessPolicyItem.FreshAt.Frequency.Week, |
| 145 | + "18:00:00", |
| 146 | + "America/Los_Angeles", |
| 147 | + [IntervalItem.Day.Wednesday, "Sunday"], |
| 148 | + ) |
| 149 | + |
| 150 | + sample_workbook.data_freshness_policy.fresh_at_schedule = fresh_at_6pm_wed_sun |
| 151 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 152 | + new_fresh_at_schedule = updated.data_freshness_policy.fresh_at_schedule |
| 153 | + print( |
| 154 | + "Workbook " |
| 155 | + + updated.name |
| 156 | + + " updated data freshness policy option to: " |
| 157 | + + updated.data_freshness_policy.option |
| 158 | + + " with frequency of " |
| 159 | + + str(new_fresh_at_schedule.time) |
| 160 | + + " every " |
| 161 | + + new_fresh_at_schedule.frequency |
| 162 | + + " on " |
| 163 | + + new_fresh_at_schedule.interval_item[0] |
| 164 | + + "," |
| 165 | + + new_fresh_at_schedule.interval_item[1] |
| 166 | + ) |
| 167 | + |
| 168 | + # Set the schedule to be fresh at 12AM every last day of the month |
| 169 | + sample_workbook.data_freshness_policy = TSC.DataFreshnessPolicyItem( |
| 170 | + TSC.DataFreshnessPolicyItem.Option.FreshAt |
| 171 | + ) |
| 172 | + fresh_at_last_day_of_month = TSC.DataFreshnessPolicyItem.FreshAt( |
| 173 | + TSC.DataFreshnessPolicyItem.FreshAt.Frequency.Month, "00:00:00", "America/Los_Angeles", ["LastDay"] |
| 174 | + ) |
| 175 | + |
| 176 | + sample_workbook.data_freshness_policy.fresh_at_schedule = fresh_at_last_day_of_month |
| 177 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 178 | + new_fresh_at_schedule = updated.data_freshness_policy.fresh_at_schedule |
| 179 | + print( |
| 180 | + "Workbook " |
| 181 | + + updated.name |
| 182 | + + " updated data freshness policy option to: " |
| 183 | + + updated.data_freshness_policy.option |
| 184 | + + " with frequency of " |
| 185 | + + str(new_fresh_at_schedule.time) |
| 186 | + + " every " |
| 187 | + + new_fresh_at_schedule.frequency |
| 188 | + + " on " |
| 189 | + + new_fresh_at_schedule.interval_item[0] |
| 190 | + ) |
| 191 | + |
| 192 | + # Set the schedule to be fresh at 8PM every 1st,13th,20th day of the month |
| 193 | + fresh_at_dates_of_month = TSC.DataFreshnessPolicyItem.FreshAt( |
| 194 | + TSC.DataFreshnessPolicyItem.FreshAt.Frequency.Month, |
| 195 | + "00:00:00", |
| 196 | + "America/Los_Angeles", |
| 197 | + ["1", "13", "20"], |
| 198 | + ) |
| 199 | + |
| 200 | + sample_workbook.data_freshness_policy.fresh_at_schedule = fresh_at_dates_of_month |
| 201 | + updated: TSC.WorkbookItem = server.workbooks.update(sample_workbook) |
| 202 | + new_fresh_at_schedule = updated.data_freshness_policy.fresh_at_schedule |
| 203 | + print( |
| 204 | + "Workbook " |
| 205 | + + updated.name |
| 206 | + + " updated data freshness policy option to: " |
| 207 | + + updated.data_freshness_policy.option |
| 208 | + + " with frequency of " |
| 209 | + + str(new_fresh_at_schedule.time) |
| 210 | + + " every " |
| 211 | + + new_fresh_at_schedule.frequency |
| 212 | + + " on " |
| 213 | + + str(new_fresh_at_schedule.interval_item) |
| 214 | + ) |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + main() |
0 commit comments