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

feat: use microseconds resolutions for data points #132

Merged
merged 2 commits into from
Jul 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp
1. [#120](https://github.com/influxdata/influxdb-client-python/pull/120): ciso8601 is an optional dependency and has to be installed separably
1. [#121](https://github.com/influxdata/influxdb-client-python/pull/121): Added query_data_frame_stream method
1. [#132](https://github.com/influxdata/influxdb-client-python/pull/132): Use microseconds resolutions for data points

### Bug Fixes
1. [#117](https://github.com/influxdata/influxdb-client-python/pull/117): Fixed appending default tags for single Point
Expand Down
23 changes: 15 additions & 8 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ def _escape_string(value):
return str(value).translate(_ESCAPE_STRING)


def _to_nanoseconds(delta):
"""
Solution comes from v1 client. Thx.

https://github.com/influxdata/influxdb-python/pull/811
"""
nanoseconds_in_days = delta.days * 86400 * 10 ** 9
nanoseconds_in_seconds = delta.seconds * 10 ** 9
nanoseconds_in_micros = delta.microseconds * 10 ** 3
return nanoseconds_in_days + nanoseconds_in_seconds + nanoseconds_in_micros


def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
if isinstance(timestamp, Integral):
return timestamp # assume precision is correct if timestamp is int
Expand All @@ -166,9 +178,9 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
timestamp = UTC.localize(timestamp)
else:
timestamp = timestamp.astimezone(UTC)
ns = (timestamp - EPOCH).total_seconds() * 1e9
else:
ns = timestamp.total_seconds() * 1e9
timestamp = timestamp - EPOCH

ns = _to_nanoseconds(timestamp)

if precision is None or precision == WritePrecision.NS:
return ns
Expand All @@ -179,9 +191,4 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
elif precision == WritePrecision.S:
return ns / 1e9

# elif precision == 'm':
# return ns / 1e9 / 60
# elif precision == 'h':
# return ns / 1e9 / 3600

raise ValueError(timestamp)
38 changes: 36 additions & 2 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from pytz import UTC, timezone

from influxdb_client import Point, WritePrecision
from tests.base_test import BaseTest


class PointTest(BaseTest):
class PointTest(unittest.TestCase):

def test_MeasurementEscape(self):
point = Point.measurement("h2 o").tag("location", "europe").tag("", "warn").field("level", 2)
Expand Down Expand Up @@ -153,6 +152,20 @@ def test_TimeSpanFormatting(self):

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

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(timedelta(microseconds=876), WritePrecision.NS)

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

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", 2) \
.time(timedelta(milliseconds=954), WritePrecision.NS)

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

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

Expand All @@ -172,6 +185,27 @@ def test_DateTimeFormatting(self):

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

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

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

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

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

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

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

point = Point.measurement("h2o") \
.tag("location", "europe") \
.field("level", True) \
Expand Down