Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion influxdb/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Meta:
# Only applicable if autocommit is True.
autocommit = True
# If True and no bulk_size, then will set bulk_size to 1.
retention_policy = 'your_retention_policy'
# Specify the retention policy for the data points
time_precision = "h"|"m"|s"|"ms"|"u"|"ns"
# Default is ns (nanoseconds)
# Setting time precision while writing point
Expand Down Expand Up @@ -83,6 +85,8 @@ def __new__(cls, *args, **kwargs):
'In {0}, time_precision is set, but invalid use any of {}.'
.format(cls.__name__, ','.join(allowed_time_precisions)))

cls._retention_policy = getattr(_meta, 'retention_policy', None)

cls._client = getattr(_meta, 'client', None)
if cls._autocommit and not cls._client:
raise AttributeError(
Expand Down Expand Up @@ -154,9 +158,11 @@ def commit(cls, client=None):
"""
if not client:
client = cls._client

rtn = client.write_points(
cls._json_body_(),
time_precision=cls._time_precision)
time_precision=cls._time_precision,
retention_policy=cls._retention_policy)
# will be None if not set and will default to ns
cls._reset_()
return rtn
Expand Down
51 changes: 51 additions & 0 deletions influxdb/tests/helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,54 @@ class Meta:
.format(WarnBulkSizeNoEffect))
self.assertIn('has no affect', str(w[-1].message),
'Warning message did not contain "has not affect".')

def testSeriesWithRetentionPolicy(self):
"""Test that the data is saved with the specified retention policy."""
my_policy = 'my_policy'

class RetentionPolicySeriesHelper(SeriesHelper):

class Meta:
client = InfluxDBClient()
series_name = 'events.stats.{server_name}'
fields = ['some_stat', 'time']
tags = ['server_name', 'other_tag']
bulk_size = 2
autocommit = True
retention_policy = my_policy

fake_write_points = mock.MagicMock()
RetentionPolicySeriesHelper(
server_name='us.east-1', some_stat=159, other_tag='gg')
RetentionPolicySeriesHelper._client.write_points = fake_write_points
RetentionPolicySeriesHelper(
server_name='us.east-1', some_stat=158, other_tag='aa')

kall = fake_write_points.call_args
args, kwargs = kall
self.assertTrue('retention_policy' in kwargs)
self.assertEqual(kwargs['retention_policy'], my_policy)

def testSeriesWithoutRetentionPolicy(self):
"""Test that the data is saved without any retention policy."""
class NoRetentionPolicySeriesHelper(SeriesHelper):

class Meta:
client = InfluxDBClient()
series_name = 'events.stats.{server_name}'
fields = ['some_stat', 'time']
tags = ['server_name', 'other_tag']
bulk_size = 2
autocommit = True

fake_write_points = mock.MagicMock()
NoRetentionPolicySeriesHelper(
server_name='us.east-1', some_stat=159, other_tag='gg')
NoRetentionPolicySeriesHelper._client.write_points = fake_write_points
NoRetentionPolicySeriesHelper(
server_name='us.east-1', some_stat=158, other_tag='aa')

kall = fake_write_points.call_args
args, kwargs = kall
self.assertTrue('retention_policy' in kwargs)
self.assertEqual(kwargs['retention_policy'], None)