Skip to content

feat(BA-4668): Add CLI commands for prometheus query preset admin CRUD and execution#9641

Open
seedspirit wants to merge 2 commits intomainfrom
BA-4668
Open

feat(BA-4668): Add CLI commands for prometheus query preset admin CRUD and execution#9641
seedspirit wants to merge 2 commits intomainfrom
BA-4668

Conversation

@seedspirit
Copy link
Contributor

Summary

  • Add client SDK function (PrometheusQueryPreset) with create, list, get, modify, delete, and execute methods calling the REST API endpoints defined in BEP-1050
  • Add admin CLI commands (backend.ai admin prometheus-query-preset list|info|add|modify|delete) for superadmin CRUD operations
  • Add top-level CLI execute command (backend.ai prometheus-query-preset execute) with repeatable --label key=value flag for all authenticated users

Test plan

  • Verify backend.ai admin prometheus-query-preset list returns presets
  • Verify backend.ai admin prometheus-query-preset add --name test --metric-name m --query-template q creates a preset
  • Verify backend.ai admin prometheus-query-preset info <ID> shows preset details
  • Verify backend.ai admin prometheus-query-preset modify <ID> --name new_name updates a preset
  • Verify backend.ai admin prometheus-query-preset delete <ID> deletes a preset with confirmation
  • Verify backend.ai prometheus-query-preset execute <ID> --start ... --end ... --step ... sends proper API request
  • Verify --label flag supports repeatable key=value format

Resolves BA-4668

…D and execution

- Add SDK function class (client/func/prometheus_query_preset.py) with
  create, list_presets, get, modify, delete, and execute methods
- Add admin CLI commands (list, info, add, modify, delete) under
  `backend.ai admin prometheus-query-preset`
- Add top-level execute CLI command under
  `backend.ai prometheus-query-preset execute` with repeatable --label flag
- Register SDK function in BaseSession and CLI groups in __init__ modules

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 5, 2026 01:22
@github-actions github-actions bot added size:L 100~500 LoC comp:client Related to Client component comp:cli Related to CLI component labels Mar 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new Prometheus query preset feature across the client SDK and CLI, enabling superadmins to CRUD presets and all authenticated users to execute them.

Changes:

  • Added PrometheusQueryPreset client SDK function with CRUD + execute methods.
  • Added admin CLI commands for preset CRUD (backend.ai admin prometheus-query-preset ...).
  • Added a top-level CLI command for executing presets with repeatable --label key=value.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/ai/backend/client/session.py Registers PrometheusQueryPreset as a session function.
src/ai/backend/client/func/prometheus_query_preset.py Implements REST-backed SDK methods for CRUD and execution.
src/ai/backend/client/cli/prometheus_query_preset/commands.py Adds execute CLI command and argument parsing.
src/ai/backend/client/cli/prometheus_query_preset/init.py Declares the top-level Click command group.
src/ai/backend/client/cli/admin/prometheus_query_preset.py Adds admin CRUD Click commands.
src/ai/backend/client/cli/admin/init.py Wires the new admin command group into the admin CLI.
src/ai/backend/client/cli/init.py Wires the new top-level command group into the CLI entrypoint.
changes/9641.feature.md Adds changelog entry for the new CLI feature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +22 to +24
@prometheus_query_preset.command()
@pass_ctx_obj
def list(ctx: CLIContext) -> None:
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def list(...) shadows the Python built-in list, and this repo appears to enforce built-in name checks (e.g., # noqa: A004 for license). Consider renaming the function (e.g., list_ / list_cmd) while keeping the CLI command name via @prometheus_query_preset.command(name=\"list\").

Suggested change
@prometheus_query_preset.command()
@pass_ctx_obj
def list(ctx: CLIContext) -> None:
@prometheus_query_preset.command(name="list")
@pass_ctx_obj
def list_cmd(ctx: CLIContext) -> None:

Copilot uses AI. Check for mistakes.
Comment on lines +104 to +116
@prometheus_query_preset.command()
@pass_ctx_obj
@click.argument("preset_id", type=str)
@click.option("--name", type=str, default=None, help="New preset name.")
@click.option("--metric-name", type=str, default=None, help="New Prometheus metric name.")
@click.option("--query-template", type=str, default=None, help="New PromQL template.")
@click.option("--time-window", type=str, default=None, help="New default time window.")
@click.option(
"--options",
type=JSONParamType(),
default=None,
help="New preset options JSON.",
)
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modify command allows invoking the API with no fields provided (all options default to None), which will send an empty PATCH body and likely fail server-side. Add a client-side validation before calling modify to require at least one of name/metric_name/query_template/time_window/options to be set; otherwise exit with ExitCode.INVALID_ARGUMENT (or raise a Click parameter error).

Copilot uses AI. Check for mistakes.
Comment on lines +129 to +136
result = session.PrometheusQueryPreset.modify(
UUID(preset_id),
name=name,
metric_name=metric_name,
query_template=query_template,
time_window=time_window,
options=options,
)
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modify command allows invoking the API with no fields provided (all options default to None), which will send an empty PATCH body and likely fail server-side. Add a client-side validation before calling modify to require at least one of name/metric_name/query_template/time_window/options to be set; otherwise exit with ExitCode.INVALID_ARGUMENT (or raise a Click parameter error).

Copilot uses AI. Check for mistakes.
if "=" not in label:
print(f"Invalid label format: {label} (expected key=value)", file=sys.stderr)
sys.exit(ExitCode.INVALID_ARGUMENT)
key, value = label.split("=", 1)
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This accepts --label values like =foo or bar= and will send empty keys/values to the API. Validate that both key and value are non-empty (and consider stripping whitespace) before appending; if invalid, fail with ExitCode.INVALID_ARGUMENT (or raise click.BadParameter).

Suggested change
key, value = label.split("=", 1)
key, value = label.split("=", 1)
key = key.strip()
value = value.strip()
if not key or not value:
print(
f"Invalid label key or value: {label} (both key and value must be non-empty)",
file=sys.stderr,
)
sys.exit(ExitCode.INVALID_ARGUMENT)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:cli Related to CLI component comp:client Related to Client component size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants