Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Add support for shard duration in retention policies #315

Closed
Closed
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
22 changes: 18 additions & 4 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ def drop_database(self, dbname):
self.query("DROP DATABASE \"%s\"" % dbname)

def create_retention_policy(self, name, duration, replication,
database=None, default=False):
database=None, default=False,
shard_duration=None):
"""Create a retention policy for a database.

:param name: the name of the new retention policy
Expand All @@ -501,19 +502,26 @@ def create_retention_policy(self, name, duration, replication,
:type database: str
:param default: whether or not to set the policy as default
:type default: bool
:param shard_duration: the shard duration of the retention policy.
Durations are similar to those allowed in the duration param.
:type shard_duration: str
"""
query_string = \
"CREATE RETENTION POLICY \"%s\" ON \"%s\" " \
"DURATION %s REPLICATION %s" % \
(name, database or self._database, duration, replication)

if shard_duration:
query_string += " SHARD DURATION %s" % shard_duration

if default is True:
query_string += " DEFAULT"

self.query(query_string)

def alter_retention_policy(self, name, database=None,
duration=None, replication=None, default=None):
duration=None, replication=None, default=None,
shard_duration=None):
"""Mofidy an existing retention policy for a database.

:param name: the name of the retention policy to modify
Expand All @@ -533,9 +541,13 @@ def alter_retention_policy(self, name, database=None,
:type replication: str
:param default: whether or not to set the modified policy as default
:type default: bool
:param shard_duration: the shard duration of the retention policy.
Durations are similar to those allowed in the duration param.
:type shard_duration: str

.. note:: at least one of duration, replication, or default flag
should be set. Otherwise the operation will fail.
.. note:: at least one of duration, replication, default or
shard_duration flag should be set. Otherwise the operation
will fail.
"""
query_string = (
"ALTER RETENTION POLICY \"{0}\" ON \"{1}\""
Expand All @@ -544,6 +556,8 @@ def alter_retention_policy(self, name, database=None,
query_string += " DURATION {0}".format(duration)
if replication:
query_string += " REPLICATION {0}".format(replication)
if shard_duration:
query_string += " SHARD DURATION {0}".format(shard_duration)
if default is True:
query_string += " DEFAULT"

Expand Down
41 changes: 41 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,47 @@ def test_create_retention_policy(self):
'"db" duration 1d replication 4'
)

def test_create_retention_policy_shard_duration(self):
example_response = '{"results":[{}]}'

with requests_mock.Mocker() as m:
m.register_uri(
requests_mock.GET,
"http://localhost:8086/query",
text=example_response
)
self.cli.create_retention_policy(
'somename2', '1d', 4, database='db',
shard_duration='1h'
)

self.assertEqual(
m.last_request.qs['q'][0],
'create retention policy "somename2" on '
'"db" duration 1d replication 4 shard duration 1h'
)

def test_create_retention_policy_shard_duration_default(self):
example_response = '{"results":[{}]}'

with requests_mock.Mocker() as m:
m.register_uri(
requests_mock.GET,
"http://localhost:8086/query",
text=example_response
)
self.cli.create_retention_policy(
'somename3', '1d', 4, database='db',
shard_duration='1h', default=True
)

self.assertEqual(
m.last_request.qs['q'][0],
'create retention policy "somename3" on '
'"db" duration 1d replication 4 shard duration 1h '
'default'
)

def test_alter_retention_policy(self):
example_response = '{"results":[{}]}'

Expand Down