Skip to content

Commit c4130ef

Browse files
committed
feat(report/power): Optimize generation of tags when serializing reports
1 parent b1d1237 commit c4130ef

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/powerapi/report/power_report.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,51 +151,54 @@ def to_virtiofs_db(report: PowerReport) -> Tuple[str, str]:
151151
power = report.power
152152
return filename, power
153153

154-
def gen_tag(self, metadata_kept: None | list[str]) -> dict[str, Any]:
154+
def gen_tags(self, selected_tags: None | list[str]) -> dict[str, Any]:
155155
"""
156-
Generate the tags list of the report.
157-
:param metadata_kept: The metadata to keep
156+
Generate the metadata tags of the report.
157+
:param selected_tags: List of tags to be included, None to include everything
158+
:return: a dictionary containing the tags
158159
"""
159-
tags = {'sensor': self.sensor, 'target': self.target}
160-
if not metadata_kept:
161-
return tags | self.metadata
160+
fixed_tags = {'sensor': self.sensor, 'target': self.target}
162161

163-
for metadata_name in metadata_kept:
164-
if metadata_name not in self.metadata:
165-
raise BadInputData(f'No tag "{metadata_name}" found in power report', self)
166-
tags[metadata_name] = self.metadata[metadata_name]
162+
if not selected_tags:
163+
return fixed_tags | self.metadata
167164

168-
return tags
165+
return fixed_tags | {tag_name: self.metadata[tag_name] for tag_name in selected_tags}
166+
167+
def gen_flattened_sanitized_tags(self, selected_tags: None | list[str]) -> dict[str, Any]:
168+
"""
169+
Generate the flattened and sanitized tags list of the report.
170+
:param selected_tags: List of tags to be included, None to include everything
171+
:return: a dictionary containing the flattened and sanitized tags
172+
"""
173+
tags = self.gen_tags(selected_tags)
174+
175+
flattened_tags = self.flatten_tags(tags)
176+
sanitized_tags_name = self.sanitize_tags_name(flattened_tags)
177+
sanitized_tags = {sanitized_tags_name[k]: v for k, v in flattened_tags.items()}
178+
179+
return sanitized_tags
169180

170181
@staticmethod
171-
def to_influxdb(report: PowerReport, tags: List[str]) -> Dict:
182+
def to_influxdb(report: PowerReport, tags: None | list[str]) -> dict[str, Any]:
172183
"""
173184
:return: a dictionary, that can be stored into an influxdb, from a given PowerReport
174185
"""
175-
report_tags = report.gen_tag(tags)
176-
sanitized_tags_name = report.sanitize_tags_name(report_tags.keys())
177-
sanitized_tags = {sanitized_tags_name[k]: v for k, v in report_tags.items()}
178-
flattened_tags = report.flatten_tags(sanitized_tags)
179186
return {
180187
'measurement': 'power_consumption',
181-
'tags': flattened_tags,
188+
'tags': report.gen_flattened_sanitized_tags(tags),
182189
'time': str(report.timestamp),
183190
'fields': {
184191
'power': report.power
185192
}
186193
}
187194

188195
@staticmethod
189-
def to_prometheus(report: PowerReport, tags: List[str]) -> Dict:
196+
def to_prometheus(report: PowerReport, tags: None | list[str]) -> dict[str, Any]:
190197
"""
191198
:return: a dictionary, that can be stored into a prometheus instance, from a given PowerReport
192199
"""
193-
report_tags = report.gen_tag(tags)
194-
sanitized_tags_name = report.sanitize_tags_name(report_tags.keys())
195-
sanitized_tags = {sanitized_tags_name[k]: v for k, v in report_tags.items()}
196-
flattened_tags = report.flatten_tags(sanitized_tags)
197200
return {
198-
'tags': flattened_tags,
201+
'tags': report.gen_flattened_sanitized_tags(tags),
199202
'time': int(report.timestamp.timestamp()),
200203
'value': report.power
201204
}

0 commit comments

Comments
 (0)