Skip to content

Commit 7ebfa2e

Browse files
authored
Merge pull request #615 from powerapi-ng/refactor/cleanup-unused-report-attrs
refactor: Cleanup unused attributes in actor message types
2 parents 3ae78f7 + adec0e8 commit 7ebfa2e

File tree

15 files changed

+88
-158
lines changed

15 files changed

+88
-158
lines changed

src/powerapi/actor/actor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ def soft_kill(self):
286286
<powerapi.message.message.PoisonPillMessage>`
287287
288288
"""
289-
self.send_control(PoisonPillMessage(soft=True, sender_name='system'))
289+
self.send_control(PoisonPillMessage())
290290
self.socket_interface.close()
291291

292292
def hard_kill(self):
293293
"""Kill this actor by sending a hard :class:`PoisonPillMessage
294294
<powerapi.message.message.PoisonPillMessage>`
295295
296296
"""
297-
self.send_control(PoisonPillMessage(soft=False, sender_name='system'))
297+
self.send_control(PoisonPillMessage(soft=False))
298298
self.socket_interface.close()

src/powerapi/actor/supervisor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def launch_actor(self, actor, start_message=True):
107107
actor.connect_data()
108108

109109
if start_message:
110-
actor.send_control(StartMessage(SUPERVISOR_NAME))
110+
actor.send_control(StartMessage())
111111
msg = actor.receive_control(2000)
112112
if isinstance(msg, ErrorMessage):
113113
raise ActorInitError(msg.error_message)

src/powerapi/handler/start_handler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def handle(self, msg):
4444
:param powerapi.StartMessage msg: Message that initialize the actor
4545
"""
4646
if self.state.initialized:
47-
self.state.actor.send_control(
48-
ErrorMessage(self.state.actor.name, 'Actor already initialized'))
47+
self.state.actor.send_control(ErrorMessage('Actor already initialized'))
4948
return
5049

5150
if not isinstance(msg, StartMessage):
@@ -55,7 +54,7 @@ def handle(self, msg):
5554

5655
if self.state.alive:
5756
self.state.initialized = True
58-
self.state.actor.send_control(OKMessage(self.state.actor.name))
57+
self.state.actor.send_control(OKMessage())
5958

6059
def initialization(self):
6160
"""

src/powerapi/message.py

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,82 +30,41 @@
3030

3131
class Message:
3232
"""
33-
Abstract Message class
33+
Abstract actor Message class.
3434
"""
3535

36-
def __init__(self, sender_name: str):
37-
self.sender_name = sender_name
38-
39-
def __str__(self):
40-
raise NotImplementedError()
41-
4236

4337
class OKMessage(Message):
4438
"""
45-
Message sends to acknowledge last received message
39+
Message sent by an actor after a successful startup.
4640
"""
4741

48-
def __init__(self, sender_name: str):
49-
Message.__init__(self, sender_name)
50-
51-
def __str__(self):
52-
return "OKMessage"
53-
5442

5543
class ErrorMessage(Message):
5644
"""
57-
Message used to indicate that an error as occuried
45+
Message sent by an actor when an error occurs during startup.
5846
"""
5947

60-
def __init__(self, sender_name: str, error_message: str):
48+
def __init__(self, error_message: str):
6149
"""
62-
:param str error_code: message associated to the error
50+
:param error_message: Message associated to the encountered error.
6351
"""
64-
Message.__init__(self, sender_name)
6552
self.error_message = error_message
6653

67-
def __str__(self):
68-
return "ErrorMessage : " + self.error_message
69-
7054

7155
class StartMessage(Message):
7256
"""
73-
Message that asks the actor to launch its initialisation process
57+
Message sent to an actor to initiate its startup.
7458
"""
7559

76-
def __init__(self, sender_name: str):
77-
Message.__init__(self, sender_name)
78-
79-
def __str__(self):
80-
return "StartMessage"
81-
82-
83-
class EndMessage(Message):
84-
"""
85-
Message sent by actor to its parent when it terminates itself
86-
"""
87-
88-
def __init__(self, sender_name: str):
89-
Message.__init__(self, sender_name)
90-
91-
def __str__(self):
92-
return "EndMessage"
93-
9460

9561
class PoisonPillMessage(Message):
9662
"""
97-
Message which allow to kill an actor
63+
Message sent to an actor to initiate its shutdown.
9864
"""
9965

100-
def __init__(self, soft: bool = True, sender_name: str = ''):
101-
Message.__init__(self, sender_name=sender_name)
66+
def __init__(self, soft: bool = True):
67+
"""
68+
:param bool soft: Indicate whether the actor should process all its messages before shutting down.
69+
"""
10270
self.is_soft = soft
103-
self.is_hard = not soft
104-
105-
def __str__(self):
106-
return "PoisonPillMessage"
107-
108-
def __eq__(self, other):
109-
if isinstance(other, PoisonPillMessage):
110-
return other.is_soft == self.is_soft and other.is_hard == self.is_hard
111-
return False

src/powerapi/puller/handlers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def run(self):
7676
dispatcher.send_data(raw_report)
7777

7878
except FilterUselessError:
79-
self.handler.handle_internal_msg(PoisonPillMessage(False, self.name))
79+
self.handler.handle_internal_msg(PoisonPillMessage(False))
8080
return
8181

8282
except BadInputData as exn:
@@ -86,7 +86,7 @@ def run(self):
8686
except StopIteration:
8787
time.sleep(self.state.timeout_puller / 1000)
8888
if not self.state.stream_mode:
89-
self.handler.handle_internal_msg(PoisonPillMessage(False, self.name))
89+
self.handler.handle_internal_msg(PoisonPillMessage(False))
9090
return
9191

9292

@@ -141,11 +141,11 @@ def handle(self, msg: Message):
141141
try:
142142
StartHandler.handle(self, msg)
143143
except PullerInitializationException as e:
144-
self.state.actor.send_control(ErrorMessage(self.state.actor.name, e.msg))
144+
self.state.actor.send_control(ErrorMessage(e.msg))
145145

146146
self.pull_db()
147147

148-
self.handle_internal_msg(PoisonPillMessage(False, self.state.actor.name))
148+
self.handle_internal_msg(PoisonPillMessage(False))
149149

150150
def pull_db(self):
151151
"""
@@ -166,4 +166,4 @@ def _connect_database(self):
166166
self.state.database_it = self.state.database.iter(self.state.stream_mode)
167167
except DBError as error:
168168
self.state.alive = False
169-
self.state.actor.send_control(ErrorMessage(self.state.actor.name, error.msg))
169+
self.state.actor.send_control(ErrorMessage(error.msg))

src/powerapi/pusher/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def initialization(self):
4747
try:
4848
self.state.database.connect()
4949
except DBError as error:
50-
self.state.actor.send_control(ErrorMessage(self.state.actor.name, error.msg))
50+
self.state.actor.send_control(ErrorMessage(error.msg))
5151
self.state.alive = False
5252

5353

src/powerapi/report/control_report.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021, INRIA
1+
# Copyright (c) 2021, Inria
22
# Copyright (c) 2021, University of Lille
33
# All rights reserved.
44
#
@@ -41,21 +41,21 @@ class ControlReport(Report):
4141
This is useful to control external tools via a producer/consumer job queue.
4242
"""
4343

44-
def __init__(self, timestamp: datetime, sensor: str, target: str, action: str, parameters: list, metadata: dict[str, Any] = {}):
44+
def __init__(self, timestamp: datetime, sensor: str, target: str, action: str, parameters: list, metadata: dict[str, Any] | None = None):
4545
"""
46-
Initialize a Control Event report using the given parameters.
4746
:param timestamp: Report timestamp
4847
:param sensor: Sensor name
4948
:param target: Target name
5049
:param action: Action name
5150
:param parameters: Parameter values
5251
"""
53-
Report.__init__(self, timestamp, sensor, target, metadata)
52+
super().__init__(timestamp, sensor, target, metadata)
53+
5454
self.action = action
5555
self.parameters = parameters
5656

5757
def __repr__(self) -> str:
58-
return f'ControlReport({self.timestamp}, {self.sensor}, {self.target}, {self.action}, {self.parameters}, {self.metadata})'
58+
return f'ControlReport({self.timestamp}, {self.sensor}, {self.target}, {self.action}, {self.parameters})'
5959

6060
def __eq__(self, other: Any) -> bool:
6161
if not isinstance(other, ControlReport):
@@ -83,4 +83,4 @@ def to_mongodb(report: ControlReport) -> dict:
8383
"""
8484
:return: a dictionary, that can be stored into a mongodb, from a given ControlReport
8585
"""
86-
return report.__dict__
86+
return vars(report)

src/powerapi/report/formula_report.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022, INRIA
1+
# Copyright (c) 2022, Inria
22
# Copyright (c) 2022, University of Lille
33
# All rights reserved.
44
#
@@ -44,18 +44,14 @@ class FormulaReport(Report):
4444
This is useful to gather information about a running formula in order to debug or compute statistics.
4545
"""
4646

47-
def __init__(self, timestamp: datetime, sensor: str, target: str, metadata: dict[str, Any]):
47+
def __init__(self, timestamp: datetime, sensor: str, target: str, metadata: dict[str, Any] | None = None):
4848
"""
49-
Initialize a Formula report using the given parameters.
5049
:param timestamp: Report timestamp
5150
:param sensor: Sensor name
5251
:param target: Target name
5352
:param metadata: Metadata values, can be anything but should be json serializable
5453
"""
55-
Report.__init__(self, timestamp, sensor, target, metadata)
56-
57-
def __repr__(self) -> str:
58-
return f'FormulaReport({self.timestamp}, {self.sensor}, {self.target}, {self.metadata})'
54+
super().__init__(timestamp, sensor, target, metadata)
5955

6056
@staticmethod
6157
def to_csv_lines(report: FormulaReport, **_) -> (list[str], dict[str, Any]):
@@ -100,3 +96,10 @@ def to_influxdb(report: FormulaReport, **_) -> dict[str, Any]:
10096
}
10197

10298
return document
99+
100+
@staticmethod
101+
def to_json(report: FormulaReport) -> dict[str, Any]:
102+
"""
103+
Convert a Formula report into a json document that can be stored into json.
104+
"""
105+
return vars(report)

src/powerapi/report/hwpc_report.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021, INRIA
1+
# Copyright (c) 2021, Inria
22
# Copyright (c) 2021, University of Lille
33
# All rights reserved.
44
#
@@ -69,22 +69,19 @@ class HWPCReport(Report):
6969
}
7070
"""
7171

72-
def __init__(self, timestamp: datetime, sensor: str, target: str, groups: dict[str, dict],
73-
metadata: dict[str, Any] = {}):
72+
def __init__(self, timestamp: datetime, sensor: str, target: str, groups: dict[str, dict], metadata: dict[str, Any] | None = None):
7473
"""
75-
Initialize an HWPC report using the given parameters.
76-
:param datetime timestamp: Timestamp of the report
77-
:param str sensor: Sensor name
78-
:param str target: Target name
79-
:param Dict groups: Events groups
74+
:param timestamp: Timestamp of the report
75+
:param sensor: Sensor name
76+
:param target: Target name
77+
:param groups: Events groups
8078
"""
81-
Report.__init__(self, timestamp, sensor, target, metadata)
79+
super().__init__(timestamp, sensor, target, metadata)
8280

83-
#: (dict): Events groups
8481
self.groups = groups
8582

8683
def __repr__(self) -> str:
87-
return f'HWPCReport({self.timestamp}, {self.sensor}, {self.target}, {sorted(self.groups.keys())}, {self.metadata})'
84+
return f'HWPCReport({self.timestamp}, {self.sensor}, {self.target}, {sorted(self.groups.keys())})'
8885

8986
def __eq__(self, other) -> bool:
9087
if not isinstance(other, HWPCReport):
@@ -112,7 +109,7 @@ def from_json(data: dict) -> HWPCReport:
112109

113110
@staticmethod
114111
def to_json(report: HWPCReport) -> dict:
115-
return report.__dict__
112+
return vars(report)
116113

117114
@staticmethod
118115
def from_mongodb(data: dict) -> HWPCReport:

src/powerapi/report/power_report.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021, INRIA
1+
# Copyright (c) 2021, Inria
22
# Copyright (c) 2021, University of Lille
33
# All rights reserved.
44
#
@@ -42,21 +42,20 @@ class PowerReport(Report):
4242
PowerReport stores the power estimation information.
4343
"""
4444

45-
def __init__(self, timestamp: datetime, sensor: str, target: str, power: float, metadata: dict[str, Any] = {}):
45+
def __init__(self, timestamp: datetime, sensor: str, target: str, power: float, metadata: dict[str, Any] | None = None):
4646
"""
47-
Initialize a Power report using the given parameters.
4847
:param datetime timestamp: Report timestamp
4948
:param str sensor: Sensor name
5049
:param str target: Target name
5150
:param float power: Power value
5251
:param dict metadata: Metadata values, can be anything that add useful information
5352
"""
54-
Report.__init__(self, timestamp, sensor, target, metadata)
53+
super().__init__(timestamp, sensor, target, metadata)
5554

5655
self.power = power
5756

5857
def __repr__(self) -> str:
59-
return f'PowerReport({self.timestamp}, {self.sensor}, {self.target}, {self.power}, {self.metadata})'
58+
return f'PowerReport({self.timestamp}, {self.sensor}, {self.target}, {self.power})'
6059

6160
def __eq__(self, other) -> bool:
6261
if not isinstance(other, PowerReport):

0 commit comments

Comments
 (0)