From f51574e07ae0ba2242e85ee39539bc16e23d21ee Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Mon, 13 Jul 2020 11:28:25 +0200 Subject: [PATCH 1/2] feat: Use microseconds resolutions for data points --- CHANGELOG.md | 1 + influxdb_client/client/write/point.py | 23 ++++++++++------ tests/test_point.py | 38 +++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68a23061..1441dd99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index 63c5667f..ebc16668 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -152,6 +152,18 @@ def _escape_string(value): return str(value).translate(_ESCAPE_STRING) +def _to_nanoseconds(delta): + """ + Solutions come 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 @@ -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 @@ -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) diff --git a/tests/test_point.py b/tests/test_point.py index f5d71317..2807d3a0 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -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) @@ -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) @@ -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) \ From 686f2066266a446dcf44a9db5309d95c233c3639 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Tue, 14 Jul 2020 08:37:12 +0200 Subject: [PATCH 2/2] fix: documentation --- influxdb_client/client/write/point.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index ebc16668..2fda279f 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -154,7 +154,7 @@ def _escape_string(value): def _to_nanoseconds(delta): """ - Solutions come from v1 client. Thx. + Solution comes from v1 client. Thx. https://github.com/influxdata/influxdb-python/pull/811 """ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy