Skip to content

Commit

Permalink
feat: metric input is now a InputParamClass
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaxde committed Oct 17, 2024
1 parent 7614304 commit f105cd0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
5 changes: 4 additions & 1 deletion examples/metricq_get_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import click_log # type: ignore

import metricq
from metricq.cli import metric_input, metricq_command
from metricq.cli import metricq_command
from metricq.cli.params import metric_input

logger = metricq.get_logger()

Expand Down Expand Up @@ -107,6 +108,8 @@ async def aget_history(
def get_history(
server: str, token: str, metric: str, list_metrics: bool, list_metadata: bool
) -> None:
print(metric)
exit(0)
if not (list_metrics or list_metadata) and metric is None:
metric = "example.quantity"
asyncio.run(aget_history(server, token, metric, list_metrics, list_metadata))
Expand Down
3 changes: 2 additions & 1 deletion metricq/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
DurationParam,
TemplateStringParam,
TimestampParam,
metric_input,
)
from .wrapper import metric_input, metricq_command
from .wrapper import metricq_command

__all__ = [
"ChoiceParam",
Expand Down
26 changes: 26 additions & 0 deletions metricq/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def convert(
ctx=ctx,
)


class DurationParam(ParamType):
name = "duration"

Expand Down Expand Up @@ -168,3 +169,28 @@ def convert(
if not isinstance(value, str):
raise TypeError("expected a string type for TemplateStringParam")
return Template(value).safe_substitute(self.mapping)


class MetricInputParam(ParamType):
pattern = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+\.)+[a-zA-Z][a-zA-Z0-9_]+")

def convert(
self, value: Any, param: Optional[Parameter], ctx: Optional[Context]
) -> str:
if not isinstance(value, str):
raise TypeError("expected a string type for the metric input")
if not self.pattern.match(value):
raise ValueError(f"Invalid metric format: '{value}'.")
return value


def metric_input(default: Optional[str] = None) -> Callable[[FC], FC]:
return option(
"--metric",
type=MetricInputParam(),
metavar="METRIC",
show_default=True,
required=default is None,
default=default,
help="Use the -–metric parameter to specify which metric the program should use",
)
27 changes: 1 addition & 26 deletions metricq/cli/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging
import re
from functools import wraps
from typing import Callable, Optional, cast
from typing import Callable, cast

import click
import click_log # type: ignore
Expand Down Expand Up @@ -85,26 +83,3 @@ def decorator(func: FC) -> click.Command:
)

return decorator


def metric_input(
required: bool = True, default: Optional[str] = None
) -> Callable[[FC], FC]:
valid_metric_regex = r"([a-zA-Z][a-zA-Z0-9_]+\.)+[a-zA-Z][a-zA-Z0-9_]+"

def decorator(func): # type: ignore
@click.option("--metric", default=default, help="Metric input")
@wraps(func)
def wrapper(*args, metric, **kwargs): # type: ignore
if metric is not None:
if not re.match(valid_metric_regex, metric):
raise ValueError(f"Invalid metric format: {metric}")

if metric is None and required:
raise Exception("Input metric is missing.")

return func(*args, metric=metric, **kwargs)

return wrapper

return decorator

0 comments on commit f105cd0

Please sign in to comment.