Skip to content

Commit 83b4d0c

Browse files
authored
feat: use microseconds resolutions for data points (#132)
1 parent d2472f4 commit 83b4d0c

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp
55
1. [#120](https://github.com/influxdata/influxdb-client-python/pull/120): ciso8601 is an optional dependency and has to be installed separably
66
1. [#121](https://github.com/influxdata/influxdb-client-python/pull/121): Added query_data_frame_stream method
7+
1. [#132](https://github.com/influxdata/influxdb-client-python/pull/132): Use microseconds resolutions for data points
78

89
### Bug Fixes
910
1. [#117](https://github.com/influxdata/influxdb-client-python/pull/117): Fixed appending default tags for single Point

influxdb_client/client/write/point.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ def _escape_string(value):
152152
return str(value).translate(_ESCAPE_STRING)
153153

154154

155+
def _to_nanoseconds(delta):
156+
"""
157+
Solution comes from v1 client. Thx.
158+
159+
https://github.com/influxdata/influxdb-python/pull/811
160+
"""
161+
nanoseconds_in_days = delta.days * 86400 * 10 ** 9
162+
nanoseconds_in_seconds = delta.seconds * 10 ** 9
163+
nanoseconds_in_micros = delta.microseconds * 10 ** 3
164+
return nanoseconds_in_days + nanoseconds_in_seconds + nanoseconds_in_micros
165+
166+
155167
def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
156168
if isinstance(timestamp, Integral):
157169
return timestamp # assume precision is correct if timestamp is int
@@ -166,9 +178,9 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
166178
timestamp = UTC.localize(timestamp)
167179
else:
168180
timestamp = timestamp.astimezone(UTC)
169-
ns = (timestamp - EPOCH).total_seconds() * 1e9
170-
else:
171-
ns = timestamp.total_seconds() * 1e9
181+
timestamp = timestamp - EPOCH
182+
183+
ns = _to_nanoseconds(timestamp)
172184

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

182-
# elif precision == 'm':
183-
# return ns / 1e9 / 60
184-
# elif precision == 'h':
185-
# return ns / 1e9 / 3600
186-
187194
raise ValueError(timestamp)

tests/test_point.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
from pytz import UTC, timezone
88

99
from influxdb_client import Point, WritePrecision
10-
from tests.base_test import BaseTest
1110

1211

13-
class PointTest(BaseTest):
12+
class PointTest(unittest.TestCase):
1413

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

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

155+
point = Point.measurement("h2o") \
156+
.tag("location", "europe") \
157+
.field("level", 2) \
158+
.time(timedelta(microseconds=876), WritePrecision.NS)
159+
160+
self.assertEqual("h2o,location=europe level=2i 876000", point.to_line_protocol())
161+
162+
point = Point.measurement("h2o") \
163+
.tag("location", "europe") \
164+
.field("level", 2) \
165+
.time(timedelta(milliseconds=954), WritePrecision.NS)
166+
167+
self.assertEqual("h2o,location=europe level=2i 954000000", point.to_line_protocol())
168+
156169
def test_DateTimeFormatting(self):
157170
date_time = datetime(2015, 10, 15, 8, 20, 15)
158171

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

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

188+
point = Point.measurement("h2o") \
189+
.tag("location", "europe") \
190+
.field("level", False) \
191+
.time(date_time, WritePrecision.MS)
192+
193+
self.assertEqual("h2o,location=europe level=false 1444897215000", point.to_line_protocol())
194+
195+
point = Point.measurement("h2o") \
196+
.tag("location", "europe") \
197+
.field("level", False) \
198+
.time(date_time, WritePrecision.US)
199+
200+
self.assertEqual("h2o,location=europe level=false 1444897215000750", point.to_line_protocol())
201+
202+
point = Point.measurement("h2o") \
203+
.tag("location", "europe") \
204+
.field("level", False) \
205+
.time(date_time, WritePrecision.NS)
206+
207+
self.assertEqual("h2o,location=europe level=false 1444897215000750000", point.to_line_protocol())
208+
175209
point = Point.measurement("h2o") \
176210
.tag("location", "europe") \
177211
.field("level", True) \

0 commit comments

Comments
 (0)