Skip to content

Commit 56ab720

Browse files
spottxginn8
authored andcommitted
Added support for not including all fields when using the series helper (influxdata#518)
* Added support for not including all fields when using the series helper * fixed flake8 check * Added tests * fixed pep error * more fixes for pep * fixed flake8 errors
1 parent 9d6de13 commit 56ab720

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

examples/tutorial_serieshelper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class Meta:
5151
MySeriesHelper(server_name='us.east-1', some_stat=159, other_stat=10)
5252
MySeriesHelper(server_name='us.east-1', some_stat=158, other_stat=20)
5353
MySeriesHelper(server_name='us.east-1', some_stat=157, other_stat=30)
54-
MySeriesHelper(server_name='us.east-1', some_stat=156, other_stat=40)
54+
MySeriesHelper(server_name='us.east-1', some_stat=156, other_stat=30)
55+
MySeriesHelper(server_name='us.east-1', some_stat=156)
5556
MySeriesHelper(server_name='us.east-1', some_stat=155, other_stat=50)
5657

5758
# To manually submit data points which are not yet written, call commit:

influxdb/helper.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,31 @@ def __new__(cls, *args, **kwargs):
9898
if 'time' in cls._fields:
9999
cls._fields.remove('time')
100100
cls._type = namedtuple(cls.__name__,
101-
cls._fields + cls._tags + ['time'])
101+
['time'] + cls._tags + cls._fields)
102+
cls._type.__new__.__defaults__ = (None,) * len(cls._fields)
102103

103104
return super(SeriesHelper, cls).__new__(cls)
104105

105106
def __init__(self, **kw):
106-
"""Call to constructor creates a new data point. All fields must be present.
107+
"""Call to constructor creates a new data point.
107108
108109
:note: Data points written when `bulk_size` is reached per Helper.
109110
:warning: Data points are *immutable* (`namedtuples`).
110111
"""
111112
cls = self.__class__
112113
timestamp = kw.pop('time', self._current_timestamp())
114+
tags = set(cls._tags)
115+
fields = set(cls._fields)
116+
keys = set(kw.keys())
113117

114-
if sorted(cls._fields + cls._tags) != sorted(kw.keys()):
118+
# all tags should be passed, and keys - tags should be a subset of keys
119+
if not(tags <= keys):
115120
raise NameError(
116-
'Expected {0}, got {1}.'.format(
117-
sorted(cls._fields + cls._tags),
118-
kw.keys()))
121+
'Expected arguments to contain all tags {0}, instead got {1}.'
122+
.format(cls._tags, kw.keys()))
123+
if not(keys - tags <= fields):
124+
raise NameError('Got arguments not in tags or fields: {0}'
125+
.format(keys - tags - fields))
119126

120127
cls._datapoints[cls._series_name.format(**kw)].append(
121128
cls._type(time=timestamp, **kw)
@@ -157,7 +164,9 @@ def _json_body_(cls):
157164
}
158165

159166
for field in cls._fields:
160-
json_point['fields'][field] = getattr(point, field)
167+
value = getattr(point, field)
168+
if value is not None:
169+
json_point['fields'][field] = value
161170

162171
for tag in cls._tags:
163172
json_point['tags'][tag] = getattr(point, tag)

influxdb/tests/helper_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ def testSeriesWithoutTimeField(self, current_timestamp):
231231
self.assertEqual(point1['time'], current_date)
232232
self.assertEqual(point2['time'], yesterday)
233233

234+
def testSeriesWithoutAllTags(self):
235+
"""Test that creating a data point without a tag throws an error."""
236+
class MyTimeFieldSeriesHelper(SeriesHelper):
237+
238+
class Meta:
239+
client = TestSeriesHelper.client
240+
series_name = 'events.stats.{server_name}'
241+
fields = ['some_stat', 'time']
242+
tags = ['server_name', 'other_tag']
243+
bulk_size = 5
244+
autocommit = True
245+
246+
self.assertRaises(NameError, MyTimeFieldSeriesHelper,
247+
**{"server_name": 'us.east-1',
248+
"some_stat": 158})
249+
234250
@mock.patch('influxdb.helper.SeriesHelper._current_timestamp')
235251
def testSeriesWithTimeField(self, current_timestamp):
236252
"""Test that time is optional on a series with a time field."""

0 commit comments

Comments
 (0)