Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/pt_snap_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def query_database(
str | None,
typer.Option(
"--category",
help="Filter templates by category (basic, statistical, business)",
help="Filter templates by category",
autocompletion=complete_categories,
),
] = None,
Expand Down Expand Up @@ -232,6 +232,7 @@ def query_database(
"""
from pt_snap_cli.query import QueryExecutor
from pt_snap_cli.query.registry import (
discover_categories,
get_template_info,
list_by_category_with_details,
)
Expand All @@ -241,12 +242,8 @@ def query_database(
list_templates = True

if list_templates:
categories = ["basic", "statistical", "business"]
category_labels = {
"basic": "Basic Queries",
"statistical": "Statistical Queries",
"business": "Business Queries",
}
categories = discover_categories()
category_labels = {cat: cat.replace("_", " ").title() + " Queries" for cat in categories}

if category is not None and category not in categories:
typer.secho(
Expand Down
6 changes: 4 additions & 2 deletions src/pt_snap_cli/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def complete_template_names() -> list[str]:


def complete_categories() -> list[str]:
"""Return valid category names for shell completion."""
return ["basic", "statistical", "business"]
"""Return valid category names from template directory structure for shell completion."""
from pt_snap_cli.query.registry import discover_categories

return discover_categories()


def _resolve_db_for_completion() -> Path | None:
Expand Down
12 changes: 8 additions & 4 deletions src/pt_snap_cli/query/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ def validate_params(self, params: dict[str, Any]) -> dict[str, Any]:
return validated

@classmethod
def from_dict(cls, data: dict[str, Any]) -> QueryTemplate:
def from_dict(cls, data: dict[str, Any], default_category: str | None = None) -> QueryTemplate:
"""Create QueryTemplate from dictionary.

Args:
data: Dictionary with template data.
default_category: Category inferred from directory structure when
not explicitly declared in the YAML.

Returns:
QueryTemplate instance.
Expand All @@ -116,7 +118,7 @@ def from_dict(cls, data: dict[str, Any]) -> QueryTemplate:
parameters=parameters,
query=data.get("query", ""),
output_schema=data.get("output_schema", []),
category=data.get("category", "basic"),
category=data.get("category") or default_category or "basic",
)


Expand Down Expand Up @@ -147,11 +149,13 @@ def list_queries(self) -> list[str]:
return list(self.queries.keys())

@classmethod
def load_yaml(cls, path: str | Path) -> QueryConfig:
def load_yaml(cls, path: str | Path, default_category: str | None = None) -> QueryConfig:
"""Load configuration from YAML file.

Args:
path: Path to YAML file.
default_category: Category inferred from directory structure when
not explicitly declared in the YAML.

Returns:
QueryConfig instance.
Expand All @@ -170,7 +174,7 @@ def load_yaml(cls, path: str | Path) -> QueryConfig:
queries = {}
for name, query_data in data.get("queries", {}).items():
query_data["name"] = name
queries[name] = QueryTemplate.from_dict(query_data)
queries[name] = QueryTemplate.from_dict(query_data, default_category=default_category)

return cls(
version=data.get("version", "1.0"),
Expand Down
55 changes: 53 additions & 2 deletions src/pt_snap_cli/query/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,59 @@ def list_by_category_with_details(category: str) -> list[dict[str, str]]:
return _registry.list_by_category_with_details(category)


def get_template_dir() -> Path:
"""Return the path to the template directory."""
return Path(__file__).parent / "templates"


def discover_categories(template_dir: Path | str | None = None) -> list[str]:
"""Discover category names from subdirectory structure.

Args:
template_dir: Path to template directory. Defaults to package templates dir.

Returns:
Sorted list of category names (subdirectory names).
"""
if template_dir is None:
template_dir = get_template_dir()
else:
template_dir = Path(template_dir)

if not template_dir.exists():
return []

return sorted(
d.name
for d in template_dir.iterdir()
if d.is_dir() and not d.name.startswith("_") and not d.name.startswith(".")
)


def _infer_category(yaml_file: Path, template_dir: Path) -> str | None:
"""Infer category from the parent directory of a YAML file.

Returns None when the file is directly under the template root
(not in a subdirectory), letting YAML's own category field or
default take precedence.
"""
try:
parent = yaml_file.resolve().parent.name
template_root = template_dir.resolve().name
if parent != template_root:
return parent
except OSError:
pass
return None


def _load_yaml_templates(template_dir: Path | str | None = None) -> None:
"""Load all YAML templates from the template directory.

Scans recursively (``**/*.yaml``) so that each subdirectory
acts as a category. The subdirectory name is used as the
template's category when the YAML does not declare one.

Args:
template_dir: Path to template directory. Defaults to package templates dir.
"""
Expand All @@ -262,9 +312,10 @@ def _load_yaml_templates(template_dir: Path | str | None = None) -> None:
if not template_dir.exists():
return

for yaml_file in template_dir.glob("*.yaml"):
for yaml_file in sorted(template_dir.glob("**/*.yaml")):
try:
config = QueryConfig.load_yaml(yaml_file)
default_cat = _infer_category(yaml_file, template_dir)
config = QueryConfig.load_yaml(yaml_file, default_category=default_cat)
for _query_name, template in config.queries.items():
_registry.register(template)
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
active_blocks:
category: basic
description: Get active memory blocks (active=1)
devices:
- all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
blocks_by_size:
category: basic
description: Get blocks filtered by size range
devices:
- all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
events_by_action:
category: basic
description: Get events filtered by action type
devices:
- all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
memory_timeline:
category: basic
description: Get memory allocation timeline for analysis
devices:
- all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
leak_detection:
category: business
description: Detect memory leaks by finding allocations without free events
devices:
- all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
callstack_analysis:
category: statistical
description: Analyze memory allocation by callstack
devices:
- all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: "1.0"
queries:
memory_peak:
category: statistical
description: Get peak memory metrics from trace events with corresponding event IDs
devices:
- all
Expand Down
6 changes: 4 additions & 2 deletions tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ def test_returns_sorted(self):
class TestCompleteCategories:
def test_returns_three_categories(self):
result = complete_categories()
assert result == ["basic", "statistical", "business"]
assert len(result) == 3
assert set(result) == {"basic", "statistical", "business"}

def test_order_is_fixed(self):
assert complete_categories() == ["basic", "statistical", "business"]
result = complete_categories()
assert result == sorted(result)


class TestCompleteDeviceIds:
Expand Down
Loading