Skip to content

Commit df5fec8

Browse files
authored
Install example command line tool as reporting-cli (#69)
* Install command line tool as `reporting-cli`: The command line tool from the example is moved to the package and can be called via `reporting-cli` or `python -m frequenz.client.reporting` from the command line. * The command-line argument `--display` is renamed to `--format` for `reporting-cli`. * The `reporting-cli` tool can output CSV format, dataframe output is removed.
2 parents f47bd29 + e79b16c commit df5fec8

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

RELEASE_NOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
## Upgrading
88

9+
* Install command line tool as `reporting-cli`: The command line tool from the example is moved
10+
to the package and can be called via `reporting-cli` or `python -m frequenz.client.reporting`
11+
from the command line.
12+
* The command-line argument `--display` is renamed to `--format` for `reporting-cli`.
13+
* The `reporting-cli` tool can output CSV format, dataframe output is removed.
14+
915
## New Features
1016

1117
## Bug Fixes

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ dependencies = [
3636
]
3737
dynamic = ["version"]
3838

39+
[project.scripts]
40+
reporting-cli = "frequenz.client.reporting.__main__:main"
41+
3942
[[project.authors]]
4043
name = "Frequenz Energy-as-a-Service GmbH"
4144

examples/client.py renamed to src/frequenz/client/reporting/__main__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
from pprint import pprint
1010
from typing import AsyncIterator
1111

12-
import pandas as pd
1312
from frequenz.client.common.metric import Metric
1413

1514
from frequenz.client.reporting import ReportingApiClient
16-
17-
# Experimental import
1815
from frequenz.client.reporting._client import MetricSample
1916

2017

@@ -52,7 +49,7 @@ def main() -> None:
5249
parser.add_argument("--resolution", type=int, help="Resolution", default=None)
5350
parser.add_argument("--psize", type=int, help="Page size", default=1000)
5451
parser.add_argument(
55-
"--display", choices=["iter", "df", "dict"], help="Display format", default="df"
52+
"--format", choices=["iter", "csv", "dict"], help="Output format", default="csv"
5653
)
5754
parser.add_argument(
5855
"--key",
@@ -72,7 +69,7 @@ def main() -> None:
7269
page_size=args.psize,
7370
service_address=args.url,
7471
key=args.key,
75-
display=args.display,
72+
fmt=args.format,
7673
)
7774
)
7875

@@ -88,7 +85,7 @@ async def run(
8885
page_size: int,
8986
service_address: str,
9087
key: str,
91-
display: str,
88+
fmt: str,
9289
) -> None:
9390
"""Test the ReportingApiClient.
9491
@@ -102,10 +99,10 @@ async def run(
10299
page_size: page size
103100
service_address: service address
104101
key: API key
105-
display: display format
102+
fmt: output format
106103
107104
Raises:
108-
ValueError: if display format is invalid
105+
ValueError: if output format is invalid
109106
"""
110107
client = ReportingApiClient(service_address, key)
111108

@@ -129,26 +126,25 @@ def data_iter() -> AsyncIterator[MetricSample]:
129126
page_size=page_size,
130127
)
131128

132-
if display == "iter":
129+
if fmt == "iter":
133130
# Iterate over single metric generator
134131
async for sample in data_iter():
135132
print(sample)
136133

137-
elif display == "dict":
134+
elif fmt == "dict":
138135
# Dumping all data as a single dict
139136
dct = await iter_to_dict(data_iter())
140137
pprint(dct)
141138

142-
elif display == "df":
143-
# Turn data into a pandas DataFrame
144-
data = [cd async for cd in data_iter()]
145-
df = pd.DataFrame(data).set_index("timestamp")
146-
# Set option to display all rows
147-
pd.set_option("display.max_rows", None)
148-
pprint(df)
139+
elif fmt == "csv":
140+
# Print header
141+
print(",".join(MetricSample._fields))
142+
# Iterate over single metric generator and format as CSV
143+
async for sample in data_iter():
144+
print(",".join(str(e) for e in sample))
149145

150146
else:
151-
raise ValueError(f"Invalid display format: {display}")
147+
raise ValueError(f"Invalid output format: {fmt}")
152148

153149
return
154150

0 commit comments

Comments
 (0)