def download_tile(tile: models.DashboardElement, format: str = "png"): """Download the dashboard tile in the specified format""" if not tile.query_id: print(f"tile {tile.title} has no query_id") return None task = sdk.create_query_render_task(query_id=tile.query_id, result_format=format, width=500, height=500) if not task or not task.id: print(f"Could not create a render task for {tile.title}") return None # poll the render task until it completes elapsed = 0.0 delay = 0.5 # wait .5 seconds while True: poll = sdk.render_task(task.id) if poll.status == "failure": print(poll) print(f"Render failed for {tile.title}") return None elif poll.status == "success": break time.sleep(delay) elapsed += delay print(f"{elapsed} seconds elapsed") result = sdk.render_task_results( task.id, # wait up to 300 seconds transport_options=transport.TransportSettings(timeout=300), ) fileName = f"{tile.title}.{format}" with open(fileName, "wb") as f: f.write(result) return fileName
def get_broken_content(): """Collect broken content""" broken_content = sdk.content_validation( transport_options=transport.TransportSettings( timeout=600)).content_with_errors return broken_content
def settings(): return transport.TransportSettings(base_url="/some/path", api_version="3.1", headers=None, verify_ssl=True)
def main(): days = 90 dashboard_keys = ["id", "title", "user_id", "folder"] look_keys = ["id", "title", "user_id", "folder"] user_keys = ["id", "first_name", "last_name", "email"] folder_keys = ["id", "parent_id", "name"] unused_content = get_unused_content(days) dashboards = sdk.all_dashboards( fields=", ".join(dashboard_keys), transport_options=transport.TransportSettings(timeout=600)) looks = sdk.all_looks( fields=", ".join(look_keys), transport_options=transport.TransportSettings(timeout=600)) users = sdk.all_users( fields=", ".join(user_keys), transport_options=transport.TransportSettings(timeout=600)) folders = sdk.all_folders( fields=", ".join(folder_keys), transport_options=transport.TransportSettings(timeout=600)) base_url = get_base_url() output_data = [] for item in unused_content: row = {} row["dashboard_id"] = item["dashboard.id"] row["look_id"] = item["look.id"] row["dashboard_created_date"] = item["dashboard.created_date"] row["look_created_date"] = item["look.created_date"] row["content_title"] = item["content_usage.content_title"] row["content_type"] = item["content_usage.content_type"] row["last_accessed_date"] = item["content_usage.last_accessed_date"] if item["content_usage.content_type"] == "dashboard": try: dashboard = join_content( dashboards, "id", item, "dashboard.id", ) user_id = dashboard.user_id folder = dashboard.folder folder_id = folder.id folder_name = folder.name parent_folder_id = folder.parent_id except (StopIteration, AttributeError) as e: user_id = None folder = None folder_id = None folder_name = None parent_folder_id = None elif item["content_usage.content_type"] == "look": try: look = join_content(looks, "id", item, "look.id") user_id = look.user_id folder = look.folder folder_id = folder.id folder_name = folder.name parent_folder_id = folder.parent_id except (StopIteration, AttributeError) as e: user_id = None folder = None folder_id = None folder_name = None parent_folder_id = None row["user_id"] = str(user_id) row["folder_id"] = folder_id row["folder_name"] = folder_name row["parent_folder_id"] = parent_folder_id try: user = next(i for i in users if str(getattr(i, "id")) == str(user_id)) row["first_name"] = user.first_name row["last_name"] = user.last_name row["email"] = user.email except (StopIteration, KeyError) as e: row["first_name"] = None row["last_name"] = None row["email"] = None if row["content_type"] == "dashboard": id = row["dashboard_id"] else: id = row["look_id"] content_type = row["content_type"] row["url"] = f"{base_url}/{content_type}s/{id}" try: parent_folder = join_content(folders, "id", row, "parent_folder_id") row["parent_folder_name"] = parent_folder.name except (StopIteration, KeyError): row["parent_folder_name"] = None output_data.append(row) write_content_to_csv(output_data, "unused_content.csv")