Skip to content

Commit 1a6ad99

Browse files
committed
Add new metrics package compatible with API v0.8
The old `metric` package (not the singular) is based on the old API v0.5. Since the package name didn't match the API naming scheme, we add a new package with the right name to keep backwards compatibility. These classes are based on the microgrid API client v0.18: https://github.com/frequenz-floss/frequenz-client-microgrid-python/tree/v0.18.0/src/frequenz/client/microgrid/metrics Only some minor changes were applied to make it closer to the API names, for example, `MetricSample.sampled_at` was renamed to `.sample_time` to match the new name. Also the `MetricConnection` and `MetricConnectionCategory` classes were added. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a0baad7 commit 1a6ad99

File tree

13 files changed

+1412
-0
lines changed

13 files changed

+1412
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# License: MIT
2+
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Metrics definitions."""
5+
6+
from ._bounds import Bounds
7+
from ._metric import Metric
8+
from ._sample import (
9+
AggregatedMetricValue,
10+
AggregationMethod,
11+
MetricConnection,
12+
MetricConnectionCategory,
13+
MetricSample,
14+
)
15+
16+
__all__ = [
17+
"AggregatedMetricValue",
18+
"AggregationMethod",
19+
"Bounds",
20+
"Metric",
21+
"MetricConnection",
22+
"MetricConnectionCategory",
23+
"MetricSample",
24+
]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
5+
"""Definitions for bounds."""
6+
7+
import dataclasses
8+
9+
10+
@dataclasses.dataclass(frozen=True, kw_only=True)
11+
class Bounds:
12+
"""A set of lower and upper bounds for any metric.
13+
14+
The lower bound must be less than or equal to the upper bound.
15+
16+
The units of the bounds are always the same as the related metric.
17+
"""
18+
19+
lower: float | None = None
20+
"""The lower bound.
21+
22+
If `None`, there is no lower bound.
23+
"""
24+
25+
upper: float | None = None
26+
"""The upper bound.
27+
28+
If `None`, there is no upper bound.
29+
"""
30+
31+
def __post_init__(self) -> None:
32+
"""Validate these bounds."""
33+
if self.lower is None:
34+
return
35+
if self.upper is None:
36+
return
37+
if self.lower > self.upper:
38+
raise ValueError(
39+
f"Lower bound ({self.lower}) must be less than or equal to upper "
40+
f"bound ({self.upper})"
41+
)
42+
43+
def __str__(self) -> str:
44+
"""Return a string representation of these bounds."""
45+
return f"[{self.lower}, {self.upper}]"
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Supported metrics for microgrid components."""
5+
6+
7+
import enum
8+
9+
from frequenz.api.common.v1alpha8.metrics import metrics_pb2
10+
11+
12+
@enum.unique
13+
class Metric(enum.Enum):
14+
"""List of supported metrics.
15+
16+
Note: AC energy metrics information
17+
- This energy metric is reported directly from the component, and not a
18+
result of aggregations in our systems. If a component does not have this
19+
metric, this field cannot be populated.
20+
21+
- Components that provide energy metrics reset this metric from time to
22+
time. This behaviour is specific to each component model. E.g., some
23+
components reset it on UTC 00:00:00.
24+
25+
- This energy metric does not specify the start time of the accumulation
26+
period,and therefore can be inconsistent.
27+
"""
28+
29+
UNSPECIFIED = metrics_pb2.METRIC_UNSPECIFIED
30+
"""The metric is unspecified (this should not be used)."""
31+
32+
DC_VOLTAGE = metrics_pb2.METRIC_DC_VOLTAGE
33+
"""The direct current voltage."""
34+
35+
DC_CURRENT = metrics_pb2.METRIC_DC_CURRENT
36+
"""The direct current current."""
37+
38+
DC_POWER = metrics_pb2.METRIC_DC_POWER
39+
"""The direct current power."""
40+
41+
AC_FREQUENCY = metrics_pb2.METRIC_AC_FREQUENCY
42+
"""The alternating current frequency."""
43+
44+
AC_VOLTAGE = metrics_pb2.METRIC_AC_VOLTAGE
45+
"""The alternating current electric potential difference."""
46+
47+
AC_VOLTAGE_PHASE_1_N = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_1_N
48+
"""The alternating current electric potential difference between phase 1 and neutral."""
49+
50+
AC_VOLTAGE_PHASE_2_N = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_2_N
51+
"""The alternating current electric potential difference between phase 2 and neutral."""
52+
53+
AC_VOLTAGE_PHASE_3_N = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_3_N
54+
"""The alternating current electric potential difference between phase 3 and neutral."""
55+
56+
AC_VOLTAGE_PHASE_1_PHASE_2 = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_1_PHASE_2
57+
"""The alternating current electric potential difference between phase 1 and phase 2."""
58+
59+
AC_VOLTAGE_PHASE_2_PHASE_3 = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_2_PHASE_3
60+
"""The alternating current electric potential difference between phase 2 and phase 3."""
61+
62+
AC_VOLTAGE_PHASE_3_PHASE_1 = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_3_PHASE_1
63+
"""The alternating current electric potential difference between phase 3 and phase 1."""
64+
65+
AC_CURRENT = metrics_pb2.METRIC_AC_CURRENT
66+
"""The alternating current current."""
67+
68+
AC_CURRENT_PHASE_1 = metrics_pb2.METRIC_AC_CURRENT_PHASE_1
69+
"""The alternating current current in phase 1."""
70+
71+
AC_CURRENT_PHASE_2 = metrics_pb2.METRIC_AC_CURRENT_PHASE_2
72+
"""The alternating current current in phase 2."""
73+
74+
AC_CURRENT_PHASE_3 = metrics_pb2.METRIC_AC_CURRENT_PHASE_3
75+
"""The alternating current current in phase 3."""
76+
77+
AC_POWER_APPARENT = metrics_pb2.METRIC_AC_POWER_APPARENT
78+
"""The alternating current apparent power."""
79+
80+
AC_POWER_APPARENT_PHASE_1 = metrics_pb2.METRIC_AC_POWER_APPARENT_PHASE_1
81+
"""The alternating current apparent power in phase 1."""
82+
83+
AC_POWER_APPARENT_PHASE_2 = metrics_pb2.METRIC_AC_POWER_APPARENT_PHASE_2
84+
"""The alternating current apparent power in phase 2."""
85+
86+
AC_POWER_APPARENT_PHASE_3 = metrics_pb2.METRIC_AC_POWER_APPARENT_PHASE_3
87+
"""The alternating current apparent power in phase 3."""
88+
89+
AC_POWER_ACTIVE = metrics_pb2.METRIC_AC_POWER_ACTIVE
90+
"""The alternating current active power."""
91+
92+
AC_POWER_ACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_POWER_ACTIVE_PHASE_1
93+
"""The alternating current active power in phase 1."""
94+
95+
AC_POWER_ACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_POWER_ACTIVE_PHASE_2
96+
"""The alternating current active power in phase 2."""
97+
98+
AC_POWER_ACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_POWER_ACTIVE_PHASE_3
99+
"""The alternating current active power in phase 3."""
100+
101+
AC_POWER_REACTIVE = metrics_pb2.METRIC_AC_POWER_REACTIVE
102+
"""The alternating current reactive power."""
103+
104+
AC_POWER_REACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_POWER_REACTIVE_PHASE_1
105+
"""The alternating current reactive power in phase 1."""
106+
107+
AC_POWER_REACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_POWER_REACTIVE_PHASE_2
108+
"""The alternating current reactive power in phase 2."""
109+
110+
AC_POWER_REACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_POWER_REACTIVE_PHASE_3
111+
"""The alternating current reactive power in phase 3."""
112+
113+
AC_POWER_FACTOR = metrics_pb2.METRIC_AC_POWER_FACTOR
114+
"""The alternating current power factor."""
115+
116+
AC_POWER_FACTOR_PHASE_1 = metrics_pb2.METRIC_AC_POWER_FACTOR_PHASE_1
117+
"""The alternating current power factor in phase 1."""
118+
119+
AC_POWER_FACTOR_PHASE_2 = metrics_pb2.METRIC_AC_POWER_FACTOR_PHASE_2
120+
"""The alternating current power factor in phase 2."""
121+
122+
AC_POWER_FACTOR_PHASE_3 = metrics_pb2.METRIC_AC_POWER_FACTOR_PHASE_3
123+
"""The alternating current power factor in phase 3."""
124+
125+
AC_ENERGY_APPARENT = metrics_pb2.METRIC_AC_ENERGY_APPARENT
126+
"""The alternating current apparent energy."""
127+
128+
AC_ENERGY_APPARENT_PHASE_1 = metrics_pb2.METRIC_AC_ENERGY_APPARENT_PHASE_1
129+
"""The alternating current apparent energy in phase 1."""
130+
131+
AC_ENERGY_APPARENT_PHASE_2 = metrics_pb2.METRIC_AC_ENERGY_APPARENT_PHASE_2
132+
"""The alternating current apparent energy in phase 2."""
133+
134+
AC_ENERGY_APPARENT_PHASE_3 = metrics_pb2.METRIC_AC_ENERGY_APPARENT_PHASE_3
135+
"""The alternating current apparent energy in phase 3."""
136+
137+
AC_ENERGY_ACTIVE = metrics_pb2.METRIC_AC_ENERGY_ACTIVE
138+
"""The alternating current active energy."""
139+
140+
AC_ENERGY_ACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_PHASE_1
141+
"""The alternating current active energy in phase 1."""
142+
143+
AC_ENERGY_ACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_PHASE_2
144+
"""The alternating current active energy in phase 2."""
145+
146+
AC_ENERGY_ACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_PHASE_3
147+
"""The alternating current active energy in phase 3."""
148+
149+
AC_ENERGY_ACTIVE_CONSUMED = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED
150+
"""The alternating current active energy consumed."""
151+
152+
AC_ENERGY_ACTIVE_CONSUMED_PHASE_1 = (
153+
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED_PHASE_1
154+
)
155+
"""The alternating current active energy consumed in phase 1."""
156+
157+
AC_ENERGY_ACTIVE_CONSUMED_PHASE_2 = (
158+
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED_PHASE_2
159+
)
160+
"""The alternating current active energy consumed in phase 2."""
161+
162+
AC_ENERGY_ACTIVE_CONSUMED_PHASE_3 = (
163+
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED_PHASE_3
164+
)
165+
"""The alternating current active energy consumed in phase 3."""
166+
167+
AC_ENERGY_ACTIVE_DELIVERED = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED
168+
"""The alternating current active energy delivered."""
169+
170+
AC_ENERGY_ACTIVE_DELIVERED_PHASE_1 = (
171+
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED_PHASE_1
172+
)
173+
"""The alternating current active energy delivered in phase 1."""
174+
175+
AC_ENERGY_ACTIVE_DELIVERED_PHASE_2 = (
176+
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED_PHASE_2
177+
)
178+
"""The alternating current active energy delivered in phase 2."""
179+
180+
AC_ENERGY_ACTIVE_DELIVERED_PHASE_3 = (
181+
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED_PHASE_3
182+
)
183+
"""The alternating current active energy delivered in phase 3."""
184+
185+
AC_ENERGY_REACTIVE = metrics_pb2.METRIC_AC_ENERGY_REACTIVE
186+
"""The alternating current reactive energy."""
187+
188+
AC_ENERGY_REACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_ENERGY_REACTIVE_PHASE_1
189+
"""The alternating current reactive energy in phase 1."""
190+
191+
AC_ENERGY_REACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_ENERGY_REACTIVE_PHASE_2
192+
"""The alternating current reactive energy in phase 2."""
193+
194+
AC_ENERGY_REACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_ENERGY_REACTIVE_PHASE_3
195+
"""The alternating current reactive energy in phase 3."""
196+
197+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT = (
198+
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT
199+
)
200+
"""The alternating current total harmonic distortion current."""
201+
202+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_1 = (
203+
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_1
204+
)
205+
"""The alternating current total harmonic distortion current in phase 1."""
206+
207+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_2 = (
208+
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_2
209+
)
210+
"""The alternating current total harmonic distortion current in phase 2."""
211+
212+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_3 = (
213+
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_3
214+
)
215+
"""The alternating current total harmonic distortion current in phase 3."""
216+
217+
BATTERY_CAPACITY = metrics_pb2.METRIC_BATTERY_CAPACITY
218+
"""The capacity of the battery."""
219+
220+
BATTERY_SOC_PCT = metrics_pb2.METRIC_BATTERY_SOC_PCT
221+
"""The state of charge of the battery as a percentage."""
222+
223+
BATTERY_TEMPERATURE = metrics_pb2.METRIC_BATTERY_TEMPERATURE
224+
"""The temperature of the battery."""
225+
226+
INVERTER_TEMPERATURE = metrics_pb2.METRIC_INVERTER_TEMPERATURE
227+
"""The temperature of the inverter."""
228+
229+
INVERTER_TEMPERATURE_CABINET = metrics_pb2.METRIC_INVERTER_TEMPERATURE_CABINET
230+
"""The temperature of the inverter cabinet."""
231+
232+
INVERTER_TEMPERATURE_HEATSINK = metrics_pb2.METRIC_INVERTER_TEMPERATURE_HEATSINK
233+
"""The temperature of the inverter heatsink."""
234+
235+
INVERTER_TEMPERATURE_TRANSFORMER = (
236+
metrics_pb2.METRIC_INVERTER_TEMPERATURE_TRANSFORMER
237+
)
238+
"""The temperature of the inverter transformer."""
239+
240+
EV_CHARGER_TEMPERATURE = metrics_pb2.METRIC_EV_CHARGER_TEMPERATURE
241+
"""The temperature of the EV charger."""
242+
243+
SENSOR_WIND_SPEED = metrics_pb2.METRIC_SENSOR_WIND_SPEED
244+
"""The speed of the wind measured."""
245+
246+
SENSOR_WIND_DIRECTION = metrics_pb2.METRIC_SENSOR_WIND_DIRECTION
247+
"""The direction of the wind measured."""
248+
249+
SENSOR_TEMPERATURE = metrics_pb2.METRIC_SENSOR_TEMPERATURE
250+
"""The temperature measured."""
251+
252+
SENSOR_RELATIVE_HUMIDITY = metrics_pb2.METRIC_SENSOR_RELATIVE_HUMIDITY
253+
"""The relative humidity measured."""
254+
255+
SENSOR_DEW_POINT = metrics_pb2.METRIC_SENSOR_DEW_POINT
256+
"""The dew point measured."""
257+
258+
SENSOR_AIR_PRESSURE = metrics_pb2.METRIC_SENSOR_AIR_PRESSURE
259+
"""The air pressure measured."""
260+
261+
SENSOR_IRRADIANCE = metrics_pb2.METRIC_SENSOR_IRRADIANCE
262+
"""The irradiance measured."""

0 commit comments

Comments
 (0)