class PandasDateTimeHelperTest(unittest.TestCase):
    def setUp(self) -> None:
        self.helper = PandasDateTimeHelper()

    def test_parse_date(self):
        date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z')

        self.assertEqual(date.year, 2020)
        self.assertEqual(date.month, 8)
        self.assertEqual(date.day, 7)
        self.assertEqual(date.hour, 6)
        self.assertEqual(date.minute, 21)
        self.assertEqual(date.second, 57)
        self.assertEqual(date.microsecond, 331249)
        self.assertEqual(date.nanosecond, 158)

    def test_to_nanoseconds(self):
        date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z')
        nanoseconds = self.helper.to_nanoseconds(
            date - UTC.localize(datetime.utcfromtimestamp(0)))

        self.assertEqual(nanoseconds, 1596781317331249158)

    def test_to_nanoseconds_buildin_timedelta(self):
        nanoseconds = self.helper.to_nanoseconds(timedelta(days=1))

        self.assertEqual(nanoseconds, 86400000000000)
示例#2
0
    def test_write_query_data_nanoseconds(self):

        from influxdb_client.client.util.date_utils_pandas import PandasDateTimeHelper
        import influxdb_client.client.util.date_utils as date_utils

        date_utils.date_helper = PandasDateTimeHelper()

        bucket = self.create_test_bucket()

        point = Point("h2o_feet") \
            .field("water_level", 155) \
            .tag("location", "creek level")\
            .time('1996-02-25T21:20:00.001001231Z')

        self.write_client.write(bucket.name, self.org, [point])

        flux_result = self.client.query_api().query(
            f'from(bucket:"{bucket.name}") |> range(start: 1970-01-01T00:00:00.000000001Z)')
        self.assertEqual(1, len(flux_result))

        record = flux_result[0].records[0]

        self.assertEqual(self.id_tag, record["id"])
        self.assertEqual(record["_value"], 155)
        self.assertEqual(record["location"], "creek level")
        self.assertEqual(record["_time"].year, 1996)
        self.assertEqual(record["_time"].month, 2)
        self.assertEqual(record["_time"].day, 25)
        self.assertEqual(record["_time"].hour, 21)
        self.assertEqual(record["_time"].minute, 20)
        self.assertEqual(record["_time"].second, 00)
        self.assertEqual(record["_time"].microsecond, 1001)
        self.assertEqual(record["_time"].nanosecond, 231)

        date_utils.date_helper = None
示例#3
0
from influxdb_client import Point, InfluxDBClient
from influxdb_client.client.util.date_utils_pandas import PandasDateTimeHelper
from influxdb_client.client.write_api import SYNCHRONOUS
"""
Set PandasDate helper which supports nanoseconds.
"""
import influxdb_client.client.util.date_utils as date_utils

date_utils.date_helper = PandasDateTimeHelper()
"""
Prepare client.
"""
client = InfluxDBClient(url="http://localhost:9999",
                        token="my-token",
                        org="my-org")

write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()
"""
Prepare data
"""

point = Point("h2o_feet") \
    .field("water_level", 10) \
    .tag("location", "pacific") \
    .time('1996-02-25T21:20:00.001001231Z')

print(f'Time serialized with nanosecond precision: {point.to_line_protocol()}')
print()

write_api.write(bucket="my-bucket", record=point)
 def setUp(self) -> None:
     self.helper = PandasDateTimeHelper()