Skip to content

Commit d2bf36c

Browse files
csanz91sebito91
andauthored
Specify Retention Policy in SeriesHelper (influxdata#723)
* Allow specify a retention policy in SeriesHelper * Complete the annotation example with the retention policy * Fix formatting * Fix formatting again * Add helper write with retention policy * Add helper write without retention policy * Remove blank line after the docstring. Co-authored-by: Sebastian Borza <sebito91@gmail.com>
1 parent aad8349 commit d2bf36c

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

influxdb/helper.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ 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+
retention_policy = 'your_retention_policy'
45+
# Specify the retention policy for the data points
4446
time_precision = "h"|"m"|s"|"ms"|"u"|"ns"
4547
# Default is ns (nanoseconds)
4648
# Setting time precision while writing point
@@ -83,6 +85,8 @@ def __new__(cls, *args, **kwargs):
8385
'In {0}, time_precision is set, but invalid use any of {}.'
8486
.format(cls.__name__, ','.join(allowed_time_precisions)))
8587

88+
cls._retention_policy = getattr(_meta, 'retention_policy', None)
89+
8690
cls._client = getattr(_meta, 'client', None)
8791
if cls._autocommit and not cls._client:
8892
raise AttributeError(
@@ -154,9 +158,11 @@ def commit(cls, client=None):
154158
"""
155159
if not client:
156160
client = cls._client
161+
157162
rtn = client.write_points(
158163
cls._json_body_(),
159-
time_precision=cls._time_precision)
164+
time_precision=cls._time_precision,
165+
retention_policy=cls._retention_policy)
160166
# will be None if not set and will default to ns
161167
cls._reset_()
162168
return rtn

influxdb/tests/helper_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,54 @@ class Meta:
376376
.format(WarnBulkSizeNoEffect))
377377
self.assertIn('has no affect', str(w[-1].message),
378378
'Warning message did not contain "has not affect".')
379+
380+
def testSeriesWithRetentionPolicy(self):
381+
"""Test that the data is saved with the specified retention policy."""
382+
my_policy = 'my_policy'
383+
384+
class RetentionPolicySeriesHelper(SeriesHelper):
385+
386+
class Meta:
387+
client = InfluxDBClient()
388+
series_name = 'events.stats.{server_name}'
389+
fields = ['some_stat', 'time']
390+
tags = ['server_name', 'other_tag']
391+
bulk_size = 2
392+
autocommit = True
393+
retention_policy = my_policy
394+
395+
fake_write_points = mock.MagicMock()
396+
RetentionPolicySeriesHelper(
397+
server_name='us.east-1', some_stat=159, other_tag='gg')
398+
RetentionPolicySeriesHelper._client.write_points = fake_write_points
399+
RetentionPolicySeriesHelper(
400+
server_name='us.east-1', some_stat=158, other_tag='aa')
401+
402+
kall = fake_write_points.call_args
403+
args, kwargs = kall
404+
self.assertTrue('retention_policy' in kwargs)
405+
self.assertEqual(kwargs['retention_policy'], my_policy)
406+
407+
def testSeriesWithoutRetentionPolicy(self):
408+
"""Test that the data is saved without any retention policy."""
409+
class NoRetentionPolicySeriesHelper(SeriesHelper):
410+
411+
class Meta:
412+
client = InfluxDBClient()
413+
series_name = 'events.stats.{server_name}'
414+
fields = ['some_stat', 'time']
415+
tags = ['server_name', 'other_tag']
416+
bulk_size = 2
417+
autocommit = True
418+
419+
fake_write_points = mock.MagicMock()
420+
NoRetentionPolicySeriesHelper(
421+
server_name='us.east-1', some_stat=159, other_tag='gg')
422+
NoRetentionPolicySeriesHelper._client.write_points = fake_write_points
423+
NoRetentionPolicySeriesHelper(
424+
server_name='us.east-1', some_stat=158, other_tag='aa')
425+
426+
kall = fake_write_points.call_args
427+
args, kwargs = kall
428+
self.assertTrue('retention_policy' in kwargs)
429+
self.assertEqual(kwargs['retention_policy'], None)

0 commit comments

Comments
 (0)