1515
1616"""Module to read the esdl profiles from an energy system."""
1717import logging
18+ from datetime import datetime
19+ from typing import cast
1820
1921import esdl
2022import pandas as pd
2123from esdl .esdl_handler import EnergySystemHandler
22- from esdl .profiles .influxdbprofilemanager import InfluxDBProfileManager
24+ from esdl .profiles .influxdbprofilemanager import ConnectionSettings , InfluxDBProfileManager
2325from esdl .units .conversion import ENERGY_IN_J , POWER_IN_W , convert_to_unit
2426
2527logger = 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+
2852def 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