chore: import upstream snapshot with attribution
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
"""PostgreSQL Slow Queries Tool."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from core.tool_framework.tool_decorator import tool
|
||||
from core.tool_framework.utils.sql_wrapper import call_db_tool_with_default_db_warning
|
||||
from integrations.postgresql import (
|
||||
get_slow_queries,
|
||||
postgresql_extract_params,
|
||||
postgresql_is_available,
|
||||
resolve_postgresql_config,
|
||||
)
|
||||
|
||||
|
||||
class PostgreSQLSlowQueriesInput(BaseModel):
|
||||
host: str = Field(description="PostgreSQL host or endpoint name.")
|
||||
database: str | None = Field(
|
||||
default=None,
|
||||
description="Target database name. Defaults to integration database when omitted.",
|
||||
)
|
||||
threshold_ms: int = Field(
|
||||
default=1000,
|
||||
description="Minimum mean execution time (ms) for query inclusion.",
|
||||
)
|
||||
port: int = Field(default=5432, description="PostgreSQL TCP port.")
|
||||
|
||||
|
||||
class PostgreSQLSlowQueriesOutput(BaseModel):
|
||||
source: str = Field(description="Evidence source label.")
|
||||
available: bool = Field(description="Whether query stats were retrieved.")
|
||||
queries: list[dict[str, Any]] = Field(
|
||||
default_factory=list,
|
||||
description="Slow query rows ranked by mean execution time.",
|
||||
)
|
||||
total_queries: int = Field(default=0, description="Number of slow query rows returned.")
|
||||
threshold_ms: int | None = Field(default=None, description="Applied threshold in ms.")
|
||||
database: str | None = Field(default=None, description="Database queried for stats.")
|
||||
default_db_warning: str | None = Field(
|
||||
default=None,
|
||||
description="Warning emitted when the default database fallback is used.",
|
||||
)
|
||||
error: str | None = Field(default=None, description="Error details when query fails.")
|
||||
|
||||
|
||||
@tool(
|
||||
name="get_postgresql_slow_queries",
|
||||
description=(
|
||||
"Retrieve slow PostgreSQL queries from pg_stat_statements extension, ranked"
|
||||
" by mean execution time."
|
||||
),
|
||||
source="postgresql",
|
||||
surfaces=("investigation", "chat"),
|
||||
use_cases=[
|
||||
"Identifying slow queries that may be causing performance degradation",
|
||||
"Analyzing query execution patterns during incident timeframes",
|
||||
"Finding poorly optimized queries with high execution times or low cache hit rates",
|
||||
],
|
||||
source_id="postgresql_pg_stat_statements",
|
||||
evidence_type="query_stats",
|
||||
side_effect_level="read_only",
|
||||
examples=[
|
||||
"List slow queries above 1000ms to diagnose database latency spikes.",
|
||||
"Lower threshold to 200ms to inspect emerging query regressions.",
|
||||
],
|
||||
anti_examples=["Use this tool for pod restart loops or Kubernetes health checks."],
|
||||
input_model=PostgreSQLSlowQueriesInput,
|
||||
output_model=PostgreSQLSlowQueriesOutput,
|
||||
is_available=postgresql_is_available,
|
||||
injected_params=("host",),
|
||||
extract_params=postgresql_extract_params,
|
||||
)
|
||||
def get_postgresql_slow_queries(
|
||||
host: str,
|
||||
database: str | None = None,
|
||||
threshold_ms: int = 1000,
|
||||
port: int = 5432,
|
||||
) -> dict[str, Any]:
|
||||
"""Fetch slow query statistics above the threshold (default 1000ms mean time)."""
|
||||
return call_db_tool_with_default_db_warning(
|
||||
database=database,
|
||||
default_db_name="postgres",
|
||||
config_resolver=resolve_postgresql_config,
|
||||
resolver_kwargs={"host": host, "port": port},
|
||||
db_caller=lambda config: get_slow_queries(config, threshold_ms=threshold_ms),
|
||||
)
|
||||
Reference in New Issue
Block a user