Skip to content

Commit ab983d3

Browse files
committed
Allow time to be specified in SeriesHelper.__init__()
1 parent 5825841 commit ab983d3

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

influxdb/helper.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from warnings import warn
77

88
import six
9+
import time
910

1011

1112
class SeriesHelper(object):
@@ -87,8 +88,11 @@ def __new__(cls, *args, **kwargs):
8788
' autocommit is false.'.format(cls.__name__))
8889

8990
cls._datapoints = defaultdict(list)
90-
cls._type = namedtuple(cls.__name__, cls._fields + cls._tags)
9191

92+
if 'time' in cls._fields:
93+
cls._fields.remove('time')
94+
cls._type = namedtuple(cls.__name__,
95+
cls._fields + cls._tags + ['time'])
9296
return super(SeriesHelper, cls).__new__(cls)
9397

9498
def __init__(self, **kw):
@@ -99,14 +103,17 @@ def __init__(self, **kw):
99103
:warning: Data points are *immutable* (`namedtuples`).
100104
"""
101105
cls = self.__class__
106+
timestamp = kw.pop('time', time.time())
102107

103108
if sorted(cls._fields + cls._tags) != sorted(kw.keys()):
104109
raise NameError(
105110
'Expected {0}, got {1}.'.format(
106111
sorted(cls._fields + cls._tags),
107112
kw.keys()))
108113

109-
cls._datapoints[cls._series_name.format(**kw)].append(cls._type(**kw))
114+
cls._datapoints[cls._series_name.format(**kw)].append(
115+
cls._type(time=timestamp, **kw)
116+
)
110117

111118
if cls._autocommit and \
112119
sum(len(series) for series in cls._datapoints.values()) \
@@ -140,6 +147,7 @@ def _json_body_(cls):
140147
"measurement": series_name,
141148
"fields": {},
142149
"tags": {},
150+
"time": getattr(point, "time")
143151
}
144152

145153
for field in cls._fields:

0 commit comments

Comments
 (0)