Skip to content

Commit

Permalink
feat: bind query parameters (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhajek committed Apr 6, 2021
1 parent 4920739 commit b179e4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
11 changes: 5 additions & 6 deletions influxdb_client/client/query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import csv
from datetime import datetime, timedelta
from typing import List, Generator, Any
from pytz import UTC

from influxdb_client import Dialect, IntegerLiteral, BooleanLiteral, FloatLiteral, DateTimeLiteral, StringLiteral, \
VariableAssignment, Identifier, OptionStatement, File, DurationLiteral, Duration, UnaryExpression
from influxdb_client import Query, QueryService
from influxdb_client.client.flux_csv_parser import FluxCsvParser, FluxSerializationMode
from influxdb_client.client.flux_table import FluxTable, FluxRecord
from influxdb_client.client.util.date_utils import get_date_helper


class QueryApi(object):
Expand Down Expand Up @@ -101,7 +101,7 @@ def query_stream(self, query: str, org=None, params: dict = None) -> Generator['
if org is None:
org = self._influxdb_client.org

response = self._query_api.post_query(org=org, query=self._create_query(query, self.default_dialect),
response = self._query_api.post_query(org=org, query=self._create_query(query, self.default_dialect, params),
async_req=False, _preload_content=False, _return_http_data_only=False)

_parser = FluxCsvParser(response=response, serialization_mode=FluxSerializationMode.stream)
Expand Down Expand Up @@ -165,6 +165,8 @@ def _params_to_extern_ast(params: dict) -> List['OptionStatement']:

statements = []
for key, value in params.items():
if value is None:
continue

if isinstance(value, bool):
literal = BooleanLiteral("BooleanLiteral", value)
Expand All @@ -173,10 +175,7 @@ def _params_to_extern_ast(params: dict) -> List['OptionStatement']:
elif isinstance(value, float):
literal = FloatLiteral("FloatLiteral", value)
elif isinstance(value, datetime):
if not value.tzinfo:
value = UTC.localize(value)
else:
value = value.astimezone(UTC)
value = get_date_helper().to_utc(value)
literal = DateTimeLiteral("DateTimeLiteral", value.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
elif isinstance(value, timedelta):
# convert to microsecodns
Expand Down
14 changes: 14 additions & 0 deletions influxdb_client/client/util/date_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Utils to get right Date parsing function."""
import datetime

from dateutil import parser
from pytz import UTC

date_helper = None

Expand Down Expand Up @@ -30,6 +32,18 @@ def to_nanoseconds(self, delta):

return nanoseconds_in_days + nanoseconds_in_seconds + nanoseconds_in_micros

def to_utc(self, value: datetime):
"""
Convert datetime to UTC timezone.
:param value: datetime
:return: datetime in UTC
"""
if not value.tzinfo:
return UTC.localize(value)
else:
return value.astimezone(UTC)


def get_date_helper() -> DateHelper:
"""
Expand Down
6 changes: 1 addition & 5 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
if isinstance(timestamp, timedelta) or isinstance(timestamp, datetime):

if isinstance(timestamp, datetime):
if not timestamp.tzinfo:
timestamp = UTC.localize(timestamp)
else:
timestamp = timestamp.astimezone(UTC)
timestamp = timestamp - EPOCH
timestamp = date_helper.to_utc(timestamp) - EPOCH

ns = date_helper.to_nanoseconds(timestamp)

Expand Down

0 comments on commit b179e4b

Please sign in to comment.