Skip to content

Commit adec0e8

Browse files
committed
refactor(report): Cleanup reports code
1 parent 50388c0 commit adec0e8

File tree

5 files changed

+40
-68
lines changed

5 files changed

+40
-68
lines changed

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):

src/powerapi/report/report.py

Lines changed: 11 additions & 38 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
#
@@ -35,7 +35,7 @@
3535
from typing import NewType, Any
3636
from zlib import crc32
3737

38-
from powerapi.exception import PowerAPIExceptionWithMessage, PowerAPIException
38+
from powerapi.exception import PowerAPIExceptionWithMessage
3939
from powerapi.message import Message
4040

4141
TIMESTAMP_KEY = 'timestamp'
@@ -60,59 +60,32 @@ def __init__(self, msg, input_data):
6060
self.input_data = input_data
6161

6262

63-
class DeserializationFail(PowerAPIException):
64-
"""
65-
Exception raised when the
66-
in the good format
67-
"""
68-
69-
7063
class Report(Message):
7164
"""
72-
Report abtract class.
65+
Report abstract class.
7366
"""
7467

75-
def __init__(self, timestamp: datetime, sensor: str, target: str, metadata: dict[str, Any] = {}):
68+
def __init__(self, timestamp: datetime, sensor: str, target: str, metadata: dict[str, Any] | None = None):
7669
"""
77-
Initialize a report using the given parameters.
78-
:param datetime timestamp: Timestamp
79-
:param str sensor: Sensor name.
80-
:param str target: Target name.
70+
:param timestamp: Timestamp of the measurements.
71+
:param sensor: Name of the sensor from which the measurements were taken.
72+
:param target: Name of the target that the measurements refer to.
8173
"""
82-
Message.__init__(self, None)
8374
self.timestamp = timestamp
8475
self.sensor = sensor
8576
self.target = target
86-
self.metadata = dict(metadata)
87-
88-
#: id given by the dispatcher actor in order manage report order
89-
self.dispatcher_report_id = None
90-
91-
def __str__(self):
92-
return f'{self.__class__.__name__}({self.timestamp}, {self.sensor}, {self.target}, {self.metadata})'
77+
self.metadata = metadata if metadata is not None else {}
9378

94-
def __repr__(self):
95-
return f'{self.__class__.__name__}({self.timestamp}, {self.sensor}, {self.target}, {self.metadata})'
79+
def __repr__(self) -> str:
80+
return f'{self.__class__.__name__}({self.timestamp}, {self.sensor}, {self.target})'
9681

97-
def __eq__(self, other):
82+
def __eq__(self, other) -> bool:
9883
return (isinstance(other, type(self)) and
9984
self.timestamp == other.timestamp and
10085
self.sensor == other.sensor and
10186
self.target == other.target and
10287
self.metadata == other.metadata)
10388

104-
@staticmethod
105-
def to_json(report: Report) -> dict:
106-
"""
107-
:return: a json dictionary, that can be converted into json format, from a given Report
108-
"""
109-
json = report.__dict__
110-
# sender_name and dispatcher_report_id are not used
111-
json.pop('sender_name')
112-
json.pop('dispatcher_report_id')
113-
114-
return json
115-
11689
@staticmethod
11790
def _extract_timestamp(ts):
11891
# Unix timestamp format (in milliseconds)

0 commit comments

Comments
 (0)