Skip to content

Commit 9b5cf4a

Browse files
committed
feat(report/power): Rework tags generation from report metadata
1 parent c4130ef commit 9b5cf4a

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/powerapi/report/power_report.py

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

154-
def gen_tags(self, selected_tags: None | list[str]) -> dict[str, Any]:
154+
def generate_tags(self, selected_tags: None | list[str] = None) -> dict[str, Any]:
155155
"""
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
156+
Generate the report tags from its metadata.
157+
:param selected_tags: List of tags to be included (in flattened/sanitized form), None to include everything
158+
:return: a single level dictionary containing the tags of the report
159159
"""
160-
fixed_tags = {'sensor': self.sensor, 'target': self.target}
161-
162-
if not selected_tags:
163-
return fixed_tags | self.metadata
164-
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)
160+
flattened_tags = self.flatten_tags(self.metadata)
176161
sanitized_tags_name = self.sanitize_tags_name(flattened_tags)
177162
sanitized_tags = {sanitized_tags_name[k]: v for k, v in flattened_tags.items()}
178163

179-
return sanitized_tags
164+
if selected_tags:
165+
tags = {k: v for k, v in sanitized_tags.items() if k in selected_tags}
166+
else:
167+
tags = sanitized_tags
168+
169+
return {'sensor': self.sensor, 'target': self.target} | tags
180170

181171
@staticmethod
182172
def to_influxdb(report: PowerReport, tags: None | list[str]) -> dict[str, Any]:
@@ -185,7 +175,7 @@ def to_influxdb(report: PowerReport, tags: None | list[str]) -> dict[str, Any]:
185175
"""
186176
return {
187177
'measurement': 'power_consumption',
188-
'tags': report.gen_flattened_sanitized_tags(tags),
178+
'tags': report.generate_tags(tags),
189179
'time': str(report.timestamp),
190180
'fields': {
191181
'power': report.power
@@ -198,7 +188,7 @@ def to_prometheus(report: PowerReport, tags: None | list[str]) -> dict[str, Any]
198188
:return: a dictionary, that can be stored into a prometheus instance, from a given PowerReport
199189
"""
200190
return {
201-
'tags': report.gen_flattened_sanitized_tags(tags),
191+
'tags': report.generate_tags(tags),
202192
'time': int(report.timestamp.timestamp()),
203193
'value': report.power
204194
}

0 commit comments

Comments
 (0)