Skip to content

Commit

Permalink
fix: mapping dictionary without timestamp and tags into LineProtocol (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Jun 5, 2020
1 parent f98bca8 commit 6706571
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 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. [#92](https://github.com/influxdata/influxdb-client-python/issues/92): Optimize serializing Pandas DataFrame for writing

### Bug Fixes
1. [#105](https://github.com/influxdata/influxdb-client-python/pull/105): Fixed mapping dictionary without timestamp and tags into LineProtocol

## 1.7.0 [2020-05-15]

### Features
Expand Down
8 changes: 5 additions & 3 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def measurement(measurement):
@staticmethod
def from_dict(dictionary: dict, write_precision: WritePrecision = DEFAULT_WRITE_PRECISION):
point = Point(dictionary['measurement'])
for tag_key, tag_value in dictionary['tags'].items():
point.tag(tag_key, tag_value)
if 'tags' in dictionary:
for tag_key, tag_value in dictionary['tags'].items():
point.tag(tag_key, tag_value)
for field_key, field_value in dictionary['fields'].items():
point.field(field_key, field_value)
point.time(dictionary['time'], write_precision=write_precision)
if 'time' in dictionary:
point.time(dictionary['time'], write_precision=write_precision)
return point

def __init__(self, measurement_name):
Expand Down
69 changes: 37 additions & 32 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@
from tests.base_test import BaseTest


class TimeSpan(object):
@classmethod
def FromDays(cls, param):
return timedelta(days=param)

@classmethod
def FromHours(cls, param):
return timedelta(hours=param)

@classmethod
def FromSeconds(cls, param):
return timedelta(seconds=param)


class PointTest(BaseTest):

def test_MeasurementEscape(self):
Expand Down Expand Up @@ -124,47 +110,47 @@ def test_TimeSpanFormatting(self):
point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(TimeSpan.FromDays(1), WritePrecision.NS)
.time(timedelta(days=1), WritePrecision.NS)

self.assertEqual("h2o,location=europe level=2i 86400000000000", point.to_line_protocol())

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(TimeSpan.FromHours(356), WritePrecision.US)
.time(timedelta(hours=356), WritePrecision.US)

self.assertEqual("h2o,location=europe level=2i 1281600000000", point.to_line_protocol())

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(TimeSpan.FromSeconds(156), WritePrecision.MS)
.time(timedelta(seconds=156), WritePrecision.MS)

self.assertEqual("h2o,location=europe level=2i 156000", point.to_line_protocol())

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(TimeSpan.FromSeconds(123), WritePrecision.S)
.time(timedelta(seconds=123), WritePrecision.S)

self.assertEqual("h2o,location=europe level=2i 123", point.to_line_protocol())

def test_DateTimeFormatting(self):
dateTime = datetime(2015, 10, 15, 8, 20, 15)
date_time = datetime(2015, 10, 15, 8, 20, 15)

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(dateTime, WritePrecision.MS)
.time(date_time, WritePrecision.MS)

self.assertEqual("h2o,location=europe level=2i 1444897215000", point.to_line_protocol())

dateTime = datetime(2015, 10, 15, 8, 20, 15, 750, UTC)
date_time = datetime(2015, 10, 15, 8, 20, 15, 750, UTC)

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", False) \
.time(dateTime, WritePrecision.S)
.time(date_time, WritePrecision.S)

self.assertEqual("h2o,location=europe level=false 1444897215", point.to_line_protocol())

Expand All @@ -173,16 +159,16 @@ def test_DateTimeFormatting(self):
.field("level", True) \
.time(datetime.now(UTC), WritePrecision.S)

lineProtocol = point.to_line_protocol()
self.assertTrue("." not in lineProtocol)
line_protocol = point.to_line_protocol()
self.assertTrue("." not in line_protocol)

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", True) \
.time(datetime.now(UTC), WritePrecision.NS)

lineProtocol = point.to_line_protocol()
self.assertTrue("." not in lineProtocol)
line_protocol = point.to_line_protocol()
self.assertTrue("." not in line_protocol)

def test_DateTimeUtc(self):
date_time = datetime(2015, 10, 15, 8, 20, 15)
Expand Down Expand Up @@ -297,12 +283,31 @@ def test_timezone(self):
eastern = berlin.astimezone(timezone('US/Eastern'))

self.assertEqual("h2o val=1i 0", Point.measurement("h2o").field("val", 1).time(0).to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time("2009-11-10T23:00:00.123456Z").to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(utc).to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(dt).to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(1257894000123456000, write_precision=WritePrecision.NS).to_line_protocol())
self.assertEqual("h2o val=1i 1257890400123456000", Point.measurement("h2o").field("val", 1).time(eastern).to_line_protocol())
self.assertEqual("h2o val=1i 1257890400123456000", Point.measurement("h2o").field("val", 1).time(berlin).to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(
"2009-11-10T23:00:00.123456Z").to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000",
Point.measurement("h2o").field("val", 1).time(utc).to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000",
Point.measurement("h2o").field("val", 1).time(dt).to_line_protocol())
self.assertEqual("h2o val=1i 1257894000123456000",
Point.measurement("h2o").field("val", 1).time(1257894000123456000,
write_precision=WritePrecision.NS).to_line_protocol())
self.assertEqual("h2o val=1i 1257890400123456000",
Point.measurement("h2o").field("val", 1).time(eastern).to_line_protocol())
self.assertEqual("h2o val=1i 1257890400123456000",
Point.measurement("h2o").field("val", 1).time(berlin).to_line_protocol())

def test_from_dict_without_timestamp(self):
json = {"measurement": "my-org", "tags": {"tag1": "tag1", "tag2": "tag2"}, "fields": {'field1': 1, "field2": 2}}

point = Point.from_dict(json)
self.assertEqual("my-org,tag1=tag1,tag2=tag2 field1=1i,field2=2i", point.to_line_protocol())

def test_from_dict_without_tags(self):
json = {"measurement": "my-org", "fields": {'field1': 1, "field2": 2}}

point = Point.from_dict(json)
self.assertEqual("my-org field1=1i,field2=2i", point.to_line_protocol())


if __name__ == '__main__':
Expand Down

0 comments on commit 6706571

Please sign in to comment.