Skip to content

Commit b14b10b

Browse files
authored
Merge pull request #11 from Enapter/feat/telemetry-aggregation
Historical telemetry aggregation control
2 parents 9342037 + a12f38d commit b14b10b

10 files changed

Lines changed: 40 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.codex
12
.gemini
23
.DS_Store
34

src/enapter_mcp_server/core/application_server.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,16 @@ async def get_historical_telemetry(
229229
time_from: datetime.datetime,
230230
time_to: datetime.datetime,
231231
granularity: int,
232+
aggregation: domain.AggregationFunction,
232233
) -> domain.HistoricalTelemetry:
233234
return await self._enapter_api.get_historical_telemetry(
234-
auth, device_id, attributes, time_from, time_to, granularity
235+
auth,
236+
device_id,
237+
attributes,
238+
time_from,
239+
time_to,
240+
granularity,
241+
aggregation,
235242
)
236243

237244
async def search_command_executions(

src/enapter_mcp_server/core/enapter_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ async def get_historical_telemetry(
5353
time_from: datetime.datetime,
5454
time_to: datetime.datetime,
5555
granularity: int,
56+
aggregation: domain.AggregationFunction,
5657
) -> domain.HistoricalTelemetry: ...

src/enapter_mcp_server/domain/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .aggregation_function import AggregationFunction
12
from .alert_declaration import AlertDeclaration
23
from .alert_severity import AlertSeverity
34
from .blueprint_section import BlueprintSection
@@ -19,6 +20,7 @@
1920
from .telemetry_attribute_declaration import TelemetryAttributeDeclaration
2021

2122
__all__ = [
23+
"AggregationFunction",
2224
"AlertDeclaration",
2325
"AlertSeverity",
2426
"BlueprintSection",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import enum
2+
3+
4+
class AggregationFunction(str, enum.Enum):
5+
AVG = "avg"
6+
MIN = "min"
7+
MAX = "max"
8+
LAST = "last"

src/enapter_mcp_server/http/enapter_api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ async def get_historical_telemetry(
100100
time_from: datetime.datetime,
101101
time_to: datetime.datetime,
102102
granularity: int,
103+
aggregation: domain.AggregationFunction,
103104
) -> domain.HistoricalTelemetry:
104105
async with self._new_client(auth) as client:
105106
telemetry = await client.telemetry.wide_timeseries(
@@ -108,7 +109,11 @@ async def get_historical_telemetry(
108109
granularity=granularity,
109110
selectors=[
110111
enapter.http.api.telemetry.Selector(
111-
device=device_id, attributes=attributes
112+
device=device_id,
113+
attributes=attributes,
114+
aggregation=enapter.http.api.telemetry.Aggregation(
115+
aggregation.value.upper()
116+
),
112117
)
113118
],
114119
)

src/enapter_mcp_server/mcp/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .aggregation_function import AggregationFunction
12
from .alert_declaration import AlertDeclaration
23
from .alert_severity import AlertSeverity
34
from .blueprint_section import BlueprintSection
@@ -18,6 +19,7 @@
1819
from .telemetry_attribute_declaration import TelemetryAttributeDeclaration
1920

2021
__all__ = [
22+
"AggregationFunction",
2123
"AlertDeclaration",
2224
"AlertSeverity",
2325
"BlueprintSection",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import Literal
2+
3+
AggregationFunction = Literal["avg", "min", "max", "last"]

src/enapter_mcp_server/mcp/server.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,15 @@ async def get_historical_telemetry(
302302
attributes: list[str],
303303
time_from: datetime.datetime,
304304
time_to: datetime.datetime,
305+
aggregation: models.AggregationFunction,
305306
granularity: int = 60 * 60,
306307
) -> models.HistoricalTelemetry:
307-
"""Retrieve aggregated telemetry data.
308+
"""Retrieve aggregated historical telemetry data for a specific device.
308309
309-
Most devices send telemetry data once per second. To reduce the amount
310-
of data transferred, the `granularity` parameter can be used to
311-
aggregate data over a specified interval (in seconds). For example, a
312-
granularity of 3600 seconds (1 hour) will return hourly averages of the
313-
telemetry data.
310+
The data is divided into time buckets of `granularity` seconds. Each
311+
returned timestamp marks the start of a bucket, and its values are the
312+
result of applying the `aggregation` function to all data points within
313+
that bucket.
314314
"""
315315
auth = await self._get_auth_config()
316316
telemetry = await self._app.get_historical_telemetry(
@@ -320,6 +320,7 @@ async def get_historical_telemetry(
320320
time_from=time_from,
321321
time_to=time_to,
322322
granularity=granularity,
323+
aggregation=domain.AggregationFunction(aggregation),
323324
)
324325
return models.HistoricalTelemetry.from_domain(telemetry)
325326

tests/unit/core/test_application_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ async def get_historical_telemetry(
102102
time_from: datetime.datetime,
103103
time_to: datetime.datetime,
104104
granularity: int,
105+
aggregation: domain.AggregationFunction,
105106
) -> domain.HistoricalTelemetry:
106107
if self._historical_telemetry is None:
107108
raise NotImplementedError()
@@ -745,6 +746,7 @@ async def test_get_historical_telemetry(self) -> None:
745746
datetime.datetime.now(),
746747
datetime.datetime.now(),
747748
60,
749+
domain.AggregationFunction.AVG,
748750
)
749751

750752
assert result == historical

0 commit comments

Comments
 (0)