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

Add event type filtering to listener command #337

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c6bc748
Add event type filtering to listener command
callumforrester Nov 15, 2023
b5de00f
Fix formatting
callumforrester Nov 28, 2023
eea2794
Extract handler methods to ABC (#342)
joeshannon Dec 13, 2023
6b45d7e
Add try except to messaging connection (#345)
abbiemery Jan 9, 2024
2f10be1
Adapt to Black 24 (#358)
joeshannon Feb 2, 2024
99762fd
Run BlueskyContext and Worker in subprocess (#343)
joeshannon Feb 5, 2024
37ce1e5
Remove reference to deleted devcontainer documentation (#373)
callumforrester Feb 7, 2024
9e214d6
Remove parameter caching inside task class (#370)
callumforrester Feb 7, 2024
b7521d4
Enable getting Stomp authentication credentials from the environment …
DiamondJoseph Feb 27, 2024
3ed23e7
Revamp architecture diagram and add logo (#344)
callumforrester Feb 27, 2024
0920886
Add documentation for writing blueapi compliant plans (#377)
DiamondJoseph Mar 6, 2024
e438a63
remove RunPlan class that wraps Task (#382)
stan-dot Mar 7, 2024
4c2c423
233 pending tasks are not cleared (#386)
stan-dot Mar 14, 2024
c4c0b48
Update run-container.rst (#349)
stan-dot Mar 14, 2024
d5d3f92
Adopt python-copier-template (#384)
joeshannon Mar 15, 2024
1bd3438
Convert README and CONTRIBUTING to markdown (#394)
joeshannon Mar 15, 2024
b424917
Enable ruff pyupgrade ruleset (#395)
joeshannon Mar 15, 2024
a1139d0
Replace skeleton with copier in add-plans-and-devices.rst (#396)
joeshannon Mar 15, 2024
440b4be
Update catalog-info.yaml (#400)
keithralphs Mar 25, 2024
e6b679c
delete config (#408)
stan-dot Apr 4, 2024
2891931
Add logo to README and concept image (#414)
joeshannon Apr 9, 2024
7e07c34
Pin dodal to previous version (#420)
joeshannon Apr 12, 2024
62bd9a5
Drop Support for Python3.9 (#418)
callumforrester Apr 12, 2024
bde195d
Convert ADR 0001 to markdown (#415)
joeshannon Apr 12, 2024
87d7fe1
Renumber adrs (#417)
joeshannon Apr 12, 2024
fc08129
Update pyproject.toml to new stomp name (#424)
DominicOram Apr 19, 2024
2ba5ee5
Allow context to deal with new style unions and add tests (#436)
DominicOram Apr 26, 2024
871d775
Remove message bus dependency (#433)
ZohebShaikh Apr 26, 2024
cd4b109
Fix Coverage Settings (#438)
callumforrester Apr 26, 2024
390aa77
Remove DirectoryProvider and pre-processor handling to Dodal (#376)
DiamondJoseph Apr 29, 2024
87dd614
Install git in runtime container (#446)
joeshannon May 1, 2024
ffc184c
Auto restart deployment on config change (#450)
joeshannon May 2, 2024
5b5bd34
Add event type filtering to listener command
callumforrester Nov 15, 2023
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
39 changes: 34 additions & 5 deletions src/blueapi/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import wraps
from pathlib import Path
from pprint import pprint
from typing import Optional, Tuple, Union
from typing import List, Optional, Tuple, Union

import click
from requests.exceptions import ConnectionError
Expand All @@ -27,6 +27,12 @@

from .rest import BlueapiRestClient

EVENT_TYPE_MAPPINGS = {
"worker": WorkerEvent,
"progress": ProgressEvent,
"data": DataEvent,
}


@click.group(invoke_without_command=True)
@click.version_option(version=__version__, prog_name="blueapi")
Expand All @@ -50,7 +56,7 @@ def main(ctx: click.Context, config: Union[Optional[Path], Tuple[Path, ...]]) ->
loaded_config: ApplicationConfig = config_loader.load()

ctx.obj["config"] = loaded_config
logging.basicConfig(level=loaded_config.logging.level)
logging.basicConfig(level=loaded_config.logging.level.cli)

if ctx.invoked_subcommand is None:
print("Please invoke subcommand!")
Expand Down Expand Up @@ -131,18 +137,41 @@ def get_devices(obj: dict) -> None:

@controller.command(name="listen")
@check_connection
@click.option(
"-t",
"--event-type",
type=click.Choice(
list(EVENT_TYPE_MAPPINGS.keys()),
case_sensitive=False,
),
help="The type of events to filter for, defaults to all",
multiple=True,
)
@click.pass_obj
def listen_to_events(obj: dict) -> None:
def listen_to_events(obj: dict, event_type: List[str]) -> None:
"""Listen to events output by blueapi"""
config: ApplicationConfig = obj["config"]
amq_client = AmqClient(StompMessagingTemplate.autoconfigured(config.stomp))
event_type = event_type or list(EVENT_TYPE_MAPPINGS.keys())

def is_allowed(event: Union[WorkerEvent, ProgressEvent, DataEvent]) -> bool:
return any(
map(
lambda allowed_type: isinstance(
event,
EVENT_TYPE_MAPPINGS[allowed_type],
),
event_type or [],
)
)

def on_event(
context: MessageContext,
event: Union[WorkerEvent, ProgressEvent, DataEvent],
) -> None:
converted = json.dumps(event.dict(), indent=2)
print(converted)
if is_allowed(event):
converted = json.dumps(event.dict(), indent=2)
print(converted)

print(
"Subscribing to all bluesky events from "
Expand Down
15 changes: 14 additions & 1 deletion src/blueapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,21 @@ class EnvironmentConfig(BlueapiBaseModel):
events: WorkerEventConfig = Field(default_factory=WorkerEventConfig)


class LoggingLevelConfig(BlueapiBaseModel):
"""
Log levels of blueapi applications and components
"""

service: LogLevel = "INFO"
cli: LogLevel = "ERROR"


class LoggingConfig(BlueapiBaseModel):
level: LogLevel = "INFO"
"""
Config for how blueapi logs behave
"""

level: LoggingLevelConfig = Field(default_factory=LoggingLevelConfig)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed any more, the CLI and server just have entirely separate configurations, so do not need a shared configuration with separate elements.



class RestConfig(BlueapiBaseModel):
Expand Down
Loading