def main() -> None: """Given a look id, obtain the query behind it and run it with the desired filter values. https://docs.looker.com/reference/api-and-integration/api-reference/v3.1/query#implementation_notes_9 # noqa: B950 shows an example of how filters are defined in the posted body. To set the same filter in this example, the script needs to be run as follows: $ python run_look_with_filters.py 5 category.name socks """ look_id = sys.argv[1] if len(sys.argv) > 1 else "" filter_args = iter(sys.argv[2:]) filters: Dict[str, str] = {} if not (look_id and len(sys.argv[2:]) > 0 and len(sys.argv[2:]) % 2 == 0): raise sdk_exceptions.ArgumentError( "Please provide: <lookId> <filter_1> <filter_value_1> " "<filter_2> <filter_value_2> ...") for filter_name in filter_args: filters[filter_name] = next(filter_args) query = get_look_query(int(look_id)) results = run_query_with_filter(query, filters) print(f"Query results with filters={filters}:\n{results}", end="\n\n")
def main(): """Given a model, view, and fields create a query, and then a query task to asynchronously execute. $ python query_task.py thelook users users.first_name [users.last_name users.email ...] """ model = sys.argv[1] if len(sys.argv) > 1 else "" view = sys.argv[1] if len(sys.argv) > 1 else "" try: model, view, *fields = sys.argv[1:] except ValueError: raise sdk_exceptions.ArgumentError( textwrap.dedent( """ Please provide: <model> <view> <field1> [<field2> ...] """ ) ) result = main_models(model, view, fields) # an alternate implementation using dictionaries # result = main_dictionaries(model, view, fields) filename = f"{model}--{view}--{('-').join(fields)}" with open(filename, "w") as f: f.write(result) print(f"Look saved to '{filename}'")
def main(): """Given a dashboard title, search all dashboards to retrieve its id and use it to render the dashboard's pdf. Examples of how to use this: $ python download_dashboard_pdf.py "A Test Dashboard" $ python download_dashboard_pdf.py "A Test Dashboard" '{"filter1": "value1, value2", "filter2": "value3"}' $ python download_dashboard_pdf.py "A Test Dashboard" {} "single_column" """ dashboard_title = sys.argv[1] if len(sys.argv) > 1 else "" filters = json.loads(sys.argv[2]) if len(sys.argv) > 2 else None pdf_style = sys.argv[3] if len(sys.argv) > 3 else "tiled" pdf_width = int(sys.argv[4]) if len(sys.argv) > 4 else 545 pdf_height = int(sys.argv[5]) if len(sys.argv) > 5 else 842 if not dashboard_title: raise sdk_exceptions.ArgumentError( textwrap.dedent(""" Please provide: <dashboard_title> [<dashboard_filters>] [<dashboard_style>] [<pdf_width>] [<pdf_height>] dashboard_style defaults to "tiled" pdf_width defaults to 545 pdf_height defaults to 842""")) dashboard = cast(models.Dashboard, get_dashboard(dashboard_title)) download_dashboard(dashboard, pdf_style, pdf_width, pdf_height, filters)
def main(): """Given a connection, obtain its supported tests and run them. Example: $ python test_connection.py thelook """ connection_name = sys.argv[1] if len(sys.argv) > 1 else "" if not connection_name: raise sdk_exceptions.ArgumentError("Please provide a connection name") elif connection_name in ["looker", "looker__internal__analytics"]: raise sdk_exceptions.ArgumentError( f"Connection '{connection_name}' is internal and cannot be tested." ) connection = get_connections(connection_name) results = test_connection(connection) output_results(cast(str, connection.name), results)
def main(): """Given a dashboard title, get the ids of all dashboards with matching titles and move them to trash. $ python soft_delete_dashboard.py "An Unused Dashboard" """ dashboard_title = sys.argv[1] if len(sys.argv) > 1 else "" if not dashboard_title: raise sdk_exceptions.ArgumentError("Please provide: <dashboardTitle>") dashboards = get_dashboards(dashboard_title) delete_dashboards(dashboards)
def main(): """Given a look title, find the corresponding look id and use it to render its image. $ python download_look.py "A good look" 1024 768 png """ look_title = sys.argv[1] if len(sys.argv) > 1 else "" image_width = int(sys.argv[2]) if len(sys.argv) > 2 else 545 image_height = int(sys.argv[3]) if len(sys.argv) > 3 else 842 image_format = sys.argv[4] if len(sys.argv) > 4 else "png" if not look_title: raise sdk_exceptions.ArgumentError( textwrap.dedent(""" Please provide: <lookTitle> [<img_width>] [<img_height>] [<img_format>] img_width defaults to 545 img_height defaults to 842 img_format defaults to 'png'""")) look = get_look(look_title) download_look(look, image_format, image_width, image_height)