-
Notifications
You must be signed in to change notification settings - Fork 13
Introduce simple reporting client with support for single metric historical data #16
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
5 commits
Select commit
Hold shift + click to select a range
d6460ac
Add common client and reporting API dependencies
cwasicki 14ef9ff
Introduce simple reporting client
cwasicki 8697782
Add code example for reporting client usage
cwasicki d2a98f6
Add initial unit tests
cwasicki 1190207
Update release notes
cwasicki 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # License: MIT | ||
| # Copyright © 2024 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Examples usage of reporting API.""" | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| from datetime import datetime | ||
| from pprint import pprint | ||
| from typing import AsyncGenerator | ||
|
|
||
| import pandas as pd | ||
| from frequenz.client.common.metric import Metric | ||
|
|
||
| from frequenz.client.reporting import ReportingClient | ||
|
|
||
| # Experimental import | ||
| from frequenz.client.reporting._client import MetricSample | ||
|
|
||
|
|
||
| # pylint: disable=too-many-locals | ||
| async def main(microgrid_id: int, component_id: int) -> None: | ||
| """Test the ReportingClient. | ||
|
|
||
| Args: | ||
| microgrid_id: int | ||
| component_id: int | ||
| """ | ||
| service_address = "localhost:50051" | ||
| client = ReportingClient(service_address) | ||
|
|
||
| microgrid_components = [(microgrid_id, [component_id])] | ||
| metrics = [ | ||
| Metric.DC_POWER, | ||
| Metric.DC_CURRENT, | ||
| ] | ||
|
|
||
| start_dt = datetime.fromisoformat("2023-11-21T12:00:00.00+00:00") | ||
| end_dt = datetime.fromisoformat("2023-11-21T12:01:00.00+00:00") | ||
|
|
||
| page_size = 10 | ||
|
|
||
| print("########################################################") | ||
| print("Iterate over single metric generator") | ||
|
|
||
| async for sample in client.iterate_single_metric( | ||
| microgrid_id=microgrid_id, | ||
| component_id=component_id, | ||
| metric=metrics[0], | ||
| start_dt=start_dt, | ||
| end_dt=end_dt, | ||
| page_size=page_size, | ||
| ): | ||
| print("Received:", sample) | ||
|
|
||
| ########################################################################### | ||
| # | ||
| # The following code is experimental and demonstrates potential future | ||
| # usage of the ReportingClient. | ||
| # | ||
| ########################################################################### | ||
|
|
||
| async def components_data_iter() -> AsyncGenerator[MetricSample, None]: | ||
| """Iterate over components data. | ||
|
|
||
| Yields: | ||
| Single metric sample | ||
| """ | ||
| # pylint: disable=protected-access | ||
| async for page in client._iterate_components_data_pages( | ||
| microgrid_components=microgrid_components, | ||
| metrics=metrics, | ||
| start_dt=start_dt, | ||
| end_dt=end_dt, | ||
| page_size=page_size, | ||
| ): | ||
| for entry in page.iterate_metric_samples(): | ||
| yield entry | ||
|
|
||
| async def components_data_dict( | ||
| components_data_iter: AsyncGenerator[MetricSample, None] | ||
| ) -> dict[int, dict[int, dict[datetime, dict[Metric, float]]]]: | ||
| """Convert components data iterator into a single dict. | ||
|
|
||
| The nesting structure is: | ||
| { | ||
| microgrid_id: { | ||
| component_id: { | ||
| timestamp: { | ||
| metric: value | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Args: | ||
| components_data_iter: async generator | ||
|
|
||
| Returns: | ||
| Single dict with with all components data | ||
| """ | ||
| ret: dict[int, dict[int, dict[datetime, dict[Metric, float]]]] = {} | ||
|
|
||
| async for ts, mid, cid, met, value in components_data_iter: | ||
| if mid not in ret: | ||
| ret[mid] = {} | ||
| if cid not in ret[mid]: | ||
| ret[mid][cid] = {} | ||
| if ts not in ret[mid][cid]: | ||
| ret[mid][cid][ts] = {} | ||
|
|
||
| ret[mid][cid][ts][met] = value | ||
|
|
||
| return ret | ||
|
|
||
| print("########################################################") | ||
| print("Iterate over generator") | ||
| async for msample in components_data_iter(): | ||
| print("Received:", msample) | ||
|
|
||
| print("########################################################") | ||
| print("Dumping all data as a single dict") | ||
| dct = await components_data_dict(components_data_iter()) | ||
| pprint(dct) | ||
|
|
||
| print("########################################################") | ||
| print("Turn data into a pandas DataFrame") | ||
| data = [cd async for cd in components_data_iter()] | ||
| df = pd.DataFrame(data).set_index("timestamp") | ||
| pprint(df) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("microgrid_id", type=int, help="Microgrid ID") | ||
| parser.add_argument("component_id", type=int, help="Component ID") | ||
|
|
||
| args = parser.parse_args() | ||
| asyncio.run(main(args.microgrid_id, args.component_id)) | ||
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 |
|---|---|---|
|
|
@@ -29,6 +29,8 @@ requires-python = ">= 3.11, < 4" | |
| # TODO(cookiecutter): Remove and add more dependencies if appropriate | ||
| dependencies = [ | ||
| "typing-extensions >= 4.5.0, < 5", | ||
| "frequenz-api-reporting >= 0.1.1, < 1", | ||
| "frequenz-client-common @ git+https://github.com/frequenz-floss/frequenz-client-common-python.git@v0.x.x", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be updated with the upcoming release. |
||
| ] | ||
| dynamic = ["version"] | ||
|
|
||
|
|
@@ -62,6 +64,7 @@ dev-mypy = [ | |
| "types-Markdown == 3.4.2.10", | ||
| # For checking the noxfile, docs/ script, and tests | ||
| "frequenz-client-reporting[dev-mkdocs,dev-noxfile,dev-pytest]", | ||
| "pandas-stubs >= 2, < 3", # Only required for example | ||
| ] | ||
| dev-noxfile = [ | ||
| "nox == 2023.4.22", | ||
|
|
@@ -71,6 +74,7 @@ dev-pylint = [ | |
| "pylint == 3.0.2", | ||
| # For checking the noxfile, docs/ script, and tests | ||
| "frequenz-client-reporting[dev-mkdocs,dev-noxfile,dev-pytest]", | ||
| "pandas >= 2, < 3", # Only required for example | ||
| ] | ||
| dev-pytest = [ | ||
| "pytest == 8.0.0", | ||
|
|
@@ -82,6 +86,10 @@ dev-pytest = [ | |
| dev = [ | ||
| "frequenz-client-reporting[dev-mkdocs,dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,dev-pylint,dev-pytest]", | ||
| ] | ||
| examples = [ | ||
| "grpcio >= 1.51.1, < 2", | ||
| "pandas >= 2, < 3", | ||
| ] | ||
|
|
||
| [project.urls] | ||
| Documentation = "https://frequenz-floss.github.io/frequenz-client-reporting-python/" | ||
|
|
||
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 |
|---|---|---|
| @@ -1,25 +1,12 @@ | ||
| # License: MIT | ||
| # Copyright © 2024 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Reporting API client for Python. | ||
| """Client to connect to the Reporting API. | ||
|
|
||
| TODO(cookiecutter): Add a more descriptive module description. | ||
| This package provides a low-level interface for interacting with the reporting API. | ||
| """ | ||
|
|
||
|
|
||
| # TODO(cookiecutter): Remove this function | ||
| def delete_me(*, blow_up: bool = False) -> bool: | ||
| """Do stuff for demonstration purposes. | ||
| from ._client import ReportingClient | ||
|
|
||
| Args: | ||
| blow_up: If True, raise an exception. | ||
|
|
||
| Returns: | ||
| True if no exception was raised. | ||
|
|
||
| Raises: | ||
| RuntimeError: if blow_up is True. | ||
| """ | ||
| if blow_up: | ||
| raise RuntimeError("This function should be removed!") | ||
| return True | ||
| __all__ = ["ReportingClient"] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.