Skip to content

Commit

Permalink
feat(report/power): Sanitize and flatten metadata when serializing re…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
gfieni committed Sep 12, 2024
1 parent 194d70f commit c0a79be
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/powerapi/report/power_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,19 @@ def to_virtiofs_db(report: PowerReport) -> Tuple[str, str]:
power = report.power
return filename, power

def gen_tag(self, metadata_kept):
def gen_tag(self, metadata_kept: None | list[str]) -> dict[str, Any]:
"""
Generate the tags list of the report.
:param metadata_kept: The metadata to keep
"""
# Always sensor and target are kept
tags = {'sensor': self.sensor, 'target': self.target}
if not metadata_kept:
return tags | self.metadata

if metadata_kept:
for metadata_name in metadata_kept:
if metadata_name not in self.metadata:
raise BadInputData(f'No tag "{metadata_name}" found in power report', self)
tags[metadata_name] = self.metadata[metadata_name]
else:
tags.update(self.metadata)
for metadata_name in metadata_kept:
if metadata_name not in self.metadata:
raise BadInputData(f'No tag "{metadata_name}" found in power report', self)
tags[metadata_name] = self.metadata[metadata_name]

return tags

Expand All @@ -177,9 +175,10 @@ def to_influxdb(report: PowerReport, tags: List[str]) -> Dict:
report_tags = report.gen_tag(tags)
sanitized_tags_name = report.sanitize_tags_name(report_tags.keys())
sanitized_tags = {sanitized_tags_name[k]: v for k, v in report_tags.items()}
flattened_tags = report.flatten_tags(sanitized_tags)
return {
'measurement': 'power_consumption',
'tags': sanitized_tags,
'tags': flattened_tags,
'time': str(report.timestamp),
'fields': {
'power': report.power
Expand All @@ -194,8 +193,9 @@ def to_prometheus(report: PowerReport, tags: List[str]) -> Dict:
report_tags = report.gen_tag(tags)
sanitized_tags_name = report.sanitize_tags_name(report_tags.keys())
sanitized_tags = {sanitized_tags_name[k]: v for k, v in report_tags.items()}
flattened_tags = report.flatten_tags(sanitized_tags)
return {
'tags': sanitized_tags,
'tags': flattened_tags,
'time': int(report.timestamp.timestamp()),
'value': report.power
}
Expand Down

0 comments on commit c0a79be

Please sign in to comment.