Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/powerapi/cli/common_cli_parsing_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def __init__(self):

subparser_influx2_output = SubgroupConfigParsingManager("influxdb2")
subparser_influx2_output.add_argument("u", "uri", help_text="specify InfluxDB uri")
subparser_influx2_output.add_argument("t", "tags", help_text="List of tags that should be kept")
subparser_influx2_output.add_argument("k", "token",
help_text="specify token for accessing the database")
subparser_influx2_output.add_argument("g", "org",
Expand Down
2 changes: 1 addition & 1 deletion src/powerapi/cli/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _influxdb2_database_factory(conf: dict) -> WritableDatabase:
InfluxDB2 database factory method.
"""
from powerapi.database.influxdb2 import InfluxDB2
return InfluxDB2(conf['model'], conf['uri'], conf['org'], conf['bucket'], conf['token'], gen_tag_list(conf))
return InfluxDB2(conf['model'], conf['uri'], conf['org'], conf['bucket'], conf['token'])

@staticmethod
def _opentsdb_database_factory(conf: dict) -> WritableDatabase:
Expand Down
22 changes: 3 additions & 19 deletions src/powerapi/database/influxdb2/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,20 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from dataclasses import dataclass

from powerapi.database.codec import CodecOptions, ReportEncoder, ReportEncoderRegistry
from powerapi.report import PowerReport, FormulaReport


@dataclass
class EncoderOptions(CodecOptions):
"""
Encoder options for the InfluxDB database.
"""
allowed_tags_name: set[str]


class PowerReportEncoder(ReportEncoder[PowerReport, dict]):
"""
Power Report encoder for the InfluxDB database.
"""

@staticmethod
def encode(report: PowerReport, opts: EncoderOptions | None = None) -> dict:
flattened_tags = report.flatten_tags(report.metadata)
if opts.allowed_tags_name:
dynamic_tags = {k: v for k, v in flattened_tags.items() if k in opts.allowed_tags_name}
else:
dynamic_tags = flattened_tags

def encode(report: PowerReport, opts: CodecOptions | None = None) -> dict:
return {
'measurement': 'powerrep',
'tags': {'sensor': report.sensor, 'target': report.target} | dynamic_tags,
'tags': {'sensor': report.sensor, 'target': report.target} | report.flatten_tags(report.metadata),
'fields': {'power_estimation': report.power},
'time': report.timestamp,
}
Expand All @@ -68,7 +52,7 @@ class FormulaReportEncoder(ReportEncoder[FormulaReport, dict]):
"""

@staticmethod
def encode(report: FormulaReport, opts: EncoderOptions | None = None) -> dict:
def encode(report: FormulaReport, opts: CodecOptions | None = None) -> dict:
return {
'measurement': 'formularep',
'tags': {'sensor': report.sensor, 'target': report.target},
Expand Down
8 changes: 3 additions & 5 deletions src/powerapi/database/influxdb2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

from powerapi.database import ConnectionFailed, WriteFailed
from powerapi.database.driver import WritableDatabase
from powerapi.database.influxdb2.codecs import ReportEncoders, EncoderOptions
from powerapi.database.influxdb2.codecs import ReportEncoders
from powerapi.report import Report


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

def __init__(self, report_type: type[Report], url: str, org: str, bucket: str, token: str, tags: list[str]):
def __init__(self, report_type: type[Report], url: str, org: str, bucket: str, token: str):
"""
:param report_type: Type of the report handled by this database
:param url: InfluxDB server URL
:param org: Organization name
:param bucket: Bucket name
:param token: Authentication token
:param tags: List of allowed tags name, leave empty to allow all tags
"""
super().__init__()

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

self._report_encoder = ReportEncoders.get(report_type)
self._report_encoder_opts = EncoderOptions(set(tags))

def connect(self) -> None:
"""
Expand Down Expand Up @@ -97,7 +95,7 @@ def write(self, reports: Iterable[Report]) -> None:
:raise: WriteFailed if the write operation fails
"""
try:
encoded_reports = [self._report_encoder.encode(report, self._report_encoder_opts) for report in reports]
encoded_reports = [self._report_encoder.encode(report) for report in reports]
self._write_api.write(self._bucket_name, record=encoded_reports)
except (OSError, HTTPError, InfluxDBError) as exn:
raise WriteFailed(f'Failed to save report to the InfluxDB database: {exn}') from exn
1 change: 0 additions & 1 deletion tests/unit/cli/test_generator_influxdb2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def test_pusher_generator_with_valid_influxdb2_config(influxdb2_config):
assert db._client.org == expected_db_attributes['org']
assert db._client.token == expected_db_attributes['token']
assert db._bucket_name == expected_db_attributes['bucket']
assert {'powerapi_example_tag1', 'powerapi_example_tag2'}.issubset(db._report_encoder_opts.allowed_tags_name)


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