Skip to content

Commit

Permalink
fix: appending default tags for single Point (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Jun 29, 2020
1 parent 5ddf643 commit d19ff96
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions influxdb_client/client/write_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_WriteApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit d19ff96

Please sign in to comment.