Skip to content

Commit 1dba030

Browse files
committed
feat: add record_measurement_name for static measurement name
1 parent ac6953e commit 1dba030

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

examples/write_structured_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ class Car:
6161
"""
6262
write_api.write(bucket="my-bucket",
6363
record=car,
64-
record_measurement_key="engine",
64+
record_measurement_name="performance",
6565
record_tag_keys=["engine", "type"],
6666
record_field_keys=["speed"])

influxdb_client/client/write/point.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,19 @@ def from_dict(dictionary: dict, write_precision: WritePrecision = DEFAULT_WRITE_
102102
record_tag_keys=["location", "version"],
103103
record_field_keys=["pressure", "temperature"])
104104
105-
:param dictionary: dictionary for serialize into Point structure
105+
:param dictionary: dictionary for serialize into data Point
106106
:param write_precision: sets the precision for the supplied time values
107107
:key record_measurement_key: key of dictionary with specified measurement
108+
:key record_measurement_name: static measurement name for data Point
108109
:key record_time_key: key of dictionary with specified timestamp
109110
:key record_tag_keys: list of dictionary keys to use as a tag
110111
:key record_field_keys: list of dictionary keys to use as a field
111112
:return: new data point
112113
"""
113-
point = Point(dictionary[kwargs.get('record_measurement_key', 'measurement')])
114+
measurement_ = kwargs.get('record_measurement_name', None)
115+
if measurement_ is None:
116+
measurement_ = dictionary[kwargs.get('record_measurement_key', 'measurement')]
117+
point = Point(measurement_)
114118

115119
record_tag_keys = kwargs.get('record_tag_keys', None)
116120
if record_tag_keys is not None:

influxdb_client/client/write_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def write(self, bucket: str, org: str = None,
259259
rest columns will be fields - ``DataFrame``
260260
:key record_measurement_key: key of record with specified measurement -
261261
``dictionary``, ``NamedTuple``, ``dataclass``
262+
:key record_measurement_name: static measurement name - ``dictionary``, ``NamedTuple``, ``dataclass``
262263
:key record_time_key: key of record with specified timestamp - ``dictionary``, ``NamedTuple``, ``dataclass``
263264
:key record_tag_keys: list of record keys to use as a tag - ``dictionary``, ``NamedTuple``, ``dataclass``
264265
:key record_field_keys: list of record keys to use as a field - ``dictionary``, ``NamedTuple``, ``dataclass``

tests/test_WriteApi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,13 @@ class Car:
576576
car = Car('12V-BT', 'sport-cars', 125.25)
577577
self.write_client.write("my-bucket", "my-org",
578578
record=car,
579-
record_measurement_key="engine",
579+
record_measurement_name="performance",
580580
record_tag_keys=["engine", "type"],
581581
record_field_keys=["speed"])
582582

583583
requests = httpretty.httpretty.latest_requests
584584
self.assertEqual(1, len(requests))
585-
self.assertEqual("12V-BT,engine=12V-BT,type=sport-cars speed=125.25", requests[0].parsed_body)
585+
self.assertEqual("performance,engine=12V-BT,type=sport-cars speed=125.25", requests[0].parsed_body)
586586

587587

588588
class AsynchronousWriteTest(BaseTest):

tests/test_WriteApiBatching.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ class Car:
562562
car = Car('12V-BT', 'sport-cars', 125.25)
563563
self._write_client.write("my-bucket", "my-org",
564564
record=car,
565-
record_measurement_key="engine",
565+
record_measurement_name="performance",
566566
record_tag_keys=["engine", "type"],
567567
record_field_keys=["speed"])
568568

@@ -571,7 +571,7 @@ class Car:
571571
_requests = httpretty.httpretty.latest_requests
572572

573573
self.assertEqual(1, len(_requests))
574-
self.assertEqual("12V-BT,engine=12V-BT,type=sport-cars speed=125.25", _requests[0].parsed_body)
574+
self.assertEqual("performance,engine=12V-BT,type=sport-cars speed=125.25", _requests[0].parsed_body)
575575

576576

577577
if __name__ == '__main__':

tests/test_point.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,19 @@ def test_from_dictionary_tolerant_to_missing_tags_and_fields(self):
472472
record_field_keys=["pressure", "temperature"])
473473
self.assertEqual("sensor_pt859,location=warehouse_125 pressure=125i", point.to_line_protocol())
474474

475+
def test_static_measurement_name(self):
476+
dictionary = {
477+
"name": "sensor_pt859",
478+
"location": "warehouse_125",
479+
"pressure": 125
480+
}
481+
point = Point.from_dict(dictionary,
482+
write_precision=WritePrecision.S,
483+
record_measurement_name="custom_sensor_id",
484+
record_tag_keys=["location", "version"],
485+
record_field_keys=["pressure", "temperature"])
486+
self.assertEqual("custom_sensor_id,location=warehouse_125 pressure=125i", point.to_line_protocol())
487+
475488

476489
if __name__ == '__main__':
477490
unittest.main()

0 commit comments

Comments
 (0)