From 965b0cf18fe91e633b3d7236022220fed04da4fa Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:16:44 +0100 Subject: [PATCH 1/5] Support multiple component IDs in CLI tool So far only single components were supported, this allows requests for data from multiple components. Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- src/frequenz/client/reporting/__main__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/frequenz/client/reporting/__main__.py b/src/frequenz/client/reporting/__main__.py index b550341..5f86a4b 100644 --- a/src/frequenz/client/reporting/__main__.py +++ b/src/frequenz/client/reporting/__main__.py @@ -25,7 +25,12 @@ def main() -> None: default="localhost:50051", ) parser.add_argument("--mid", type=int, help="Microgrid ID", required=True) - parser.add_argument("--cid", type=int, help="Component ID", required=True) + parser.add_argument( + "--cid", + nargs="+", + type=int, + help="Component IDs", + ) parser.add_argument( "--metrics", type=str, @@ -97,7 +102,7 @@ def main() -> None: async def run( *, microgrid_id: int, - component_id: int, + component_id: list[int], metric_names: list[str], start_dt: datetime | None, end_dt: datetime | None, @@ -129,6 +134,7 @@ async def run( client = ReportingApiClient(service_address, key) metrics = [Metric[mn] for mn in metric_names] + microgrid_components = [(microgrid_id, component_id)] def data_iter() -> AsyncIterator[MetricSample]: """Iterate over single metric. @@ -144,9 +150,8 @@ def data_iter() -> AsyncIterator[MetricSample]: else None ) - return client.list_single_component_data( - microgrid_id=microgrid_id, - component_id=component_id, + return client.list_microgrid_components_data( + microgrid_components=microgrid_components, metrics=metrics, start_dt=start_dt, end_dt=end_dt, From 343b4a1ecb93c586158e9887907751f13a901ea8 Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:50:05 +0100 Subject: [PATCH 2/5] Support formulae in CLI tool This change allows to pass formula strings as a `cid` argument from the command line. Multiple formula strings can be passed also together with individual components. Multiple metrics are still supported. Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- src/frequenz/client/reporting/__main__.py | 35 +++++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/frequenz/client/reporting/__main__.py b/src/frequenz/client/reporting/__main__.py index 5f86a4b..fe88ca1 100644 --- a/src/frequenz/client/reporting/__main__.py +++ b/src/frequenz/client/reporting/__main__.py @@ -28,8 +28,8 @@ def main() -> None: parser.add_argument( "--cid", nargs="+", - type=int, - help="Component IDs", + type=str, + help="Component IDs or formulae", ) parser.add_argument( "--metrics", @@ -102,7 +102,7 @@ def main() -> None: async def run( *, microgrid_id: int, - component_id: list[int], + component_id: list[str], metric_names: list[str], start_dt: datetime | None, end_dt: datetime | None, @@ -134,15 +134,18 @@ async def run( client = ReportingApiClient(service_address, key) metrics = [Metric[mn] for mn in metric_names] - microgrid_components = [(microgrid_id, component_id)] - def data_iter() -> AsyncIterator[MetricSample]: + cids = [int(cid.strip()) for cid in component_id if cid.strip().isdigit()] + formulas = [cid.strip() for cid in component_id if not cid.strip().isdigit()] + microgrid_components = [(microgrid_id, cids)] + + async def data_iter() -> AsyncIterator[MetricSample]: """Iterate over single metric. Just a wrapper around the client method for readability. - Returns: - Iterator over single metric samples + Yields: + Single metric samples """ resampling_period = ( timedelta(seconds=resampling_period_s) @@ -150,7 +153,7 @@ def data_iter() -> AsyncIterator[MetricSample]: else None ) - return client.list_microgrid_components_data( + async for sample in client.list_microgrid_components_data( microgrid_components=microgrid_components, metrics=metrics, start_dt=start_dt, @@ -158,7 +161,21 @@ def data_iter() -> AsyncIterator[MetricSample]: resampling_period=resampling_period, include_states=states, include_bounds=bounds, - ) + ): + yield sample + + for formula in formulas: + assert resampling_period is not None + for metric in metrics: + async for sample in client.receive_aggregated_data( + microgrid_id=microgrid_id, + metric=metric, + aggregation_formula=formula, + start=start_dt, + end=end_dt, + resampling_period=resampling_period, + ): + yield sample if fmt == "iter": # Iterate over single metric generator From 6e2ee869becba092da11143a4295b05d18b616dd Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:55:04 +0100 Subject: [PATCH 3/5] Minor: Formatting of CLI tool Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- src/frequenz/client/reporting/__main__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/frequenz/client/reporting/__main__.py b/src/frequenz/client/reporting/__main__.py index fe88ca1..22f9e65 100644 --- a/src/frequenz/client/reporting/__main__.py +++ b/src/frequenz/client/reporting/__main__.py @@ -24,7 +24,12 @@ def main() -> None: help="URL of the Reporting service", default="localhost:50051", ) - parser.add_argument("--mid", type=int, help="Microgrid ID", required=True) + parser.add_argument( + "--mid", + type=int, + help="Microgrid ID", + required=True, + ) parser.add_argument( "--cid", nargs="+", From 8a21fb58fe36894199e133c162df3237b811ef26 Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:55:55 +0100 Subject: [PATCH 4/5] Move CLI tool to dedicated folder Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- pyproject.toml | 2 +- src/frequenz/client/reporting/{ => cli}/__main__.py | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/frequenz/client/reporting/{ => cli}/__main__.py (100%) diff --git a/pyproject.toml b/pyproject.toml index 6a0a0f5..b1af7df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,7 @@ dependencies = [ dynamic = ["version"] [project.scripts] -reporting-cli = "frequenz.client.reporting.__main__:main" +reporting-cli = "frequenz.client.reporting.cli.__main__:main" [[project.authors]] name = "Frequenz Energy-as-a-Service GmbH" diff --git a/src/frequenz/client/reporting/__main__.py b/src/frequenz/client/reporting/cli/__main__.py similarity index 100% rename from src/frequenz/client/reporting/__main__.py rename to src/frequenz/client/reporting/cli/__main__.py From 91e0df80e16db78760d2bc07f932973b492523d4 Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:09:46 +0100 Subject: [PATCH 5/5] Update release notes Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- RELEASE_NOTES.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index f80f12e..e49f72e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -6,10 +6,15 @@ ## Upgrading +* The CLI tool is moved to dedicated folder. + ## New Features +* Support data requests for multiple component IDs from command line. +* Support for passing formula strings as a `cid` argument from the command line. + ## Bug Fixes