Skip to content

Commit

Permalink
implementing the new changes to the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaxde committed Oct 17, 2024
1 parent 294d65f commit c895f86
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 103 deletions.
8 changes: 0 additions & 8 deletions examples/metricq_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,13 @@
`telnet localhost 50101` (or `netcat`), inspect tasks and run code in a REPL.
"""
import asyncio
import logging

import aiomonitor # type: ignore
import click_log # type: ignore

import metricq
from metricq.cli import metricq_command

logger = metricq.get_logger()
click_log.basic_config(logger)
logger.setLevel("INFO")
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)


async def run(server: str, token: str) -> None:
Expand All @@ -67,7 +60,6 @@ async def run(server: str, token: str) -> None:


@metricq_command(default_token="client-py-example")
@click_log.simple_verbosity_option(logger) # type: ignore
def main(server: str, token: str) -> None:
asyncio.run(run(server, token))

Expand Down
18 changes: 4 additions & 14 deletions examples/metricq_get_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,17 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import asyncio
import logging
import pprint
from datetime import timedelta

import click
import click_log # type: ignore

import metricq
from metricq.cli import metricq_command
from metricq.cli.params import metric_input
from metricq.cli.wrapper import metricq_metric_option
from metricq.logging import get_logger

logger = metricq.get_logger()

click_log.basic_config(logger)
logger.setLevel("INFO")
# Use this if we ever use threads
# logger.handlers[0].formatter = logging.Formatter(fmt='%(asctime)s %(threadName)-16s %(levelname)-8s %(message)s')
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)
logger = get_logger()


async def aget_history(
Expand Down Expand Up @@ -101,10 +92,9 @@ async def aget_history(


@metricq_command(default_token="history-py-dummy")
@metric_input()
@metricq_metric_option()
@click.option("--list-metrics", is_flag=True)
@click.option("--list-metadata", is_flag=True)
@click_log.simple_verbosity_option(logger) # type: ignore
def get_history(
server: str, token: str, metric: str, list_metrics: bool, list_metadata: bool
) -> None:
Expand Down
16 changes: 2 additions & 14 deletions examples/metricq_get_history_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,15 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import asyncio
import logging
from datetime import timedelta

import click
import click_log # type: ignore

import metricq
from metricq.cli import metricq_command
from metricq.cli.wrapper import metricq_metric_option
from metricq.history_client import HistoryRequestType

logger = metricq.get_logger()

click_log.basic_config(logger)
logger.setLevel("INFO")
# Use this if we ever use threads
# logger.handlers[0].formatter = logging.Formatter(fmt='%(asctime)s %(threadName)-16s %(levelname)-8s %(message)s')
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)


async def aget_history(server: str, token: str, metric: str) -> None:
client = metricq.HistoryClient(token=token, url=server)
Expand Down Expand Up @@ -85,8 +74,7 @@ async def aget_history(server: str, token: str, metric: str) -> None:


@metricq_command(default_token="history-py-dummy")
@click.argument("metric")
@click_log.simple_verbosity_option(logger) # type: ignore
@metricq_metric_option()
def get_history(server: str, token: str, metric: str) -> None:
asyncio.run(aget_history(server, token, metric))

Expand Down
16 changes: 3 additions & 13 deletions examples/metricq_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,17 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import asyncio
import logging
from datetime import timedelta

import click
import click_log # type: ignore

import metricq
from metricq.cli import metric_input, metricq_command
from metricq.cli import metricq_command
from metricq.cli.wrapper import metricq_metric_option
from metricq.pandas import PandasHistoryClient

logger = metricq.get_logger()

click_log.basic_config(logger)
logger.setLevel("INFO")
# Use this if we ever use threads
# logger.handlers[0].formatter = logging.Formatter(fmt='%(asctime)s %(threadName)-16s %(levelname)-8s %(message)s')
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)


async def aget_history(server: str, token: str, metric: str) -> None:
async with PandasHistoryClient(token=token, url=server) as client:
Expand Down Expand Up @@ -83,8 +74,7 @@ async def aget_history(server: str, token: str, metric: str) -> None:


@metricq_command(default_token="history-py-dummy")
@metric_input(default="example.quantity")
@click_log.simple_verbosity_option(logger) # type: ignore
@metricq_metric_option(default="example.quantity")
def get_history(server: str, token: str, metric: str) -> None:
asyncio.run(aget_history(server, token, metric))

Expand Down
9 changes: 0 additions & 9 deletions examples/metricq_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging
from typing import Any

import click
import click_log # type: ignore

import metricq
from metricq import Metric
Expand All @@ -40,12 +38,6 @@

logger = get_logger()

click_log.basic_config(logger)
logger.setLevel("INFO")
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)


# To implement a MetricQ Sink, subclass metricq.Sink
class DummySink(metricq.Sink):
Expand Down Expand Up @@ -79,7 +71,6 @@ async def on_data(

@metricq_command(default_token="sink-py-dummy")
@click.option("-m", "--metrics", multiple=True, required=True)
@click_log.simple_verbosity_option(logger) # type: ignore
def source(server: str, token: str, metrics: list[Metric]) -> None:
# Initialize the DummySink class with a list of metrics given on the
# command line.
Expand Down
10 changes: 0 additions & 10 deletions examples/metricq_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,15 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging
import random
from typing import Any

import click_log # type: ignore

import metricq
from metricq.cli import metricq_command
from metricq.logging import get_logger

logger = get_logger()

click_log.basic_config(logger)
logger.setLevel("INFO")
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)


class DummySource(metricq.IntervalSource):
def __init__(self, *args: Any, **kwargs: Any):
Expand Down Expand Up @@ -75,7 +66,6 @@ async def update(self) -> None:


@metricq_command(default_token="source-py-dummy")
@click_log.simple_verbosity_option(logger) # type: ignore
def source(server: str, token: str) -> None:
src = DummySource(token=token, url=server)
src.run()
Expand Down
17 changes: 4 additions & 13 deletions examples/metricq_synchronous_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,18 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging

import random
import time

import click_log # type: ignore

from metricq import SynchronousSource, Timestamp, get_logger
from metricq import SynchronousSource, Timestamp
from metricq.cli import metricq_command

logger = get_logger()

click_log.basic_config(logger)
logger.setLevel("INFO")
logger.handlers[0].formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
)
from metricq.logging import get_logger


@metricq_command(default_token="source-py-dummy")
@click_log.simple_verbosity_option(logger) # type: ignore
def synchronous_source(server: str, token: str) -> None:
logger = get_logger()
ssource = SynchronousSource(token=token, url=server)
ssource.declare_metrics(
{
Expand Down
12 changes: 9 additions & 3 deletions metricq/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
DurationParam,
TemplateStringParam,
TimestampParam,
metric_input,
)
from .wrapper import metricq_command
from .wrapper import (
metricq_command,
metricq_metric_option,
metricq_server_option,
metricq_token_option,
)

__all__ = [
"ChoiceParam",
Expand All @@ -15,5 +19,7 @@
"TemplateStringParam",
"TimestampParam",
"metricq_command",
"metric_input",
"metricq_metric_option",
"metricq_server_option",
"metricq_token_option",
]
21 changes: 4 additions & 17 deletions metricq/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from getpass import getuser
from socket import gethostname
from string import Template
from typing import Any, Generic, List, Optional, Type, TypeVar, Union, cast, Callable
from typing import Any, Generic, List, Optional, Type, TypeVar, Union, cast

from click import Context, Parameter, ParamType, option
from click import Context, Parameter, ParamType

from ..timeseries import Timedelta, Timestamp
from .types import FC

_C = TypeVar("_C", covariant=True)

Expand Down Expand Up @@ -171,26 +170,14 @@ def convert(
return Template(value).safe_substitute(self.mapping)


class MetricInputParam(ParamType):
class MetricParam(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")
raise TypeError("expected a string type for the MetricParam")
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",
)
5 changes: 3 additions & 2 deletions metricq/cli/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging
from typing import Callable, cast
from typing import Callable, Optional, cast

import click
import click_log # type: ignore
from click import option
from dotenv import find_dotenv, load_dotenv

from .. import get_logger
from .params import TemplateStringParam
from .params import MetricParam, TemplateStringParam
from .types import FC

# We do not interpolate (i.e. replace ${VAR} with corresponding environment variables).
Expand Down Expand Up @@ -53,6 +53,7 @@ def metricq_metric_option(default: Optional[str] = None) -> Callable[[FC], FC]:
)



def get_metric_command_logger() -> logging.Logger:
logger = get_logger()
logger.setLevel(logging.WARNING)
Expand Down

0 comments on commit c895f86

Please sign in to comment.