From 6740c11f5b644cba83e8d5385f031d12897cf2e4 Mon Sep 17 00:00:00 2001 From: Ilia Kurenkov Date: Wed, 18 Dec 2024 13:38:56 +0100 Subject: [PATCH] First version of helper for creating logs saved views (#17353) * First version of helper for creating logs saved views * add changelog * Drop trivial subfunction, add docs --- ddev/changelog.d/17353.added | 1 + ddev/src/ddev/cli/meta/scripts/__init__.py | 2 + ddev/src/ddev/cli/meta/scripts/saved_views.py | 74 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 ddev/changelog.d/17353.added create mode 100644 ddev/src/ddev/cli/meta/scripts/saved_views.py diff --git a/ddev/changelog.d/17353.added b/ddev/changelog.d/17353.added new file mode 100644 index 0000000000000..f954084777bcd --- /dev/null +++ b/ddev/changelog.d/17353.added @@ -0,0 +1 @@ +First version of helper for creating logs saved views. diff --git a/ddev/src/ddev/cli/meta/scripts/__init__.py b/ddev/src/ddev/cli/meta/scripts/__init__.py index 5605258b6499d..d70d059bee64c 100644 --- a/ddev/src/ddev/cli/meta/scripts/__init__.py +++ b/ddev/src/ddev/cli/meta/scripts/__init__.py @@ -8,6 +8,7 @@ from ddev.cli.meta.scripts.generate_metrics import generate_metrics from ddev.cli.meta.scripts.monitor import monitor +from ddev.cli.meta.scripts.saved_views import sv from ddev.cli.meta.scripts.serve_openmetrics_payload import serve_openmetrics_payload from ddev.cli.meta.scripts.upgrade_python import upgrade_python @@ -25,4 +26,5 @@ def scripts(): scripts.add_command(remove_labels) scripts.add_command(serve_openmetrics_payload) scripts.add_command(upgrade_python) +scripts.add_command(sv) scripts.add_command(monitor) diff --git a/ddev/src/ddev/cli/meta/scripts/saved_views.py b/ddev/src/ddev/cli/meta/scripts/saved_views.py new file mode 100644 index 0000000000000..be44a5dabc0e0 --- /dev/null +++ b/ddev/src/ddev/cli/meta/scripts/saved_views.py @@ -0,0 +1,74 @@ +# (C) Datadog, Inc. 2024-present +# All rights reserved +# Licensed under a 3-clause BSD style license (see LICENSE) +import json +import webbrowser +from urllib.parse import parse_qs, urlparse + +import click + +TIP = """ +We're about to open JSON definition of the saved view in your browser. Copy the JSON on that page and paste at +the prompt. Hit any key to continue. +""" + + +def _convert_options(api_options): + stream = api_options["stream"] + asset_options = { + "columns": stream["columns"], + } + for old_f, new_f in { + "showDateColumn": "show_date_column", + "showMessageColumn": "show_message_column", + "messageDisplay": "message_display", + "showTimeline": "show_timeline", + }.items(): + if old_f in stream: + asset_options[new_f] = stream[old_f] + + return asset_options + + +def convert_to_asset(sv_json): + """ + Take saved view json from the API and convert it to an asset definition. + """ + asset_def = {} + logs_view = sv_json["logs_view"] + + keep_as_is = ( + "name", + "type", + "page", + ) + for f in keep_as_is: + asset_def[f] = logs_view[f] + + renames = {"search": "query", "facets": "visible_facets"} + for old_f, new_f in renames.items(): + asset_def[new_f] = logs_view[old_f] + asset_def["options"] = _convert_options(logs_view["options"]) + asset_def["timerange"] = {"interval_ms": logs_view["timerange"]["interval"]} + return asset_def + + +@click.command() +@click.argument('saved_view_permalink', type=str) +def sv(saved_view_permalink): + """ + Helper for working with Logs Saved Views. + + Accepts a permalink to a saved view, then guides you towards creating an asset definition in JSON. + + VERY EARLY VERSION, MAKE SURE TO CHECK --help FOR CHANGES BEFORE USING. + """ + parsed_url = urlparse(saved_view_permalink) + # urllib parses query values as lists, so we must take the first (and only) element as the ID. + sv_id = parse_qs(parsed_url.query)['saved-view-id'][0] + json_url = parsed_url._replace(path=f"/api/v1/logs/views/{sv_id}", query='').geturl() + input(TIP) + webbrowser.open(json_url) + sv_json = json.loads(click.prompt(text="Paste your JSON here, then hit ENTER ", prompt_suffix="> ")) + + click.echo(json.dumps(convert_to_asset(sv_json), indent=2, sort_keys=True))