Skip to content

Commit 2c10803

Browse files
authored
Merge pull request #344 from powerapi-ng/fix/issue323/correct-tags-filtering
fix(report): Fix `PowerReport` tags filtering
2 parents c0b1950 + ccd1f87 commit 2c10803

File tree

4 files changed

+469
-6
lines changed

4 files changed

+469
-6
lines changed

src/powerapi/database/prometheus_db.py

-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ def save(self, report: Report):
142142
143143
:param report: Report to save
144144
"""
145-
if self.tags is None or not self.tags:
146-
self.tags = list(report.metadata.keys())
147145
self._init_metrics()
148146

149147
key, measure = self._report_to_measure_and_key(report)

src/powerapi/report/power_report.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,18 @@ def to_virtiofs_db(report: PowerReport) -> Tuple[str, str]:
154154
def gen_tag(self, metadata_kept):
155155
"""
156156
Generate the tags list of the report.
157+
:param metadata_kept: The metadata to keep
157158
"""
159+
# Always sensor and target are kept
158160
tags = {'sensor': self.sensor, 'target': self.target}
159161

160-
for metadata_name in metadata_kept:
161-
if metadata_name not in self.metadata:
162-
raise BadInputData(f'No tag "{metadata_name}" found in power report', self)
163-
tags[metadata_name] = self.metadata[metadata_name]
162+
if metadata_kept:
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]
167+
else:
168+
tags.update(self.metadata)
164169

165170
return tags
166171

tests/unit/report/conftest.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) 2024, Inria
2+
# Copyright (c) 2024, University of Lille
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright notice, this
9+
# list of conditions and the following disclaimer.
10+
#
11+
# * Redistributions in binary form must reproduce the above copyright notice,
12+
# this list of conditions and the following disclaimer in the documentation
13+
# and/or other materials provided with the distribution.
14+
#
15+
# * Neither the name of the copyright holder nor the names of its
16+
# contributors may be used to endorse or promote products derived from
17+
# this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
import pytest
30+
31+
from powerapi.report import PowerReport
32+
from tests.utils.report.power import gen_json_power_report
33+
34+
35+
@pytest.fixture
36+
def power_report_without_metadata() -> PowerReport:
37+
"""
38+
Generates a power_power
39+
"""
40+
json_input = gen_json_power_report(1)[0]
41+
report = PowerReport.from_json(json_input)
42+
43+
return report
44+
45+
46+
@pytest.fixture
47+
def power_report_with_metadata(power_report_without_metadata) -> PowerReport:
48+
"""
49+
Generates a power_power
50+
"""
51+
power_report_without_metadata.metadata = {'k1': 'v1',
52+
'k2': 'v2',
53+
'k3': 333,
54+
'k4': 'vv4'}
55+
56+
return power_report_without_metadata
57+
58+
59+
@pytest.fixture
60+
def power_report_with_nested_metadata(power_report_without_metadata) -> PowerReport:
61+
"""
62+
Generates a power_power
63+
"""
64+
power_report_without_metadata.metadata = {'k1': {'k1_k1': 1},
65+
'k2': 'v2',
66+
'k3': 333,
67+
'k4': {'k4_k1': 'v1',
68+
'k4_k2': {'k4_k2_k1': 'v2'}
69+
}
70+
}
71+
72+
return power_report_without_metadata

0 commit comments

Comments
 (0)