Skip to content

Commit b991b74

Browse files
committed
Fixed influxdb importer so it can now take profiles from the optimizer.
1 parent 8b21628 commit b991b74

2 files changed

Lines changed: 51 additions & 13 deletions

File tree

src/omotes_simulator_core/entities/assets/esdl_asset_object.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def get_out_port_profile(self) -> pd.DataFrame:
106106
for port in self.esdl_asset.port:
107107
if isinstance(port, esdl.OutPort) and port.profile.items:
108108
for profile in port.profile:
109-
if profile.field == "HeatIn.Q":
109+
if profile.field == "Heat_flow":
110110
return get_data_from_profile(profile)
111111
logger.error(
112112
f"No profile found for asset: {self.esdl_asset.name}",
@@ -233,7 +233,7 @@ def has_out_optimizer_profile(self) -> bool:
233233
try:
234234
data_source = profile.dataSource
235235
if data_source is not None:
236-
if data_source.attribution == "optimizer":
236+
if data_source.name == "Optimizer":
237237
return True
238238
except AttributeError:
239239
continue

src/omotes_simulator_core/entities/utility/influxdb_reader.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,40 @@
1515

1616
"""Module to read the esdl profiles from an energy system."""
1717
import logging
18+
from datetime import datetime
19+
from typing import cast
1820

1921
import esdl
2022
import pandas as pd
2123
from esdl.esdl_handler import EnergySystemHandler
22-
from esdl.profiles.influxdbprofilemanager import InfluxDBProfileManager
24+
from esdl.profiles.influxdbprofilemanager import ConnectionSettings, InfluxDBProfileManager
2325
from esdl.units.conversion import ENERGY_IN_J, POWER_IN_W, convert_to_unit
2426

2527
logger = logging.getLogger(__name__)
2628

2729

30+
def _normalize_influx_filters(filters: str | None) -> list[dict[str, str]]:
31+
"""Parse and normalize filters to the format expected by load_influxdb.
32+
33+
The upstream parser may return dictionaries with a `key` field while
34+
load_influxdb expects `tag`. This shim keeps compatibility across versions.
35+
"""
36+
parsed_filters = InfluxDBProfileManager._parse_esdl_profile_filters(filters)
37+
normalized_filters: list[dict[str, str]] = []
38+
for influx_filter in parsed_filters:
39+
tag = influx_filter.get("tag") or influx_filter.get("key")
40+
value = influx_filter.get("value")
41+
if not tag or value is None:
42+
continue
43+
normalized_filters.append(
44+
{
45+
"tag": str(tag),
46+
"value": str(value).strip().strip("\"").strip("'"),
47+
}
48+
)
49+
return normalized_filters
50+
51+
2852
def parse_esdl_profiles(esh: EnergySystemHandler) -> dict[str, pd.DataFrame]:
2953
"""Method to parse the esdl profiles from an energy system.
3054
@@ -48,33 +72,47 @@ def get_data_from_profile(esdl_profile: esdl.InfluxDBProfile) -> pd.DataFrame:
4872
:return: pandas.DataFrame with the data
4973
"""
5074
influx_cred_map: dict[str, tuple[str, str]] = {}
51-
profile_host = esdl_profile.host
75+
profile_host = str(esdl_profile.host)
76+
profile_port = int(esdl_profile.port)
77+
profile_database = str(esdl_profile.database)
78+
profile_measurement = str(esdl_profile.measurement)
79+
profile_field = str(esdl_profile.field)
5280
ssl_setting = False
5381
if "https" in profile_host:
5482
profile_host = profile_host[8:]
5583
ssl_setting = True
5684
elif "http" in profile_host:
5785
profile_host = profile_host[7:]
5886
# why is this here?
59-
if esdl_profile.port == 443:
87+
if profile_port == 443:
6088
ssl_setting = True
6189

62-
influx_host = f"{profile_host}:{esdl_profile.port}"
90+
influx_host = f"{profile_host}:{profile_port}"
6391
if influx_host in influx_cred_map:
6492
(username, password) = influx_cred_map[influx_host]
6593
else:
6694
username = None
6795
password = None
68-
time_series_data = InfluxDBProfileManager.create_esdl_influxdb_profile_manager(
69-
esdl_profile,
70-
username,
71-
password,
72-
ssl_setting,
73-
ssl_setting,
96+
conn_settings = ConnectionSettings(
97+
host=profile_host,
98+
port=profile_port,
99+
database=profile_database,
100+
username=username or "",
101+
password=password or "",
102+
ssl=ssl_setting,
103+
verify_ssl=ssl_setting,
104+
)
105+
time_series_data = InfluxDBProfileManager(conn_settings)
106+
time_series_data.load_influxdb(
107+
measurement=profile_measurement,
108+
fields=[profile_field],
109+
from_datetime=cast(datetime, esdl_profile.startDate),
110+
to_datetime=cast(datetime, esdl_profile.endDate),
111+
filters=_normalize_influx_filters(str(esdl_profile.filters) if esdl_profile.filters else None),
74112
)
75113
# Error check start and end dates of profiles
76114

77-
# I do not thing this is required since you set it in mapeditor.
115+
# I do not think this is required since you set it in mapeditor.
78116
if time_series_data.end_datetime != esdl_profile.endDate:
79117
logger.error(
80118
f"The user input profile end datetime: {esdl_profile.endDate} does not match the end"

0 commit comments

Comments
 (0)