-
Notifications
You must be signed in to change notification settings - Fork 0
Add command line interface #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Allows executing pon2imas as a module with `python -m imas_streams` | ||
|
|
||
| from imas_streams.cli import main | ||
|
|
||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import logging | ||
|
|
||
| import click | ||
| import imas | ||
|
|
||
| from imas_streams import BatchedIDSConsumer | ||
|
|
||
|
|
||
| @click.group(invoke_without_command=True, no_args_is_help=True) | ||
| @click.version_option() | ||
| def main() -> None: | ||
| """Command line utilities for streaming IMAS data.""" | ||
| # Disable IMAS-Python log handler (prevent double output for imas log messages) | ||
| imas_logger = logging.getLogger("imas") | ||
| for handler in imas_logger.handlers: | ||
| imas_logger.removeHandler(handler) | ||
| # Set up our own basic log hander, writing messages to sys.stderr | ||
| logging.basicConfig( | ||
| level=logging.INFO, | ||
| format="%(asctime)s %(levelname)s [%(name)s] %(message)s", | ||
| ) | ||
|
|
||
|
|
||
| @main.command() | ||
| @click.argument("kafka_host") | ||
| @click.argument("kafka_topic") | ||
| @click.argument("imas_uri") | ||
| @click.option( | ||
| "--batch-size", default=100, help="Number of time slice to batch per put_slice." | ||
| ) | ||
| @click.option( | ||
| "--overwrite", is_flag=True, help="Overwrite any existing IMAS Data Entry." | ||
| ) | ||
| def kafka_to_imasentry( | ||
| kafka_host: str, kafka_topic: str, imas_uri: str, batch_size: int, overwrite: bool | ||
| ): | ||
| """Consume streaming IMAS data from Kafka and store data in an IMAS Data Entry. | ||
|
|
||
| N.B. This program requires the optional kafka dependency. | ||
|
|
||
| \b | ||
| Arguments: | ||
| KAFKA_HOST Kafka host and port (aka bootstrap.servers). E.g. 'localhost:9092' | ||
| KAFKA_TOPIC Name of the kafka topic with streaming IMAS data. | ||
| IMAS_URI IMAS URI to store the data at, for example 'imas:hdf5?path=./out'. | ||
| The program will not overwrite existing data (unless the --overwrite | ||
| flag is given). Only backends that implement 'put_slice' are | ||
| supported, such as HDF5 and MDSPLUS. | ||
| """ | ||
| # Local import: kafka is an optional dependency | ||
| from imas_streams.kafka import KafkaConsumer, KafkaSettings | ||
|
|
||
| consumer = KafkaConsumer( | ||
| KafkaSettings(host=kafka_host, topic_name=kafka_topic), | ||
| BatchedIDSConsumer, | ||
| batch_size=batch_size, | ||
| return_copy=False, | ||
| ) | ||
|
|
||
| mode = "w" if overwrite else "x" | ||
| with imas.DBEntry(imas_uri, mode) as entry: | ||
| for result in consumer.stream(): | ||
| if result is not None: | ||
| entry.put_slice(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import imas | ||
| from click.testing import CliRunner | ||
| from imas.ids_defs import IDS_TIME_MODE_HOMOGENEOUS | ||
| from imas.util import idsdiffgen | ||
|
|
||
| from imas_streams import BatchedIDSConsumer | ||
| from imas_streams.cli import main | ||
| from imas_streams.kafka import KafkaSettings | ||
|
|
||
|
|
||
| def test_version(): | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--version"]) | ||
| assert result.exit_code == 0 | ||
|
|
||
|
|
||
| @patch("imas_streams.kafka.KafkaConsumer") | ||
| def test_kafka_to_imasentry(mock_kafkaconsumer, tmp_path): | ||
| # Make some testdata to store | ||
| ids = imas.IDSFactory().core_profiles() | ||
| ids.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS | ||
| ids.time = [0.0, 0.1, 0.2] | ||
| ids.global_quantities.ip = [0.2, 0.3, 0.4] | ||
| mock_kafkaconsumer.return_value.stream.return_value = [None, None, ids] | ||
|
|
||
| # Run CLI | ||
| runner = CliRunner() | ||
| uri = f"imas:hdf5?path={tmp_path}" | ||
| result = runner.invoke( | ||
| main, | ||
| ["kafka-to-imasentry", "kafka_host:port", "topic_name", uri, "--batch-size=10"], | ||
| ) | ||
|
|
||
| assert result.exit_code == 0 | ||
| mock_kafkaconsumer.assert_called_once_with( | ||
| KafkaSettings(host="kafka_host:port", topic_name="topic_name"), | ||
| BatchedIDSConsumer, | ||
| batch_size=10, | ||
| return_copy=False, | ||
| ) | ||
| # Check that the IDS was stored correctly | ||
| with imas.DBEntry(uri, "r") as entry: | ||
| ids2 = entry.get("core_profiles") | ||
| assert list(idsdiffgen(ids, ids2)) == [] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to also mock the ids put_slice part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed: mocking the
put_slicedoesn't improve the readability of the test, so we'll keep it as is.