Skip to content

Commit 42d172d

Browse files
klDenappunni-mxginn8appunni
authored
Fix tests for existing 'Adding time_precision optional option to SeriesHelper' PR (influxdata#719)
* Adding time_precision into Meta of SeriesHelper time_precision option not currently supported in influx-db python * Making Time Precision optional Changed it from required to optional * Fixing Typo in _time_precision Attribute * Fixing coding conventions for Travis CI * Appunni: Test for invalid time_precision on SeriesHelper * Appunni: Intendation problem resolution in master * - Fix flake7 errors : E131 continuation line unaligned for hanging indent - Fix typo - Fix cls._client declaration ordering - Remove duplicate code cls._autocommit ... Co-authored-by: appunni-dishq <31534711+appunni-dishq@users.noreply.github.com> Co-authored-by: xginn8 <xginn8@users.noreply.github.com> Co-authored-by: appunni <appunni@dishq.in>
1 parent 896f623 commit 42d172d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

influxdb/helper.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class Meta:
4141
# Only applicable if autocommit is True.
4242
autocommit = True
4343
# If True and no bulk_size, then will set bulk_size to 1.
44+
time_precision = "h"|"m"|s"|"ms"|"u"|"ns"
45+
# Default is ns (nanoseconds)
46+
# Setting time precision while writing point
47+
# You should also make sure time is set in the given precision
4448
4549
"""
4650

@@ -71,6 +75,13 @@ def __new__(cls, *args, **kwargs):
7175
cls.__name__))
7276

7377
cls._autocommit = getattr(_meta, 'autocommit', False)
78+
cls._time_precision = getattr(_meta, 'time_precision', None)
79+
80+
allowed_time_precisions = ['h', 'm', 's', 'ms', 'u', 'ns', None]
81+
if cls._time_precision not in allowed_time_precisions:
82+
raise AttributeError(
83+
'In {0}, time_precision is set, but invalid use any of {}.'
84+
.format(cls.__name__, ','.join(allowed_time_precisions)))
7485

7586
cls._client = getattr(_meta, 'client', None)
7687
if cls._autocommit and not cls._client:
@@ -116,11 +127,11 @@ def __init__(self, **kw):
116127
keys = set(kw.keys())
117128

118129
# all tags should be passed, and keys - tags should be a subset of keys
119-
if not(tags <= keys):
130+
if not (tags <= keys):
120131
raise NameError(
121132
'Expected arguments to contain all tags {0}, instead got {1}.'
122133
.format(cls._tags, kw.keys()))
123-
if not(keys - tags <= fields):
134+
if not (keys - tags <= fields):
124135
raise NameError('Got arguments not in tags or fields: {0}'
125136
.format(keys - tags - fields))
126137

@@ -143,7 +154,10 @@ def commit(cls, client=None):
143154
"""
144155
if not client:
145156
client = cls._client
146-
rtn = client.write_points(cls._json_body_())
157+
rtn = client.write_points(
158+
cls._json_body_(),
159+
time_precision=cls._time_precision)
160+
# will be None if not set and will default to ns
147161
cls._reset_()
148162
return rtn
149163

influxdb/tests/helper_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,19 @@ class Meta:
310310

311311
series_name = 'events.stats.{server_name}'
312312

313+
class InvalidTimePrecision(SeriesHelper):
314+
"""Define instance of SeriesHelper for invalid time precision."""
315+
316+
class Meta:
317+
"""Define metadata for InvalidTimePrecision."""
318+
319+
series_name = 'events.stats.{server_name}'
320+
time_precision = "ks"
321+
fields = ['time', 'server_name']
322+
autocommit = True
323+
313324
for cls in [MissingMeta, MissingClient, MissingFields,
314-
MissingSeriesName]:
325+
MissingSeriesName, InvalidTimePrecision]:
315326
self.assertRaises(
316327
AttributeError, cls, **{'time': 159,
317328
'server_name': 'us.east-1'})

0 commit comments

Comments
 (0)