Skip to content

Commit 52acb2e

Browse files
authored
Merge pull request #346 from powerapi-ng/feat/socketdb-host-parameter
feat(database/socket): Allow the socket listen address to be configurable
2 parents 860ee60 + c863617 commit 52acb2e

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

src/powerapi/cli/common_cli_parsing_manager.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ def __init__(self):
118118

119119
subparser_socket_input = SubgroupConfigParsingManager("socket")
120120
subparser_socket_input.add_argument(
121-
"p", "port", argument_type=int, help_text="specify port to bind the socket"
121+
"h", "host", help_text="Specify the host the socket should listen on", default_value='127.0.0.1'
122+
)
123+
subparser_socket_input.add_argument(
124+
"p", "port", help_text="Specify the port the socket should listen on", argument_type=int, default_value=9080,
122125
)
123126
subparser_socket_input.add_argument(
124127
"n", "name", help_text="specify puller name", default_value="puller_socket"

src/powerapi/cli/generator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def __init__(self, component_group_name: str):
172172
self.db_factory = {
173173
'mongodb': lambda db_config: MongoDB(report_type=db_config['model'], uri=db_config['uri'],
174174
db_name=db_config['db'], collection_name=db_config['collection']),
175-
'socket': lambda db_config: SocketDB(report_type=db_config['model'], port=db_config['port']),
175+
'socket': lambda db_config: SocketDB(db_config['model'], db_config['host'], db_config['port']),
176176
'csv': lambda db_config: CsvDB(report_type=db_config['model'], tags=gen_tag_list(db_config),
177177
current_path=os.getcwd() if 'directory' not in db_config else db_config[
178178
'directory'],

src/powerapi/database/socket_db.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ class SocketDB(BaseDB):
6868
Database that act as a server that expose a socket where data source will push data
6969
"""
7070

71-
def __init__(self, report_type: Type[Report], port: int):
71+
def __init__(self, report_type: Type[Report], host: str, port: int):
7272
BaseDB.__init__(self, report_type, is_async=True)
7373
self.queue = None
74+
self.host = host
7475
self.port = port
7576
self.server = None
7677

@@ -79,7 +80,7 @@ async def connect(self):
7980
Connect to the socket database.
8081
"""
8182
self.queue = asyncio.Queue()
82-
self.server = await asyncio.start_server(self._gen_server_callback(), host='127.0.0.1', port=self.port)
83+
self.server = await asyncio.start_server(self._gen_server_callback(), host=self.host, port=self.port)
8384

8485
async def disconnect(self):
8586
"""

src/powerapi/report/report.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,23 @@ def to_json(report: Report) -> Dict:
109109

110110
@staticmethod
111111
def _extract_timestamp(ts):
112+
# Unix timestamp format (in milliseconds)
113+
if isinstance(ts, int):
114+
return datetime.fromtimestamp(ts / 1000)
115+
116+
# datetime object
117+
if isinstance(ts, datetime):
118+
return ts
119+
112120
if isinstance(ts, str):
113121
try:
122+
# ISO 8601 date format
114123
return datetime.strptime(ts, "%Y-%m-%dT%H:%M:%S.%f")
115124
except ValueError:
125+
# Unix timestamp format (in milliseconds)
116126
return datetime.fromtimestamp(int(ts) / 1000)
117-
if isinstance(ts, datetime):
118-
return ts
119127

120-
raise ValueError('timestamp must be a datetime.datetime or a string')
128+
raise ValueError('Invalid timestamp format')
121129

122130
@staticmethod
123131
def create_empty_report():

tests/utils/cli/several_inputs_outputs_stream_mode_enabled_configuration.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"puller3": {
2020
"model": "HWPCReport",
2121
"type": "socket",
22+
"host": "localhost",
2223
"port": 1111,
2324
"name": "puller3"
2425
}

0 commit comments

Comments
 (0)