Skip to content

Commit 22f1765

Browse files
authored
Merge pull request #819 from powerapi-ng/refactor/cleanup-influxdb-driver
refactor(database/influxdb2): Remove tags filter from report encoders
2 parents e11848e + 010b249 commit 22f1765

5 files changed

Lines changed: 7 additions & 27 deletions

File tree

src/powerapi/cli/common_cli_parsing_manager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ def __init__(self):
281281

282282
subparser_influx2_output = SubgroupConfigParsingManager("influxdb2")
283283
subparser_influx2_output.add_argument("u", "uri", help_text="specify InfluxDB uri")
284-
subparser_influx2_output.add_argument("t", "tags", help_text="List of tags that should be kept")
285284
subparser_influx2_output.add_argument("k", "token",
286285
help_text="specify token for accessing the database")
287286
subparser_influx2_output.add_argument("g", "org",

src/powerapi/cli/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def _influxdb2_database_factory(conf: dict) -> WritableDatabase:
309309
InfluxDB2 database factory method.
310310
"""
311311
from powerapi.database.influxdb2 import InfluxDB2
312-
return InfluxDB2(conf['model'], conf['uri'], conf['org'], conf['bucket'], conf['token'], gen_tag_list(conf))
312+
return InfluxDB2(conf['model'], conf['uri'], conf['org'], conf['bucket'], conf['token'])
313313

314314
@staticmethod
315315
def _opentsdb_database_factory(conf: dict) -> WritableDatabase:

src/powerapi/database/influxdb2/codecs.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,20 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
from dataclasses import dataclass
31-
3230
from powerapi.database.codec import CodecOptions, ReportEncoder, ReportEncoderRegistry
3331
from powerapi.report import PowerReport, FormulaReport
3432

3533

36-
@dataclass
37-
class EncoderOptions(CodecOptions):
38-
"""
39-
Encoder options for the InfluxDB database.
40-
"""
41-
allowed_tags_name: set[str]
42-
43-
4434
class PowerReportEncoder(ReportEncoder[PowerReport, dict]):
4535
"""
4636
Power Report encoder for the InfluxDB database.
4737
"""
4838

4939
@staticmethod
50-
def encode(report: PowerReport, opts: EncoderOptions | None = None) -> dict:
51-
flattened_tags = report.flatten_tags(report.metadata)
52-
if opts.allowed_tags_name:
53-
dynamic_tags = {k: v for k, v in flattened_tags.items() if k in opts.allowed_tags_name}
54-
else:
55-
dynamic_tags = flattened_tags
56-
40+
def encode(report: PowerReport, opts: CodecOptions | None = None) -> dict:
5741
return {
5842
'measurement': 'powerrep',
59-
'tags': {'sensor': report.sensor, 'target': report.target} | dynamic_tags,
43+
'tags': {'sensor': report.sensor, 'target': report.target} | report.flatten_tags(report.metadata),
6044
'fields': {'power_estimation': report.power},
6145
'time': report.timestamp,
6246
}
@@ -68,7 +52,7 @@ class FormulaReportEncoder(ReportEncoder[FormulaReport, dict]):
6852
"""
6953

7054
@staticmethod
71-
def encode(report: FormulaReport, opts: EncoderOptions | None = None) -> dict:
55+
def encode(report: FormulaReport, opts: CodecOptions | None = None) -> dict:
7256
return {
7357
'measurement': 'formularep',
7458
'tags': {'sensor': report.sensor, 'target': report.target},

src/powerapi/database/influxdb2/driver.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
from powerapi.database import ConnectionFailed, WriteFailed
3737
from powerapi.database.driver import WritableDatabase
38-
from powerapi.database.influxdb2.codecs import ReportEncoders, EncoderOptions
38+
from powerapi.database.influxdb2.codecs import ReportEncoders
3939
from powerapi.report import Report
4040

4141

@@ -45,14 +45,13 @@ class InfluxDB2(WritableDatabase):
4545
Allow to persist reports to an InfluxDB (version 2) database.
4646
"""
4747

48-
def __init__(self, report_type: type[Report], url: str, org: str, bucket: str, token: str, tags: list[str]):
48+
def __init__(self, report_type: type[Report], url: str, org: str, bucket: str, token: str):
4949
"""
5050
:param report_type: Type of the report handled by this database
5151
:param url: InfluxDB server URL
5252
:param org: Organization name
5353
:param bucket: Bucket name
5454
:param token: Authentication token
55-
:param tags: List of allowed tags name, leave empty to allow all tags
5655
"""
5756
super().__init__()
5857

@@ -63,7 +62,6 @@ def __init__(self, report_type: type[Report], url: str, org: str, bucket: str, t
6362
self._write_api = self._client.write_api()
6463

6564
self._report_encoder = ReportEncoders.get(report_type)
66-
self._report_encoder_opts = EncoderOptions(set(tags))
6765

6866
def connect(self) -> None:
6967
"""
@@ -97,7 +95,7 @@ def write(self, reports: Iterable[Report]) -> None:
9795
:raise: WriteFailed if the write operation fails
9896
"""
9997
try:
100-
encoded_reports = [self._report_encoder.encode(report, self._report_encoder_opts) for report in reports]
98+
encoded_reports = [self._report_encoder.encode(report) for report in reports]
10199
self._write_api.write(self._bucket_name, record=encoded_reports)
102100
except (OSError, HTTPError, InfluxDBError) as exn:
103101
raise WriteFailed(f'Failed to save report to the InfluxDB database: {exn}') from exn

tests/unit/cli/test_generator_influxdb2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def test_pusher_generator_with_valid_influxdb2_config(influxdb2_config):
8080
assert db._client.org == expected_db_attributes['org']
8181
assert db._client.token == expected_db_attributes['token']
8282
assert db._bucket_name == expected_db_attributes['bucket']
83-
assert {'powerapi_example_tag1', 'powerapi_example_tag2'}.issubset(db._report_encoder_opts.allowed_tags_name)
8483

8584

8685
@pytest.mark.parametrize('missing_arg', ['model', 'uri', 'org', 'token', 'bucket'])

0 commit comments

Comments
 (0)