|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""A highlevel interface for the reporting API.""" |
| 5 | + |
| 6 | +from collections import namedtuple |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +from frequenz.client.common.metric import Metric |
| 10 | +from frequenz.client.reporting import ReportingApiClient |
| 11 | + |
| 12 | +CumulativeEnergy = namedtuple( |
| 13 | + "CumulativeEnergy", ["start_time", "end_time", "consumption", "production"] |
| 14 | +) |
| 15 | +"""Type for cumulative energy consumption and production over a specified time.""" |
| 16 | + |
| 17 | + |
| 18 | +# pylint: disable=too-many-arguments |
| 19 | +async def cumulative_energy( |
| 20 | + client: ReportingApiClient, |
| 21 | + microgrid_id: int, |
| 22 | + component_id: int, |
| 23 | + start_time: datetime, |
| 24 | + end_time: datetime, |
| 25 | + use_active_power: bool, |
| 26 | + resolution: int | None = None, |
| 27 | +) -> CumulativeEnergy: |
| 28 | + """ |
| 29 | + Calculate the cumulative energy consumption and production over a specified time range. |
| 30 | +
|
| 31 | + Args: |
| 32 | + client: The client used to fetch the metric samples from the Reporting API. |
| 33 | + microgrid_id: The ID of the microgrid. |
| 34 | + component_id: The ID of the component within the microgrid. |
| 35 | + start_time: The start date and time for the period. |
| 36 | + end_time: The end date and time for the period. |
| 37 | + use_active_power: If True, use the 'AC_ACTIVE_POWER' metric. |
| 38 | + If False, use the 'AC_ACTIVE_ENERGY' metric. |
| 39 | + resolution: The resampling resolution for the data, represented in seconds. |
| 40 | + If None, no resampling is applied. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + EnergyMetric: A named tuple with start_time, end_time, consumption, and production |
| 44 | + in Wh. |
| 45 | + """ |
| 46 | + metric = Metric.AC_ACTIVE_POWER if use_active_power else Metric.AC_ACTIVE_ENERGY |
| 47 | + |
| 48 | + metric_samples = [ |
| 49 | + sample |
| 50 | + async for sample in client.list_microgrid_components_data( |
| 51 | + microgrid_components=[(microgrid_id, [component_id])], |
| 52 | + metrics=metric, |
| 53 | + start_dt=start_time, |
| 54 | + end_dt=end_time, |
| 55 | + resolution=resolution, |
| 56 | + ) |
| 57 | + ] |
| 58 | + |
| 59 | + if metric_samples: |
| 60 | + if use_active_power: |
| 61 | + # Convert power to energy if using AC_ACTIVE_POWER |
| 62 | + consumption = ( |
| 63 | + sum( |
| 64 | + m1.value * (m2.timestamp - m1.timestamp).total_seconds() |
| 65 | + for m1, m2 in zip(metric_samples, metric_samples[1:]) |
| 66 | + if m1.value > 0 |
| 67 | + ) |
| 68 | + / 3600.0 |
| 69 | + ) # Convert seconds to hours |
| 70 | + |
| 71 | + production = ( |
| 72 | + sum( |
| 73 | + m1.value * (m2.timestamp - m1.timestamp).total_seconds() |
| 74 | + for m1, m2 in zip(metric_samples, metric_samples[1:]) |
| 75 | + if m1.value < 0 |
| 76 | + ) |
| 77 | + / 3600.0 |
| 78 | + ) |
| 79 | + else: |
| 80 | + # Directly use energy values if using AC_ACTIVE_ENERGY |
| 81 | + consumption = sum( |
| 82 | + m2.value - m1.value |
| 83 | + for m1, m2 in zip(metric_samples, metric_samples[1:]) |
| 84 | + if m1.value > 0 |
| 85 | + ) |
| 86 | + production = sum( |
| 87 | + m2.value - m1.value |
| 88 | + for m1, m2 in zip(metric_samples, metric_samples[1:]) |
| 89 | + if m1.value < 0 |
| 90 | + ) |
| 91 | + else: |
| 92 | + consumption = production = 0.0 |
| 93 | + |
| 94 | + return CumulativeEnergy( |
| 95 | + start_time=start_time, |
| 96 | + end_time=end_time, |
| 97 | + consumption=consumption, |
| 98 | + production=production, |
| 99 | + ) |
0 commit comments