Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mapping dictionary without timestamp and tags into LineProtocol #105

Merged
merged 1 commit into from Jun 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
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
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
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