From d19ff96bcdfe63d0384dde51e3339b2afdb3ada0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Mon, 29 Jun 2020 08:49:25 +0200 Subject: [PATCH] fix: appending default tags for single Point (#117) --- CHANGELOG.md | 3 +++ influxdb_client/client/write_api.py | 18 ++++++++------- tests/test_WriteApi.py | 36 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 907eedff..5d415d1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ### Features 1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp +### Bug Fixes +1. [#117](https://github.com/influxdata/influxdb-client-python/pull/117): Fixed appending default tags for single Point + ## 1.8.0 [2020-06-19] ### Features diff --git a/influxdb_client/client/write_api.py b/influxdb_client/client/write_api.py index 897ee8cb..4db44639 100644 --- a/influxdb_client/client/write_api.py +++ b/influxdb_client/client/write_api.py @@ -202,14 +202,7 @@ def write(self, bucket: str, org: str = None, if self._point_settings.defaultTags and record is not None: for key, val in self._point_settings.defaultTags.items(): - if isinstance(record, dict): - record.get("tags")[key] = val - else: - for r in record: - if isinstance(r, dict): - r.get("tags")[key] = val - elif isinstance(r, Point): - r.tag(key, val) + self._append_default_tag(key, val, record) if self._write_options.write_type is WriteType.batching: return self._write_batching(bucket, org, record, @@ -301,6 +294,15 @@ def _write_batching(self, bucket, org, data, return None + def _append_default_tag(self, key, val, record): + if isinstance(record, Point): + record.tag(key, val) + elif isinstance(record, dict): + record.get("tags")[key] = val + elif isinstance(record, list): + for item in record: + self._append_default_tag(key, val, item) + def _itertuples(self, data_frame): cols = [data_frame.iloc[:, k] for k in range(len(data_frame.columns))] return zip(data_frame.index, *cols) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index f87e2002..ce9f1759 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -334,6 +334,42 @@ def test_write_point_different_precision(self): self.assertEqual(result[1].records[0].get_time(), datetime.datetime(2020, 4, 20, 6, 30, tzinfo=datetime.timezone.utc)) + def test_write_point_with_default_tags(self): + bucket = self.create_test_bucket() + + point = Point("h2o_feet")\ + .field("water_level", 1)\ + .tag("location", "creek level") + + self.write_client.write(bucket.name, self.org, point) + + flux_result = self.client.query_api().query(f'from(bucket:"{bucket.name}") |> range(start: 1970-01-01T00:00:00.000000001Z)') + self.assertEqual(1, len(flux_result)) + + record = flux_result[0].records[0] + + self.assertEqual(self.id_tag, record["id"]) + self.assertEqual(self.customer_tag, record["customer"]) + self.assertEqual("LA", record[self.data_center_key]) + + def test_write_list_of_list_point_with_default_tags(self): + bucket = self.create_test_bucket() + + point = Point("h2o_feet")\ + .field("water_level", 1)\ + .tag("location", "creek level") + + self.write_client.write(bucket.name, self.org, [[point]]) + + flux_result = self.client.query_api().query(f'from(bucket:"{bucket.name}") |> range(start: 1970-01-01T00:00:00.000000001Z)') + self.assertEqual(1, len(flux_result)) + + record = flux_result[0].records[0] + + self.assertEqual(self.id_tag, record["id"]) + self.assertEqual(self.customer_tag, record["customer"]) + self.assertEqual("LA", record[self.data_center_key]) + class AsynchronousWriteTest(BaseTest):