Skip to content

Commit 318b643

Browse files
committed
Move ComponentMetricRequest structure to avoid cyclic imports
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 31e2f58 commit 318b643

File tree

4 files changed

+55
-47
lines changed

4 files changed

+55
-47
lines changed

src/frequenz/sdk/actor/_data_sourcing/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
"""The DataSourcingActor."""
55

6+
from ._component_metric_request import ComponentMetricRequest
67
from .data_sourcing import DataSourcingActor
7-
from .microgrid_api_source import ComponentMetricRequest
88

99
__all__ = [
1010
"ComponentMetricRequest",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""The ComponentMetricRequest class."""
5+
6+
from dataclasses import dataclass
7+
from datetime import datetime
8+
9+
from ...microgrid.component._component import ComponentMetricId
10+
11+
12+
@dataclass
13+
class ComponentMetricRequest:
14+
"""A request object to start streaming a metric for a component."""
15+
16+
namespace: str
17+
"""The namespace that this request belongs to.
18+
19+
Metric requests with a shared namespace enable the reuse of channels within
20+
that namespace.
21+
22+
If for example, an actor making a multiple requests, uses the name of the
23+
actor as the namespace, then requests from the actor will get reused when
24+
possible.
25+
"""
26+
27+
component_id: int
28+
"""The ID of the requested component."""
29+
30+
metric_id: ComponentMetricId
31+
"""The ID of the requested component's metric."""
32+
33+
start_time: datetime | None
34+
"""The start time from which data is required.
35+
36+
When None, we will stream only live data.
37+
"""
38+
39+
def get_channel_name(self) -> str:
40+
"""Return a channel name constructed from Self.
41+
42+
This channel name can be used by the sending side and receiving sides to
43+
identify the right channel from the ChannelRegistry.
44+
45+
Returns:
46+
A string denoting a channel name.
47+
"""
48+
return (
49+
f"component-stream::{self.component_id}::{self.metric_id.name}::"
50+
f"{self.start_time}::{self.namespace}"
51+
)

src/frequenz/sdk/actor/_data_sourcing/data_sourcing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
from .._actor import Actor
99
from .._channel_registry import ChannelRegistry
10-
from .microgrid_api_source import ComponentMetricRequest, MicrogridApiSource
10+
from ._component_metric_request import ComponentMetricRequest
11+
from .microgrid_api_source import MicrogridApiSource
1112

1213

1314
class DataSourcingActor(Actor):

src/frequenz/sdk/actor/_data_sourcing/microgrid_api_source.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import asyncio
77
import logging
8-
from dataclasses import dataclass
9-
from datetime import datetime
108
from typing import Any, Callable, Dict, List, Optional, Tuple
119

1210
from frequenz.channels import Receiver, Sender
@@ -23,49 +21,7 @@
2321
from ...timeseries import Sample
2422
from ...timeseries._quantities import Quantity
2523
from .._channel_registry import ChannelRegistry
26-
27-
28-
@dataclass
29-
class ComponentMetricRequest:
30-
"""A request object to start streaming a metric for a component."""
31-
32-
namespace: str
33-
"""The namespace that this request belongs to.
34-
35-
Metric requests with a shared namespace enable the reuse of channels within
36-
that namespace.
37-
38-
If for example, an actor making a multiple requests, uses the name of the
39-
actor as the namespace, then requests from the actor will get reused when
40-
possible.
41-
"""
42-
43-
component_id: int
44-
"""The ID of the requested component."""
45-
46-
metric_id: ComponentMetricId
47-
"""The ID of the requested component's metric."""
48-
49-
start_time: Optional[datetime]
50-
"""The start time from which data is required.
51-
52-
When None, we will stream only live data.
53-
"""
54-
55-
def get_channel_name(self) -> str:
56-
"""Return a channel name constructed from Self.
57-
58-
This channel name can be used by the sending side and receiving sides to
59-
identify the right channel from the ChannelRegistry.
60-
61-
Returns:
62-
A string denoting a channel name.
63-
"""
64-
return (
65-
f"component-stream::{self.component_id}::{self.metric_id.name}::"
66-
f"{self.start_time}::{self.namespace}"
67-
)
68-
24+
from ._component_metric_request import ComponentMetricRequest
6925

7026
_MeterDataMethods: Dict[ComponentMetricId, Callable[[MeterData], float]] = {
7127
ComponentMetricId.ACTIVE_POWER: lambda msg: msg.active_power,

0 commit comments

Comments
 (0)