Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An assortment of Pbench Ops fixes and fun #3612

Merged
merged 7 commits into from
Apr 5, 2024
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
57 changes: 30 additions & 27 deletions lib/pbench/cli/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from threading import Thread
import time
from typing import Any, Optional
from typing import Any, Optional, Union

import click
from click import Context, Parameter, ParamType
Expand All @@ -11,6 +11,27 @@
from pbench.server.database import init_db


class DateParser(ParamType):
"""The DateParser type converts date strings into `datetime` objects.

This is a variant of click's built-in DateTime parser, but uses the
more flexible dateutil.parser
"""

name = "dateparser"

def convert(
self, value: Any, param: Optional[Parameter], ctx: Optional[Context]
) -> Any:
if isinstance(value, datetime.datetime):
return value

try:
return parser.parse(value)
except Exception as e:
self.fail(f"{value!r} cannot be converted to a datetime: {str(e)!r}")


class Detail:
"""Encapsulate generation of additional diagnostics"""

Expand Down Expand Up @@ -63,29 +84,32 @@ def warning(self, message: str):
class Verify:
"""Encapsulate -v status messages."""

def __init__(self, verify: bool):
def __init__(self, verify: Union[bool, int]):
"""Initialize the object.

Args:
verify: True to write status messages.
"""
self.verify = verify
if isinstance(verify, int):
self.verify = verify
else:
self.verify = 1 if verify else 0

def __bool__(self) -> bool:
"""Report whether verification is enabled.

Returns:
True if verification is enabled.
"""
return self.verify
return bool(self.verify)

def status(self, message: str):
def status(self, message: str, level: int = 1):
webbnh marked this conversation as resolved.
Show resolved Hide resolved
"""Write a message if verification is enabled.

Args:
message: status string
"""
if self.verify:
if self.verify >= level:
ts = datetime.datetime.now().astimezone()
click.secho(f"({ts:%H:%M:%S}) {message}", fg="green", err=True)

Expand Down Expand Up @@ -138,27 +162,6 @@ def watcher(self):
)


class DateParser(ParamType):
"""The DateParser type converts date strings into `datetime` objects.

This is a variant of click's built-in DateTime parser, but uses the
more flexible dateutil.parser
"""

name = "dateparser"

def convert(
self, value: Any, param: Optional[Parameter], ctx: Optional[Context]
) -> Any:
if isinstance(value, datetime.datetime):
return value

try:
return parser.parse(value)
except Exception as e:
self.fail(f"{value!r} cannot be converted to a datetime: {str(e)!r}")


def config_setup(context: object) -> PbenchServerConfig:
config = PbenchServerConfig.create(context.config)
# We're going to need the DB to track dataset state, so setup DB access.
Expand Down
8 changes: 5 additions & 3 deletions lib/pbench/cli/server/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import click

from pbench.cli import pass_cli_context
from pbench.cli.server import config_setup, Verify
from pbench.cli.server import config_setup, DateParser, Verify
from pbench.cli.server.options import common_options
from pbench.server import BadConfig, OperationCode
from pbench.server.database.database import Database
Expand Down Expand Up @@ -110,6 +110,7 @@ def auditor(kwargs) -> Iterator[str]:

@click.command(name="pbench-audit")
@pass_cli_context
@click.option("--id", type=int, help="Select by audit event ID")
@click.option(
"--ids",
default=False,
Expand All @@ -130,6 +131,7 @@ def auditor(kwargs) -> Iterator[str]:
@click.option("--object-id", type=str, help="Select by object ID")
@click.option("--object-name", type=str, help="Select by object name")
@click.option("--page", default=False, is_flag=True, help="Paginate the output")
@click.option("--root-id", type=int, help="Select by audit event root ID")
@click.option("--user-id", type=str, help="Select by user ID")
@click.option("--user-name", type=str, help="Select by username")
@click.option(
Expand All @@ -142,12 +144,12 @@ def auditor(kwargs) -> Iterator[str]:
)
@click.option(
"--since",
type=click.DateTime(),
type=DateParser(),
help="Select entries on or after specified date/time",
)
@click.option(
"--until",
type=click.DateTime(),
type=DateParser(),
help="Select entries on or before specified date/time",
)
@click.option(
Expand Down
Loading
Loading